ActiveMQ with Spring Boot

About

Apache ActiveMQ is an open-source message broker that implements the Java Message Service (JMS) and the Message Queuing Telemetry Transport (MQTT) protocol. It provides scalable, reliable, and high-performance message queuing and publish-subscribe messaging systems. ActiveMQ allows communication between applications by sending and receiving messages. It supports various messaging patterns such as point-to-point, publish-subscribe, request-reply, and others.

When integrated with Spring Boot, ActiveMQ becomes even easier to configure and use. Spring Boot provides built-in support for JMS, auto-configuration, and streamlined connection management, allowing developers to send and receive messages with minimal boilerplate code.

By combining ActiveMQ with Spring Boot, applications can:

  • Decouple components for better scalability.

  • Handle high-throughput asynchronous processing.

  • Integrate with microservices and legacy systems via standard messaging protocols.

This integration is widely used for real-time notifications, event-driven architectures, order processing systems, and inter-service communication in enterprise applications.

Setup ActiveMQ using docker-compose

docker-compose.yaml

version: "3.9"
# https://docs.docker.com/compose/compose-file/

services:
  # ActiveMQ web url - http://localhost:8161/admin
  activemq:
    container_name: activemq
    image: rmohr/activemq:5.15.4
    ports:
      - "61616:61616"
      - "8161:8161"
    environment:
      ACTIVEMQ_ADMIN_LOGIN: admin
      ACTIVEMQ_ADMIN_PASSWORD: admin

networks:
  default:
    name: company_default

Run the docker-compose file to start the ActiveMQ service

ActiveMQ console url

ree

Setup Spring Boot Application

In this example, we will create a Spring Boot application that sets up a queue and a topic in ActiveMQ based on the configuration provided in the application.yaml file. We will then implement separate producers that sends messages to the queue and the topic every 5 seconds. Additionally, we will create separate consumers for the queue and the topic.

Let's craft pom.xml file and add the required dependencies

Let's look at application.yml file. Set the queue-name and topic-name as per need. Application will create the queue and topic with given name in ActiveMQ

Now, we need to create a Config class which will establish connection with ActiveMQ, create queue and topic with given name and expose DefaultJmsListenerContainerFactory and JmsTemplate spring beans with configuration for queue and topic. DefaultJmsListenerContainerFactory bean will be used with @JmsListener annotation and JmsTemplate will be used to produce messages.

JmsConfig.java

It's time to create consumers for queue and topic.

ConsumerService.java

Let's create a class with helpful methods to send and receive message

PubSubService.java

QueueService.java

At last we will create producers which will produce message on the topic and queue every 4 seconds with the help of scheduler.

Note: We are using RandomStringUtils to generate random strings

MessageSchedulerService.java

Finally, we will build our application and run to see it in action.

We can verify from the logs that messages are getting produced and consumed from the queue and topic

ree

Let's verify from the ActiveMQ console as well.

Queues

ree

Topics

ree

Additional Feature

We can create a controller with endpoints to produce and consume message from the queue and topic.

Files are attached for the reference below.

Last updated