DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Apache Camel Integration with Kafka
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • The "Zombie API" Attack: Why Your Old Integrations Are Your Biggest Security Risk

Trending

  • Architecting Sub-Microsecond HFT Systems With C++ and Zero-Copy IPC
  • Context Is the New Schema
  • Java Backend Development in the Era of Kubernetes and Docker
  • Improving Java Application Reliability with Dynatrace AI Engine
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Apache Camel Integration With ActiveMQ

Apache Camel Integration With ActiveMQ

This article covers integration of Apache Camel with ActiveMQ.

By 
Vignesh Prabhu user avatar
Vignesh Prabhu
·
Feb. 25, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.8K Views

Join the DZone community and get the full member experience.

Join For Free

This 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.

PowerShell
 




xxxxxxxxxx
1


 
1
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:

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>org.apache.camel.springboot</groupId>
3
    <artifactId>camel-activemq-starter</artifactId>
4
    <version>3.8.0</version>
5
</dependency>


Configure the broker URL in the application.properties

Properties files
 




x


 
1
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:

Java
 




xxxxxxxxxx
1
16


 
1
package com.vignesh.cameldemoa.routes.a;
2

          
3
import org.apache.camel.builder.RouteBuilder;
4
import org.springframework.stereotype.Component;
5

          
6
@Component
7
public class ActiveMQSenderRoute extends RouteBuilder {
8

          
9
    @Override
10
    public void configure() throws Exception {        
11

          
12
        from("file:files/input")
13
                .to("activemq:myqueue");
14
    }
15
}


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: 

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>org.apache.camel.springboot</groupId>
3
    <artifactId>camel-jackson-starter</artifactId>
4
    <version>3.8.0</version>
5
</dependency>


Creating the Model Class

Java
 




xxxxxxxxxx
1
31


 
1
package com.vignesh.cameldemob.model;
2

          
3
public class Employee {
4
    private int id;
5
    private String name;
6

          
7
    public Employee() {
8
    }
9

          
10
    public Employee(int id, String name) {
11
        this.id = id;
12
        this.name = name;
13
    }
14

          
15
    public int getId() {
16
        return id;
17
    }
18

          
19
    public String getName() {
20
        return name;
21
    }
22

          
23
    @Override
24
    public String toString() {
25
        return "Employee{" +
26
                "id=" + id +
27
                ", name='" + name + '\'' +
28
                '}';
29
    }
30
}


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.  

Java
 




x
33


 
1
package com.vignesh.cameldemob.route.b;
2

          
3
import com.vignesh.cameldemob.model.Employee;
4
import org.apache.camel.builder.RouteBuilder;
5
import org.apache.camel.model.dataformat.JsonLibrary;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.stereotype.Component;
10

          
11
@Component
12
public class ActiveMQReceiverRoute extends RouteBuilder {
13

          
14
    @Autowired
15
    GetEmployee getEmployee;
16

          
17
    @Override
18
    public void configure() throws Exception {
19
        from("activemq:myqueue")
20
                .unmarshal().json(JsonLibrary.Jackson, Employee.class)
21
                .bean(getEmployee)
22
                .to("log:myloggingqueue");
23
    }
24
}
25

          
26
@Component
27
class GetEmployee{
28
    Logger logger= LoggerFactory.getLogger(GetEmployee.class);
29
    public void getData(Employee employee){
30
        logger.info("Emp data: "+employee.getId());
31
    }
32
}


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.

Apache Camel Integration

Opinions expressed by DZone contributors are their own.

Related

  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Apache Camel Integration with Kafka
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • The "Zombie API" Attack: Why Your Old Integrations Are Your Biggest Security Risk

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook