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 5 server.

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

For example on a Linux machine:

$ netstat -an | grep 61616 tcp6       0      0 :::61616                :::*                    LISTEN 

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

package com.example.camel;

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 MainApp {
  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:

<?xml version="1.0" encoding="UTF-8"?><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.example.camel</groupId>
      
   <artifactId>camel-activemq</artifactId>
      
   <packaging>jar</packaging>
      
   <version>1.0-SNAPSHOT</version>
       
   <name>A Camel Route</name>
       
   <properties>
           
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         
   </properties>
       
   <dependencyManagement>
           
      <dependencies>
                
         <!-- Camel BOM -->
                
         <dependency>
                     
            <groupId>org.apache.camel</groupId>
                     
            <artifactId>camel-parent</artifactId>
                     
            <version>3.0.0</version>
                     
            <scope>import</scope>
                     
            <type>pom</type>
                   
         </dependency>
              
      </dependencies>
         
   </dependencyManagement>
       
   <dependencies>
           
      <dependency>
                
         <groupId>org.apache.camel</groupId>
                
         <artifactId>camel-jms</artifactId>
               
      </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.15.12</version>
              
      </dependency>
            
      <dependency>
                
         <groupId>org.apache.activemq</groupId>
                
         <artifactId>activemq-client</artifactId>
                
         <version>5.15.12</version>
              
      </dependency>
           
      <dependency>
                
         <groupId>org.apache.activemq</groupId>
                
         <artifactId>activemq-pool</artifactId>
                
         <version>5.15.12</version>
              
      </dependency>
           
      <dependency>
                
         <groupId>org.apache.camel</groupId>
                
         <artifactId>camel-core</artifactId>
              
      </dependency>
           
      <dependency>
                
         <groupId>org.apache.camel</groupId>
                
         <artifactId>camel-main</artifactId>
              
      </dependency>
            
      <!-- logging -->
           
      <dependency>
                
         <groupId>org.apache.logging.log4j</groupId>
                
         <artifactId>log4j-api</artifactId>
                
         <scope>runtime</scope>
              
      </dependency>
           
      <dependency>
                
         <groupId>org.apache.logging.log4j</groupId>
                
         <artifactId>log4j-core</artifactId>
                
         <scope>runtime</scope>
              
      </dependency>
           
      <dependency>
                
         <groupId>org.apache.logging.log4j</groupId>
                
         <artifactId>log4j-slf4j-impl</artifactId>
                
         <scope>runtime</scope>
              
      </dependency>
            
      <!-- testing -->
           
      <dependency>
                
         <groupId>org.apache.camel</groupId>
                
         <artifactId>camel-test</artifactId>
                
         <scope>test</scope>
              
      </dependency>
         
   </dependencies>
       
   <build>
           
      <defaultGoal>install</defaultGoal>
            
      <plugins>
                
         <plugin>
                     
            <groupId>org.apache.maven.plugins</groupId>
                     
            <artifactId>maven-compiler-plugin</artifactId>
                     
            <version>3.8.1</version>
                     
            <configuration>
                          
               <source>1.8</source>
                          
               <target>1.8</target>
                        
            </configuration>
                   
         </plugin>
                
         <plugin>
                     
            <groupId>org.apache.maven.plugins</groupId>
                     
            <artifactId>maven-resources-plugin</artifactId>
                     
            <version>3.1.0</version>
                     
            <configuration>
                          
               <encoding>UTF-8</encoding>
                        
            </configuration>
                   
         </plugin>
                 
         <!-- Allows the example to be run via 'mvn compile exec:java' -->
                
         <plugin>
                     
            <groupId>org.codehaus.mojo</groupId>
                     
            <artifactId>exec-maven-plugin</artifactId>
                     
            <version>1.6.0</version>
                     
            <configuration>
                          
               <mainClass>com.example.camel.MainApp</mainClass>
                          
               <includePluginDependencies>false</includePluginDependencies>
                        
            </configuration>
                   
         </plugin>
               
      </plugins>
         
   </build>
     
</project>

Execute the Maven application as:

mvn clean install exec:java  

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

activemq camel

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