So if you’re a Spring JMS beginner, you’ll love this guide.
Let’s get this show on the road!
If you want to learn more about Spring JMS – head on over to the Spring JMS tutorials page.
1. What is Spring JMS?
Spring provides a JMS integration framework that simplifies the use of the JMS API. Much like Spring’s integration does for the JDBC API.
The Spring Framework will take care of some low-level details when working with the JMS API.
In this tutorial, we will create a Hello World example in which we will send/receive a message to/from Apache ActiveMQ using Spring JMS, Spring Boot, and Maven.
2. General Project Overview
We will use the following tools/frameworks:
Spring JMS 5.1
Spring Boot 2.1
ActiveMQ 5.15
Maven 3.6
Our project has the following directory structure:
Let’s use Spring Initializr to generate our Maven project. Make sure to select JMS (ActiveMQ) as a dependency.
Click Generate Project to generate and download the Spring Boot project template. At the root of the project, you’ll find a pom.xml file which is the XML representation of the Maven project.
To avoid having to manage the version compatibility of the different Spring dependencies, we will inherit the defaults from the spring-boot-starter-parent parent POM.
The generated project contains Spring Boot Starters that manage the different Spring dependencies.
You can find back the exact dependency versions in Appendix F of the reference documentation. For Spring Boot 2.1.5 the ActiveMQ dependency is version 5.15.9.
The spring-boot-starter-activemq dependency includes the needed dependencies for using Spring JMS in combination with ActiveMQ.
The spring-boot-starter-test includes the dependencies for testing Spring Boot applications with libraries that include JUnit, Hamcrest and Mockito.
In the plugins section, you’ll find the Spring Boot Maven Plugin. spring-boot-maven-plugin allows us to build a single, runnable “uber-jar”. This is a convenient way to execute and transport code.
Also, the plugin allows you to start the example via a Maven command.
4. Spring Boot Setup
Spring Boot is used in order to make a Spring JMS example application that you can “just run”.
We start by creating a SpringJmsApplication which contains the main() method that uses Spring Boot’s SpringApplication.run() method to launch the application.
The @SpringBootApplication annotation is a convenience annotation that adds: @Configuration, @EnableAutoConfiguration and @ComponentScan.
The below sections will detail how to create a sender and receiver together with their respective configurations.
It is also possible to have Spring Boot autoconfigure Spring JMS using default values so that actual code that needs to be written is reduced to a bare minimum.
5. Create a Spring JMS Message Producer
For sending messages we will be using the JmsTemplate which requires a reference to a ConnectionFactory. The template provides convenience methods which handle the creation and release of resources when sending or synchronously receiving messages.
In the below Sender class, the JmsTemplate is auto-wired as the actual creation of the Bean will be done in a separate SenderConfig class.
In this tutorial we will use the convertAndSend() method which sends the given object to the helloworld.q destination, converting the object to a JMS message.
The type of JMS message depends on the type of the object being passed. In the case of a String, a JMS TextMessage will be created.
The creation of the JmsTemplate and Sender is handled in the SenderConfig class. This class is annotated with @Configuration which indicates that the class can be used by the Spring IoC container as a source of bean definitions.
In order to be able to use the Spring JMS template, we need to provide a reference to a ConnectionFactory which is used to create connections with the JMS provider. In addition, it encapsulates various configuration parameters, many of which are vendor specific.
In the case of ActiveMQ, we use the ActiveMQConnectionFactory.
On the ActiveMQConnectionFactory we set the broker URL which is fetched from the application.yml properties file using the @Value annotation.
The JmsTemplate was originally designed to be used in combination with a J2EE container where the container would provide the necessary pooling of the JMS resources.
As we are running this example on Spring Boot, we will wrap ActiveMQConnectionFactory using Spring’s CachingConnectionFactory in order to still have the benefit of caching of sessions, connections, and producers as well as automatic connection recovery.
6. Create a Spring JMS Message Consumer
Like with any messaging-based application, you need to create a receiver that will handle the messages that have been sent. The below Receiver is nothing more than a simple POJO that defines a method for receiving messages. In this guide we named the method receive(), but you can name it anything you like.
The @JmsListener annotation creates a message listener container behind the scenes for each annotated method, using a JmsListenerContainerFactory. By default, a bean with name jmsListenerContainerFactory is expected that we will set up in the next section.
Using the destination element, we specify the destination for this listener. In the below example we set the destination to helloworld.q.
For testing convenience, we added a CountDownLatch. This allows the POJO to signal that a message is received. This is something you are not likely to implement in a production application.
The creation and configuration of the different Spring Beans needed for the Receiver POJO are grouped in the ReceiverConfig class.
Note that we need to add the @EnableJms annotation to enable support for the @JmsListener annotation that was used on the Receiver.
The jmsListenerContainerFactory() is expected by the @JmsListener annotation from the Receiver.
Spring Boot will automatically start an embedded broker if the following conditions are met:
ActiveMQ is on the classpath
No broker URL is specified through spring.activemq.broker-url
Let’s use the embedded broker for testing. We add a dedicated application.yml properties file under src/test/resources. Inside we specify the VM URI as broker connection URL.
Next, we create a basic SpringJmsApplicationTest class to verify that we are able to send and receive a message to and from ActiveMQ.
It contains a testReceive() unit test case that uses the Sender to send a message to the ‘helloworld.q’ queue on the ActiveMQ message broker. We then use the CountDownLatch from the Receiver to verify that a message was received.
In order to execute the above test, open a command prompt in the project root directory and run following Maven command:
Maven will download the dependencies, compile the code and run the unit test case. The result should be a successful build as shown below:
Above test case can also be executed after you install Apache ActiveMQ on your local system.
Simply change the ‘activemq:broker-url’ property to point to ‘tcp://localhost:61616’ in case the broker is running on the default URL.
If you would like to run the above code sample you can get the full source code here.
This concludes our example in which we used Spring JMS and Spring Boot in order to connect to ActiveMQ.
If you found this sample useful or have a question you would like to ask.