by BehindJava

How to create a sender and receiver that produces and consumes messages to ActiveMQ using Apache Camel with SpringBoot Micro services a and b

Home » springboot » How to create a sender and receiver that produces and consumes messages to ActiveMQ using Apache Camel with SpringBoot Micro services a and b

In this tutorial we are going to learn about creating a route with Apache Camel that produces messages from the sender router of microservice A to the ActiveMQ and consumer router that consumes messages from ActiveMQ of microservice B.

Firstly, Go through these tutorials for Creating MicroServices and Configuring ActiveMQ in your local machine.

Once you have all the applications up and running add these dependencies to your Microservice applications i.e., related to ActiveMQ as given below.

<dependency>
			<groupId>org.apache.camel.springboot</groupId>
			<artifactId>camel-activemq-starter</artifactId>
			<version>3.14.1</version>
		</dependency>

Also add the ActiveMQ url on which it is running in the application.properties file.

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

Java class

package com.example.apachemsa.activemq;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MessageSenderRoute extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("timer:activemq-timer-route?period=10000")
        .transform().constant("Hello from BehindJAVA")
	    .log("${body}")
        .to("activemq:route-activemq-queue");
	}
}

Now, Once all the above things are done. Start the ActiveMQ docker image, Spring boot microservice A as shown below.

docker

You will see the messages that queued from the sender microservice application in the ActiveMQ.

docker

Configure the receiver with the same dependencies and properties mentioned above in a different spring boot microservice application i.e., b and since Microservice a is running on the port number 8080, you need to run the microservice b on a different port i.e., 8000.

server.port=8000
spring.activemq.broker-url=tcp://localhost:61616

create a new Java class with name MessageReceiverRoute.

package com.example.apachemsb.activemq;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MessageRecieverRoute extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("activemq:route-activemq-queue")
        .to("log:reciever-mesages-activemq");

	}
}

On the ActiveMQ you can see the consumer that consumes the messages queued.

reciever