How to use Spring JMS with ActiveMQ – JMS Consumer and JMS Producer | Spring Boot

Spring JMS (Java Message Service) is a powerful mechanism to integrate in distributed system.
ActiveMq is a Java Open Source, it is simple JMS solution for concurrent, consumers and producers architecture in integrated development.
The tutorial will guide you how to build a simple application with Spring JMS, ActiveMQ.

Related Articles:
Apache Artemis – How to produce/consume JMS messages with SpringBoot Artemis applications.
ActiveMQ – How to create a SpringBoot ActiveMQ Response Management application by @SendTo annotation
How to start Spring Apache Kafka Application with SpringBoot
Spring Jms ActiveMq – How to send Java object messages to ActiveMQ server (specially with Bi-Directional relationship Java objects)
ActiveMq – How to work with Spring JMS ActiveMq Topic (Publisher-Subcribers pattern) using SpringBoot

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE
– ActiveMQ 5.14.0
– Docker 1.12.0

II. Overview

1. Goal

Our goal is to build an application that receives processing message request from REST Client, in our case, a browser, then use Spring JMS API with Jms Template for JMS Producer and JMS Customer, via Connection Factory to interact with ActiveMQ which is run by Docker, and return the response message to Client.

2. Project Structure

springjms-activemq-project-structure to configure ConnectionFactory
JmsProducer and JmsConsumer autowired JmsTemplate
– interface JmsClient with 2 methods: send and receive.
– implementation of JmsClient autowired JmsProducer, JmsConsumer, JmsClient should be autowired in WebController and will be used to implement method send/receive message.
WebController is a RestController which has request mapping method for RESTful request, such as produce and receive.
Dependencies for Spring Boot Web, JMS and ActiveMQ are added in pom.xml

3. Step to do

– Create Spring Boot project & add Dependencies
– Set up ConnectionFactory
– Create JMS Consumer and JMS Producer
– Create JMS Client
– Create a Web Controller
– Run ActiveMQ by Docker
– Run Spring Boot Application & Enjoy Result

4. Demo Video

III. Practice

1. Create Spring Boot project & add Dependencies

– Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields:
Click Next, then click Finish. Spring Boot project will be created successfully.

– Open pom.xml, add dependencies: Starter Web for working with Spring MVC, ActiveMQ Broker and Spring JMS.


2. Set up ConnectionFactory

Add configuration for ActiveMQ to and setup ConnectionFactory
– The brokerURL property tells the Connection Factory where the message broker is located.
– ActiveMQ user and password for authentication and destination for JMS Queue.
Spring Boot will create a Connection Factory basing on those information automatically.

3. Create JMS Consumer and JMS Producer

If we want to send or receive messages using JMS, we should have a connection to JMS provider, get session, create destination, so JMS API becomes too complex. JmsTemplate is a helper class that simplifies receiving and sending of messages via JMS and removes lots of boilerplate code.
JMS Consumer has JMS Template which automatically created by Spring Boot with the Connection Factory that we created at the previous step.

Under package consumer, create

public class JmsConsumer {
	JmsTemplate jmsTemplate;
	String destinationQueue;
	public String receive(){
		return (String)jmsTemplate.receiveAndConvert(destinationQueue); 

JMS Consumer uses Jms Template method receiveAndConvert to get message from destination queue that we have defined by Connection Factory before in So JMS Consumer just get the value from Annotation value.

JMS Producer is similar to JMS Consumer. It just use another JMS Template method convertAndSend with one more input parameter message.

Content of

public class JmsProducer {
	JmsTemplate jmsTemplate;
	String destinationQueue;
	public void send(String msg){
		jmsTemplate.convertAndSend(destinationQueue, msg);

4. Create JMS Client

This is an abstract object will be used by Controller. Interface JMSClient has 2 methods: send and receive.

Under package client, create

public interface JmsClient {
	public void send(String msg);
	public String receive();

Implementation of JMSClient autowires JMS Consumer and JMS Producer to send and receive message.

Under package client.imp, create

public class JmsClientImpl implements JmsClient{

	JmsConsumer jmsConsumer;
	JmsProducer jmsProducer;
	public void send(String msg) {

	public String receive() {
		return jmsConsumer.receive();


5. Create a Web Controller

WebController receives request from client to implement send or receive message, and return the result.
It is a RestController, we autowire JMS Client that we created at previous step.

Under package controller, create

public class WebController {
	JmsClient jsmClient;
	public String produce(@RequestParam("msg")String msg){
		return "Done";
	public String receive(){
		return jsmClient.receive();

– @RequestMapping(“/produce”): use method send with message as input parameter and return String Done to Client (browser).
– @RequestMapping(“/receive”): use method receive and show Client the message we got.

6. Run ActiveMQ by Docker

Using Docker to run ActiveMQ with command:
docker run -p 61616:61616 -p 8161:8161 -t webcenter/activemq
Open Browser, enter address below with username: admin and password: admin
The browser will show:

It means ActiveMQ is running.

7. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:
Request 1
Produce Request:
Request 2
Consumer Request:

IV. Source Code


12 thoughts on “How to use Spring JMS with ActiveMQ – JMS Consumer and JMS Producer | Spring Boot”

    1. Thank you very much!

      We are on the way to keep the site more helpful!
      You can share with your friends about our articles!

      Nice day!

  1. Hi ,

    Nice Article with clear explanation. I tried with the same example but one thing got stuck with consumer request http://localhost:8080/recieve. It is not showing any thing. Value of number of consumers is showing as 0.

    In logs showing below error.

    2018-01-24 21:14:57.252 INFO 15892 — [ActiveMQ Task-1] o.a.a.t.failover.FailoverTransport : Successfully connected to tcp://localhost:61616


  2. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘jmsClientImpl’:
    Love the example, but keep getting below when run as springapp in STS; appreciate any advice:

    Unsatisfied dependency expressed through field ‘jmsConsumer’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘jmsConsumer’: Unsatisfied dependency expressed through field ‘connectionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘cachingJmsConnectionFactory’ defined in class path resource [org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration$SimpleConnectionFactoryConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.jms.connection.SingleConnectionFactory] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]

  3. To make it getting compiled and run, I had to update the pom.xml to:















    And also I had to change this: see the exclude in :

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration;

    @SpringBootApplication(exclude = ActiveMQAutoConfiguration.class)
    public class SpringbootJmsApplication {

    public static void main(String[] args) {, args);

    Running the application: java -jar spring-jms-activemq-0.0.1.jar gives a clean output.

  4. Good web site! I really love how it is simple on my eyes and the data are well written. I’m wondering how I might be notified when a new post has been made. I’ve subscribed to your feed which must do the trick! Have a nice day!

Leave a Reply

Your email address will not be published. Required fields are marked *