ActiveMQ in Spring boot using JMSTemplate

Ankitha Gowda
3 min readOct 15, 2021

--

Photo by Bryan Hanson on Unsplash

Hey there, In this article let us know about ActiveMQ messaging queues for asynchronous service-to-service communication used in serverless and microservices architectures.

As the name conveys, a queue gets and removes data in First In First Out order. There are many types of message queues available like — IBM MQ, ActiveMQ, RabbitMQ etc.

In Spring, there are in-build templates available for this purpose. If you are using ActiveMQ, we can use JMSTemplate. Let’s look at how it works.

In some cases, we may want to share the message to a single application or a group of applications. There comes the concept of — Queues and Topics.

Queue: Point-to-Point communication

The message will be held by container until it get consumed. There will be a single producer and single consumer.

Topic: Publisher-Subscriber communication.

The message will be placed by a producer. Here the consumers can be more than one. So, the message will be present in container till its read by all consumers.

We need to have the ActiveMQ up and running in a machine. You can download and setup ActiveMQ here.

We need below dependency in our pom.xml file:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

By default, ActiveMQ will run in port 61616 and we need to specify these connection details in application.properties or application.yaml file:

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin

In Configuration class, we need to create a bean of JMSTemplate as below referring environment variables from properties file.

In above code snippet, we need some beans to be created for customising the ActiveMQ behaviour:

  • ActiveMQConnectionFactory bean: It is required to establish connection to ActiveMQ. Refer additional options at Apache ActiveMQ documentation
  • MessageConverter bean: It is used to specify the converter between Java objects and JMS messages. Here, we are converting the message to TextMessage.
  • DynamicDestinationResolver bean : It is needed when a same application is having both queues and topics. In JMSTemplate we have single method send() for both queues and topics. It will be decided based on value of flag pubSubDomain at runtime. It could be done by overriding default implementation of resolveDestinationName() in DynamicDestinationResolver class.
  • JMSTemplate bean: Needed to perform all ActiveMQ related operations. The deliveryMode could be DeliveryMode.PERSISTENT (messages will survive a broker restart) or DeliveryMode.NON_PERSISTENT(will lose all in-transit messages on failure). By default the deliveryMode is PERSISTENT.

Note: KahaDB is the the default storage mechanism since ActiveMQ 5.4

Sending messages:

All these thing are required for configuring the ActiveMQ in a Spring application. To place messages into messaging queue:

The above code snippet is having two methods one for sending messages into queue and another to a topic. Both are having same implementation and calling the same send() method of JMSTemplate as discussed before. We can optimise the above code a bit with common method for both queue and topic.

Receiving messages:

In Spring we have @JmsListener annotation that could be applied on method level which acts as receiver of message from the destination specified in this annotation.

We can forward the message to next destination using @SendTo annotation on listener method.

Note: To forward a message to next container, we need to return the value from method annotated as listener.

In above code snippet, in receiveAndForwardMessageFromQueue function we are receiving a message from queue myQueue and forwarding it to another queue myQueue2.

In other two functions we are receiving the data from topic myTopic. Thus, we are creating 2 consumers for single topic myTopic. Here also, we can optionally forward the message received.

Note: We can forward message from a queue to another queue only. But in case of topic, we can forward to a queue or another topic.

You can always explore more about ActiveMQ in official documentation.

Thank you for reading. Happy exploring!!!

The complete code can be found on GitHub here

--

--