Spring Boot Jms ActiveMQ Example

author-image   By Dhiraj,   Last updated on: 12 December, 2017 78K

This article is about spring boot and JMS integration. Here we will be using spring boot features to implement asynchronous messaging service. We wil be using activemq as a message broker and look into different support provided by spring boot while integrating JMS with activemq. The application that we will be creating here will be a simple spring jms activemq example with a sample JMS producer and consumer.

If you are looking for spring 4 and JMS integration with activemq then follow - Spring JMS ActiveMq Exmple

Spring Boot ActiveMQ Support

Spring boot automatically configures connectionfactory if it detects ActiveMQ on the classpath. In this case, it also makes use of embedded broker if does not find any ActiveMQ custom configurations in application.properties. Here in this example we will have activemq configurations outside the application but we will also look into ways to integrate embedded activemq with spring boot by defining our JMS configurations in application.properties. So let us get started with defining the project structure first.

Project Structure


Maven Dependencies

spring-boot-starter-activemq : It provides all the required dependencies to integrate JMS and activemq with spring boot.

activemq-broker : This provides embedded activemq in spring boot application. But since, we will be configuring our activemq outside the application we have commented it for time being.

spring-boot-maven-plugin : It will collect all the jar files present in the classpath and create a single executable jar.



<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency>
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Other Interesting Posts
Spring JMS ActiveMQ Example
Spring Boot Actuator Complete Guide
Spring Boot Security Hibernate Example
Spring Mvc Anglar Js Example
Working with Spring Boot Jdbc Template
Spring Boot Security Password Encoding using Bcrypt Encoder
Websocket spring Boot Integration Without STOMP

Configuring ActiveMQ in Spring Boot

Following is the application.properties which has configurations for activemq. Here we are configuring the tcp broker url along with the username and password required to make activemq connection


Configuring Spring Beans for JMS(Not Required)

While integrating JMS and ActiveMQ with spring boot, no extra configuratins specific to JmsTemplate or containerFactory is required. The following configurations will be implicitly provided by spring boot for us. Hence, the following class is not available in the source. I am including these configurations just to show you guys how Spring Boot helps to get rid of boiler plate codes and configurations.

public class JmsConfig {

	String BROKER_URL = "tcp://localhost:61616"; 
	String BROKER_USERNAME = "admin"; 
	String BROKER_PASSWORD = "admin";
	public ActiveMQConnectionFactory connectionFactory(){
	    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
	    return connectionFactory;

	public JmsTemplate jmsTemplate(){
	    JmsTemplate template = new JmsTemplate();
	    return template;

	public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
	    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
	    return factory;


JMS Listener

@JmsListener:It marks a method to be the target of a JMS messagelistener on the specified destination. In our case the destination is inbound.queue.This class is responsible to listen messsage from the inbound queue and process the same.

@SendTo annotation will take care of sending the return value of receiveMessage() to the destination defined in @SendTo.

public class Listener {

	@JmsListener(destination = "inbound.queue")
	public String receiveMessage(final Message jsonMessage) throws JMSException {
		String messageData = null;
		System.out.println("Received message " + jsonMessage);
		String response = null;
		if(jsonMessage instanceof TextMessage) {
			TextMessage textMessage = (TextMessage)jsonMessage;
			messageData = textMessage.getText();
			Map map = new Gson().fromJson(message, Map.class);
			response  = "Hello " + map.get("name");
		return response;


JMS Producer

Spring boot provides a very convenient way to send message to a destined queue.Since the introduction of @SendTo annotation, it only requires the queue name in the parameter to send messages.Annotating any method with @SendTo annotation ensures the returning object of the method will be pushed to the queue.

Working with JMS Topic

Spring boot provides above discussed default behaviours to work with messaging queue.But to deal with topic in Spring JMS, we require to manually tell spring to enable it.For this, we only require to set the boolean flag i.e. pubSubDomain to true.To do this, we require our JMSConfig.java and set pubSubDomain to true for the bean definition of JmsTemplate and DefaultJmsListenerContainerFactory.Following are the changes we require to do.


Once this is done, rest of the configuration remain same and in place of listening from a queue our aplication can start listening from the topic and publish message to a topic.Following are the lines I have added to consume message from a topic in Listener.java.

@JmsListener(destination = "inbound.topic")
	public String receiveMessageFromTopic(final Message jsonMessage) throws JMSException {
		String messageData = null;
		System.out.println("Received message " + jsonMessage);
		String response = null;
		if(jsonMessage instanceof TextMessage) {
			TextMessage textMessage = (TextMessage)jsonMessage;
			messageData = textMessage.getText();
			Map map = new Gson().fromJson(message, Map.class);
			response  = "Hello " + map.get("name");
		return response;

Once this configuration is done and appliation is deployed you can see the following topics under topics menu.


Spring Bean Configuration

@SpringBootApplication Equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes:

package com.devglan.tiles;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);


Installing Apache ActiveMq

ActiveMq by default exposes a broker url tcp://localhost:61616 and an admin console on tcp://localhost:61616 with userId and password as admin and admin. Following are the steps to download and install activemq. 1. Download apache activemq from here as per your operating system. 2. Extract under some folder. In my case it's under java\apache-activemq-5.11.1-bin. 3. Now traverse to java\apache-activemq-5.11.1-bin\bin\win64 and execute the acivemq.bat file. 4. Open the browser and hit - http://localhost:8161/admin/ 4. Enter userId/password as admin/admin. Then following screen will appear:


5. Now click on the queues option present in menu bar and you can notice there are no queues available as below image.


Run Application

1. Run Application.java as a java applicaton and you can see one queue automatically created with name inbound.queue which we had configured in our Listener.class. Refresh the page after application restart.


2. Our application is now listening to this queue and whenever we push messsage to this queue our Listener should automatically pick this message. Now click on the send To option. Enter a JSON string as {"name":"John"} and click on send.


3. After the messsage is sent, the message is received by Listener.java and the same message will be processed and send back to outbound.queue by Producer.java. Now you can see one more queue created once you click on the queues option from the menu as shown below:


Also, you can check logs in the console.


This demonstrates a simple JMS integration. It is very useful in a mechanism to allow asynchronous request processing. You may wish to implement JMS because the request take a long time to complete or because several parties may be interested in the actual request. Another reason for using it is to allow multiple clients (potentially written in different languages) to access information via JMS.

I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.

Download source

If You Appreciate This, You Can Consider:

  • Like us at: Facebook or follow us at Twitter
  • Share this article on social media or with your teammates.
  • We are thankful for your never ending support.

About The Author


I am an energetic professional who enjoys the challenges involved in working with people and resolving real-time problems. Technical expertise in building highly scalable, distributed and self-healing cloud applications. Technical Skills: Java/J2EE, Spring Framework, Hibernate, Angular, Reactive Programming, Microservices, Rest APIs, Kafka, ELK, etc.

Further Reading on Spring Boot

1 Spring Jms Solace Example

2 Spring Jms Activemq Integration Example

3 Spring Boot Mvc App With Jsp

4 Spring Boot Security Hibernate Login Example

5 Spring Boot Actuator Tutorial Guide

6 Spring Boot Websocket Integration Example