Building a Camel route to remote ActiveMQ

In this tutorial we will demonstrate how to create a simple Camel route which sends messages (based on a timer) to an ActiveMQ server.

So first of all, let's start ActiveMQ and verify that it's listening on the TCP port:

For example on a Windows machine:

C:\>netstat -an | find "61616"

  TCP    0.0.0.0:61616          0.0.0.0:0              LISTENING

  TCP    [::]:61616             [::]:0                      LISTENING

And now let's create our simple route using Java DSL:

package com.sample;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

 
public class App {
	public static void main(String args[]) throws Exception {
		CamelContext context = new DefaultCamelContext();
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				"tcp://0.0.0.0:61616");
		context.addComponent("jms",
				JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
		context.addRoutes(new RouteBuilder() {
			public void configure() {
				from("timer:foo?period=1s").setBody(simple("Message at ${date:now:yyyy-MM-dd HH:mm:ss}")).to(
						"jms:queue:activemq/queue/TestQueue");
			}
		});
		context.start();
		Thread.sleep(10000);
		context.stop();
	}
}

As you can see, we are binding to an ActiveMQConnectionfactory listening on the address and port we have just checked.

Next the The JMSComponent allows messages to be sent to (or consumed from) a JMS Queue or Topic. The URI format for messages is:

jms:[queue:|topic:]destinationName[?options]

And next our route which uses as input the timer: component that is used to generate message exchanges when a timer fires. BTW You can only consume events from this endpoint. The body of the message includes a simple transformation of a text and the current date.

The destination is a jms queue named "TestQueue" which is available on ActiveMQ at the JNDI "activemq/queue/TestQueue" (If you don't have it, ActiveMQ will create it for you on the fily).

Here is the pom.xml I have used to build the project:

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample</groupId>
	<artifactId>camel-activemq</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>camel-activemq</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.14.1</version>
		</dependency>

		<!-- camel jms and activemq -->
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jms</artifactId>
			<version>2.14.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-camel</artifactId>
			<version>5.7.0</version>
		</dependency>

		<!-- embed ActiveMQ broker -->
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
			<version>5.10.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-client</artifactId>
			<version>5.10.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
			<version>5.10.1</version>
		</dependency>


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.10</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>1.7.10</version>
		</dependency>

	</dependencies>
</project> 

Execute the Maven application as: 

mvn clean install exec:java -Dexec.mainClass="com.sample.App

 Finally check on the ActiveMQ console that messages have been received:

 activemq camel

 Source code for this example available at: https://github.com/fmarchioni/mastertheintegration/tree/master/camel/jms/camel-activemq


Advertisement

Cookie Alert