Code Monkey home page Code Monkey logo

sample-spring-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 @JmsListener annotated method of a managed bean.

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[]

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. Besides, the method may have a very flexible signature; in our simple example we will just use a simple Email POJO that defines a to and body attributes. Note in particular that this class has no import on the JMS API.

The JmsListener annotation defines the name of the Destination that this method should listen to and the reference to the JmsListenerContainerFactory to use to create the underlying message listener container. Strictly speaking that last attribute is not necessary unless you need to customize the way the container is built as Spring Boot registers a default factory if necessary.

The reference documentation covers this in more detail.

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[]

@EnableJms triggers the discovery of methods annotated with @JmsListener, creating the message listener container under the covers.

For clarity, we have also defined a myFactory bean that is referenced in the JmsListener annotation of the receiver. Because we use the DefaultJmsListenerContainerFactoryConfigurer infrastructure provided by Spring Boot, that JmsMessageListenerContainer will be identical to the one that boot creates by default.

The default MessageConverter is able to convert only basic types (such as String, Map, Serializable) and our Email is not Serializable on purpose. We want to use Jackson and serialize the content to json in text format (i.e. as a TextMessage). Spring Boot will detect the presence of a MessageConverter and will associate it to both the default JmsTemplate and any JmsListenerContainerFactory created by DefaultJmsListenerContainerFactoryConfigurer.

JmsTemplate makes it very simple to send messages to a JMS destination. In the main runner method, after starting things up, you can just use jmsTemplate to send an Email POJO. Because our custom MessageConverter has been automatically associated to it, a json document will be generated in a TextMessage only.

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

By default, Spring Boot creates a JmsTemplate configured to transmit to queues by having pubSubDomain set to false. The JmsMessageListenerContainer is also configured the same. To override, set spring.jms.isPubSubDomain=true via Boot’s property settings (either inside application.properties or by environment variable). Then make sure the receiving container has the same setting.

Note
Spring’s JmsTemplate can receive messages directly through its receive method, but that only works synchronously, meaning it will block. That’s why we recommend that you use a listener container such as DefaultMessageListenerContainer 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 an email message.
Received <Email{[email protected], body=Hello}>

Summary

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

sample-spring-gs-messaging-jms's People

Contributors

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

Watchers

 avatar  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.