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
Newsletter
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
  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue

Trending

  • Kill the Worker, Keep the Research: Build a Recoverable LangGraph Agent on Temporal
  • Building a Practical Cloud-Native Golden Path: A Guide to Kubernetes-Based Service Delivery, Self-Service, and Developer-Friendly Defaults
  • Going All In On the JVM 25 Virtual Threads
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Feb. 25, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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