Implementation of Microservices Communication with Apache ActiveMQ

Below is the implementation of microservices communication with Apache ActiveMQ.

Step 1: Setup the Apache ActiveMQ

We can download the ActiveMQ from official website and follow the installation process instructions.

Start ActiveMQ:

Write the below command in cmd and start ActiveMQ.

cd apache-activemq-5.3.2/bin
./activemq start

Image reference:


ActiveMQ Dashboard:

Below is the ActiveMQ Dashboard.

Create the producer-service

Step 1: Create a new Spring Boot project using Spring Initializr, and add the following dependencies:

  • Spring Web
  • Apache ActiveMQ
  • Spring DevTools
  • Lombok

Once the project is created, the folder structure will be like below.


Step 2: Open the application.properties file and add the ActiveMQ connection properties to the project.

spring.application.name=producer-service

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


Step 3: Create the ActiveMQConfig Configuration class

We will create the configuration class to configure ActiveMQ.

Go to src > org.example.produceservice> config > ActiveMQConfig and put the below code.

Java
package org.example.producerservice.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;

@Configuration
@EnableJms
public class ActiveMQConfig {

    private static final String BROKER_URL = "tcp://localhost:61616";

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(BROKER_URL);
        return factory;
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        return new JmsTemplate(connectionFactory());
    }
}


Step 4: Create the MessageService class

Go to src > org.example.produceservice> service > MessageService and put the below code.

Java
package org.example.producerservice.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageService {

    private static final String QUEUE_NAME = "sample-queue";

    @Autowired
    private JmsTemplate jmsTemplate;

    public void sendMessage(String message) {
        jmsTemplate.convertAndSend(QUEUE_NAME, message);
    }
}


Step 5: Create the MessageController class

Go to src > org.example.produceservice> controller > MessageController and put the below code.

Java
package org.example.producerservice.controller;

import org.example.producerservice.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    @PostMapping("/send")
    public String sendMessage(@RequestParam String message) {
        messageService.sendMessage(message);
        return "Message sent: " + message;
    }
}


Step 6: Open the Main Class and write the following code.

Go to src > org.example.produceservice > ProduceServiceApplication and put the below code.

Java
package org.example.producerservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProducerServiceApplication {

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

}


pom.xml:

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>producer-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>producer-service</name>
    <description>producer-service</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


Step 7: Run the application

Once we run the application, then the project will run at port 8081.


Create the consumer service

Step 1: Create a new Spring Boot project using Spring Initializr, and include the following dependencies:

  • Spring Web
  • Apache ActiveMQ
  • Spring DevTools
  • Lombok

Once the project is created, the file structure will be like below.


Step 2: Open the application.properties file and add the ActiveMQ connection properties to the project.

spring.application.name=consumer-service

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


Step 3: Create the ActiveMQConfig Configuration class

We will create the configuration class to configure ActiveMQ.

Go to src > org.example.consumerservice> config > ActiveMQConfig and put the below code.

Java
package org.example.consumerservice.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;

@Configuration
@EnableJms
public class ActiveMQConfig {

    private static final String BROKER_URL = "tcp://localhost:61616";

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(BROKER_URL);
        return factory;
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        return factory;
    }
}


Step 4: Create the MessageListener class

We will create the configuration class to listen the ActiveMQ message broker of the application.

Go to src > org.example.consumerservice> listener > MessageListener and put the below code.

Java
package org.example.consumerservice.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class MessageListener {

    @JmsListener(destination = "sample-queue")
    public void listen(String message) {
        System.out.println("Received message: " + message);
    }
}


Step 5: Open the Main Class and write the following code.

Go to src > org.example.consumerservice > ConsumerServiceApplication and put the below code.

Java
package org.example.consumerservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerServiceApplication {

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

}


pom.xml:

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>consumer-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer-service</name>
    <description>consumer-service</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


Step 6: Run the application

Once we run the application, then the project will run at port 8081.


Producer-service endpoint:

POST http:localhost:8080/send?message=HelloActiveMQ

Output:


ActiveMQ Queues Dashboard

Below is the ActiveMQ queues dashboard.


Consumer-service Application log

Below is the image of Consumer-service Application log.

This project demonstrates the how to microservices communication with the Apache ActiveMQ of the Spring Boot application. The microservice producer-service and consumer-service can establish the communication using the ActiveMQ message broker of the spring application.

Microservices Communication with Apache ActiveMQ

Microservice design involves breaking down an application into smaller tasks that can participate. These tasks often need to interact with each other to meet business needs. Apache ActiveMQ is a popular open-source message broker that facilitates asynchronous communication between microservices. Using message queues, ActiveMQ can ensure that messages are reliably exchanged between services, allowing fragmented and scalable microservice communication

Apache ActiveMQ is a messaging-centric middleware that allows receiving applications to communicate with each other by sending messages via queues and topics In a microservice environment, ActiveMQ can enable services to exchange and view messages asynchronously found that loose coupling and scalability.

Components

  • Producer: The service that sends the messages to a message broker.
  • Consumer: The service that receives the message from the message broker.
  • Message Broker (Apache ActiveMQ): The server that can manage the message queues and topics and ensure reliable message delivery.

Similar Reads

Working of Microservices Communication with Apache ActiveMQ

Message Production...

Implementation of Microservices Communication with Apache ActiveMQ

Below is the implementation of microservices communication with Apache ActiveMQ....

Conclusion

In this article, we have covered the main concepts of the microservices communication with Apache ActiveMQ. It includes the roles of the producers, consumers and the message broker. We also demonstrated the practical example to how to set up and use the ActiveMQ for the inter-service communication....

Contact Us