You’re going to see a detailed example to get you up and running in record time.
So without further ado, let’s get started…
If you want to learn more about Spring JMS – head on over to the Spring JMS tutorials page.
1. What is Spring JmsTemplate?
The JmsTemplate is a central class from the Spring core package.
It simplifies the use of JMS and gets rid of boilerplate code. It handles the creation and release of JMS resources when sending or receiving messages.
Let’s create a code sample that shows how to configure the Spring JmsTemplate. We will send an order message to an order queue and then synchronously receive a status message from a status queue.
Our project has the following directory structure:
3. Configuring the JmsTemplate
The JmsTemplate was originally designed to be used with a J2EE container. It was the responsibility of the container to provide the necessary pooling of the JMS resources (connections, sessions, consumers and producers).
With the development of frameworks like Spring Boot a different solution was needed. As caching of JMS resources was no longer handled by the container.
This is where the CachingConnectionFactory comes into play. It wraps a JMS provider’s connection to provide caching of sessions, connections, and producers. It also handles automatic connection recovery.
By default, it uses a single session to create many connections. If you need to scale further, you can also specify the number of sessions to cache using the sessionCacheSize property.
Configuring a CachingConnectionFactory is quite simple. Pass a ConnectionFactory and don’t forget to set the sessionCacheSize if you need to scale.
The JmsTemplate requires a reference to a ConnectionFactory. In this example, we pass the CachingConnectionFactory.
If you run on a J2EE runtime obtain the ConnectionFactory from the application’s environment naming context via JNDI.
There are a number of items that are set by default:
Dynamic destination creation is set to queues.
JMS Sessions are “not transacted” and “auto-acknowledged”.
In addition, we set the defaultDestination to which messages should be sent. And as we will also receive messages using the JmsTemplate we also set the receiveTimeout.
Sending messages using the JmsTemplate can be done in two ways:
Using send(messageCreator): The MessageCreator callback interface creates the JMS message.
Using convertAndSend(message, messagePostProcessor): The MessageConverter assigned to the JmsTemplate creates the JMS message. The MessagePostProcessor allows for further modification of the message after it has been processed by the converter.
We will use the second approach to send a simple order message.
Create a Sender class and auto-wire the JmsTemplate.
Define a sendOrder() method that takes an orderNumber String as parameter. The convertAndSend() method on the template will take the String and convert it into a JMS TextMessage.
We use a MessagePostProcessor to retrieve the JMSMessageID. This is a JMS header field that contains the unique identifier of the message. We will use this value to select the corresponding status message that the Receiver will return.
In the receiveOrderStatus() method we use the receiveSelectedAndConvert() method to synchronously receive a status message. This means that this method will block until it receives the message.
In the Receiver class we configure a JmsListener to receive the order message.
We then create a status message on which we apply the JMS Message ID Pattern. We copy the message ID from the request to the correlation ID of the response.
This time we use the send() method on the JmsTemplate to send back the status message. In the MessageCreator we create a TextMessage and set a simple Accepted String as response.
Note that we reuse the same JmsTemplate in both Sender and Receiver class.
You can configure a single JmsTemplate instance and then safely inject this shared reference into multiple collaborators since the template is thread-safe.
5. Testing the JmsTemplate
To test the setup we adapt the existing test case.
We use the Sender to send out an order message. We then receive the status message and check that it contains the Accepted state.
Open a command prompt in the project root directory and run the test case.
In the output logs, we can see that the order and status messages are received.
If you would like to run the above code sample you can get the full source code here.
In this tutorial, you learned how to configure the JmsTemplate to send and receive JMS messages.