Apache Camel Integration With ActiveMQ
This article covers integration of Apache Camel with ActiveMQ.
Join the DZone community and get the full member experience.
Join For FreeThis article covers Apache Camel Integration with ActiveMQ.
Setup
Apache MQ Setup
We will launch Apache MQ as a Docker container. Run the below command to launch Apache MQ as a Docker container.
xxxxxxxxxx
docker run -p 61616:61616 -p 8161:8161 rmohr/activemq
Refer: https://hub.docker.com/r/rmohr/activemq
Open the ActiveMQ web console at http://localhost:8161. Click on Manage ActiveMQ broker and login using admin as the username and password when prompted for credentials.
A screen like the below opens:
Let's create two Spring Boot Camel microservices, camel-demo-a
and camel-demo-b
camel-demo-a
will publish the data to the ActiveMQ queue which will be consumed by camel-demo-b
In the pom.xml
of both the microservices, add the below dependency:
xxxxxxxxxx
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-activemq-starter</artifactId>
<version>3.8.0</version>
</dependency>
Configure the broker URL in the application.properties
spring.activemq.broker-url=tcp://localhost:61616
Configuring the ActiveMQ Sender Route in camel-demo-a
The route is configured to read from the file and publish it to the ActiveMQ queue:
xxxxxxxxxx
package com.vignesh.cameldemoa.routes.a;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
public class ActiveMQSenderRoute extends RouteBuilder {
public void configure() throws Exception {
from("file:files/input")
.to("activemq:myqueue");
}
}
Configuring the ActiveMQ Receiver Route in camel-demo-b
Let's assume that the sender route is publishing a JSON message, which we will unmarshall and use to perform some processing.
To the pom.xml
file of the camel-demo-b
application, add the below dependency:
xxxxxxxxxx
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>3.8.0</version>
</dependency>
Creating the Model Class
xxxxxxxxxx
package com.vignesh.cameldemob.model;
public class Employee {
private int id;
private String name;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
The route is configured to consume the message from the ActiveMQ queue, so we'll unmarshal it using the Jackson JSON library and do some processing.
package com.vignesh.cameldemob.route.b;
import com.vignesh.cameldemob.model.Employee;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class ActiveMQReceiverRoute extends RouteBuilder {
GetEmployee getEmployee;
public void configure() throws Exception {
from("activemq:myqueue")
.unmarshal().json(JsonLibrary.Jackson, Employee.class)
.bean(getEmployee)
.to("log:myloggingqueue");
}
}
class GetEmployee{
Logger logger= LoggerFactory.getLogger(GetEmployee.class);
public void getData(Employee employee){
logger.info("Emp data: "+employee.getId());
}
}
Testing
Start the camel-demo-a
application and place the JSON file in the input folder.
The file will be read and the message will be published to the ActiveMQ queue.
Start the camel-demo-b
application. Observe that the route consumes the message from the queue and that it performs JSON unmarshalling as well as further processing.
Opinions expressed by DZone contributors are their own.
Comments