In this post I’m going to show you EXACTLY how to auto-configure Spring JMS using annotations and Spring Boot.
So if you want to get up and running with a minimum amount of coding, then you’ll love this guide.
Let’s jump right in…
If you want to learn more about Spring JMS – head on over to the Spring JMS tutorials page.
1. What is Spring Boot Auto-Configuration?
Spring Boot auto-configuration will try to automatically configure your Spring application based on the JAR dependencies that are available.
In other words, if the spring-jms and activemq-broker dependencies are on the classpath and you have not manually configured the ConnectionFactory, JmsTemplate or JmsListenerContainerFactory beans, then Spring Boot will auto-configure them for you using default values.
To show this behavior we will start from a previous Spring JMS tutorial in which we send/receive messages to/from ActiveMQ using Spring JMS.
The original code will be reduced to a bare minimum in order to demonstrate Spring Boot’s autoconfiguration capabilities.
We will also use Spring JMS annotations to ease the JMS implementation.
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:
Needed dependencies like Spring Boot and Spring JMS are included by declaring the spring-boot-starter-activemqSpring Boot starter in the POM file as shown below.
4. Spring Boot Setup
The main SpringJmsApplication class remains untouched.
Note that in order for the auto-configuration to work we need to opt-in by adding the @EnableAutoConfiguration or @SpringBootApplication annotation to one of our @Configuration classes.
Only ever add one @EnableAutoConfiguration annotation. It is recommended to add it to your primary @Configuration class.
5. Autoconfigure the Spring JMS Message Producer
The setup and creation of the JmsTemplate and ConnectionFactory beans are automatically done by Spring Boot. We just need to auto-wire the JmsTemplate and use it in the send() method.
By annotating the Sender class with @Component, Spring will instantiate this class as a bean that we will use in a below test case. In order for this to work, we also need the @EnableAutoConfiguration which was indirectly specified on SpringJmsApplication by using the @SpringBootApplication annotation.
6. Autoconfigure the Spring JMS Message Consumer
Similar to the Sender, the setup and creation of the ConnectionFactory and JmsListenerContainerFactory beans are automatically done by Spring Boot. The @JmsListener annotation creates a message listener container for the annotated receive() method.
The destination name is specified using the ‘${destination.boot}’ placeholder for which the value will be fetched from the application.yml properties file.
Using application properties we can further fine-tune the different settings of the ConnectionFactory, JmsTemplate and JmsListenerContainerFactory beans.
In this example, we use default values and only specify the destination and broker URL in the included application.yml properties file.
7. Testing the Sender and Receiver
In order to verify that our code works, a simple SpringJmsApplicationTest test case is used.
It contains a testReceive() unit test case that uses the Sender to send a message to the ‘boot.q’ queue on the ActiveMQ broker. We then use the CountDownLatch from the Receiver to verify that a message was successfully received.
We include a dedicated application.yml properties file for testing under src/test/resources that does not contain a broker URL.
If Spring Boot does not find a broker URL, auto-configuration will automatically start an embedded ActiveMQ broker instance.
Let’s run the test case. Execute the following Maven command at the command prompt:
The test case will be triggered resulting in following log statements:
If you would like to run the above code sample you can get the full source code here.
In the above example, we were able to autoconfigure a JMS connection to ActiveMQ using Spring annotations and a couple of lines of code.
Drop a comment in case you thought the example was helpful or if you found something was missing.