Apache Camel + JMS (ActiveMQ) Hello World Example
Overview
Previous tutorial we had implemented Apache Camel Example to use File Component and seen what are Routes and Components. In this tutorial we will making use of Apache Camel JMS Component. We will be reading from one JMS Queue and sending the message to another jms queue. Also we will be making use of ActiveMQ queues.Table Of Contents :
Technology Stack
We will be making use of-- Java 1.8
- Apache Camel 3.0.0-M2
- Maven
- ActiveMQ 5.15.9
Implementation
ActiveMQ Installation and Startup
-
Go to ActiveMq Downloads Page and select the ActiveMQ downloads depending on your operating system.
-
Go to the location where you have unzipped the downloaded ActiveMQ installable and start ActiveMQ as follows-
-
Go to localhost:8161
-
Select Manage ActiveMQ Broker and you will be prompted for credentials. Enter the username and password as admin.
-
Select Queues. Currently we will see there are no Queues.
Apache Camel JMS implementation
The maven project we will be creating is as follows-The pom.xml will be as follows-
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codeusingjava</groupId>
<artifactId>apache-camel-jms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>3.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
<version>5.15.9</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.15.9</version>
</dependency>
</dependencies>
</project>
For this example we make use of Java DSL for writing Routes.
A Route consists of Message Channel for using which applications communicate using Messages.
The end points of these message channels either consume of produce messages.What are Components?
Component act as an endpoint factory using which we can interact with external systems. Camel provides a large number of components using which we can interact with externals systems.We have to transfer a data from one JMS Queue to another JMS Queue so we make use of the JMS Component.
In Apache Camel for creating a route we need to extend RouteBuilder class and override configure method. Then in configure method we define our route. So our route using Java DSL will be as follows-
package com.codeusingjava.route;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
public class JMSRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
from("jmsComponent:queue:codeusingjava-inputqueue").process(new Processor() {
public void process(Exchange exchange) throws Exception {
System.out.println(exchange.getIn().getBody());
}
}).to("jmsComponent:queue:codeusingjava-outputqueue");
}
}
What is CamelContext?
We cannot run our Camel Application only using the route. We also need the camel context which acts as a runtime system that runs and manages the routes. It is responsible for all managing all aspects of a route.- 1. Create CamelContext.
- 2. Add routes to the CamelContext.
- 3. Start the CamelContext. So that the Routes get executed.
- 4. Stop the Camel Context.
package com.codeusingjava;
import javax.jms.ConnectionFactory;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
import com.codeusingjava.route.JMSRouteBuilder;
public class CamelMain {
public static void main(String[] args) {
CamelContext ctx = new DefaultCamelContext();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
ctx.addComponent("jmsComponent", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
JMSRouteBuilder jmsRouteBuilder = new JMSRouteBuilder();
try {
ctx.addRoutes(jmsRouteBuilder);
ctx.start();
Thread.sleep(5 * 60 * 1000);
ctx.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
If we send a message in codeusingjava-inputqueue it gets transferred to the codeusingjava-outputqueue .