Giter Club home page Giter Club logo

gs-messaging-jms's Introduction

tags projects
messaging
jms
spring-framework

This guide walks you through the process of publishing and subscribing to messages using a JMS broker.

What you’ll build

You’ll build an application that uses Spring’s JmsTemplate to post a single message and subscribes to it with a POJO using MessageListenerAdapter.

Create a message receiver

Spring provides the means to publish messages to any POJO.

src/main/java/hello/Receiver.java

link:complete/src/main/java/hello/Receiver.java[role=include]

This is also known as a message driven POJO. As you can see in the code above, there is no need to implement any particular interface or for the method to have any particular name.

Send and receive JMS messages with Spring

Next, wire up a sender and a receiver.

src/main/java/hello/Application.java

link:complete/src/main/java/hello/Application.java[role=include]

To wrap the Receiver you coded earlier, use MessageListenerAdapter. Then use the setDefaultListenerMethod to configure which method to invoke when a message comes in. Thus you avoid implementing any JMS or broker-specific interfaces.

The SimpleMessageListenerContainer class is an asynchronous message receiver. It uses the MessageListenerAdapter and the ConnectionFactory and is fired up when the application context starts. Another parameter is the queue name set in mailboxDestination. It is also set up to receive messages in a publish/subscribe fashion.

Spring provides a convenient template class called JmsTemplate. JmsTemplate makes it very simple to send messages to a JMS message queue. In the main runner method, after starting things up, you create a MessageCreator and use it from jmsTemplate to send a message.

Two beans that you don’t see defined are JmsTemplate and ActiveMQConnectionFactory. These are created automatically by Spring Boot. In this case, the ActiveMQ broker runs embedded.

Note
Spring’s JmsTemplate can receive messages directly through its receive method, but that only works synchronously, meaning it will block. That’s why Spring recommends that you use a listener container such as SimpleMessageListenerContainer with a cache-based connection factory, so you can consume messages asynchronously and with maximum connection efficiency.

When it runs, buried amidst all the logging, you should see these messages:

Sending a new message.
Received <ping!>

Summary

Congratulations! You’ve just developed a publisher and consumer of JMS-based messages.

gs-messaging-jms's People

Contributors

gregturn avatar joshlong avatar cbeams avatar btalbott avatar royclarkson avatar habuma avatar snicoll avatar fisco-unimatic avatar

Watchers

James Cloos avatar zyro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.