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:
ActiveMQ – How to create a SpringBoot ActiveMQ Response Management application by @SendTo annotation
How to start Spring Apache Kafka Application with SpringBoot
Spring JMS – Explicitly Configure Spring ActiveMQ ConnectionFactory | 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

springjms-activemq-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
application.properties 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:
springjms-newproject
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 application.properties 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 JmsConsumer.java:

JMS Consumer uses Jms Template method receiveAndConvert to get message from destination queue that we have defined by Connection Factory before in application.properties. 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 JmsProducer.java

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 JmsClient.java

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

Under package client.imp, create JmsClientImpl.java

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 WebController.java

– @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
localhost:8161/admin/
The browser will show:
activemq-welcome

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:
http://localhost:8080/produce?msg=hello
springjms-activemq-producerequest
Request 2
Consumer Request:
http://localhost:8080/receive
springjms-activemq-consumerequest

IV. Source Code

Springboot-JMS-ActiveMQ


Related Posts



2 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!

Got Something To Say:

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

*