Searching for Apache Camel ActiveMQ Example ?
You are going right.They both work greatly together. ActiveMQ can work as JMS Provider.So It can Produce / Consume JMS message and Apache Camel can route the message to destination end point.

This Apache Camel ActiveMQ Example has covered two different concepts:

One,How to use jms component in camel route.

Jms component has been used to get/put message to/from queue/topic.And Jms component is connecting with running ActiveMQ with default tcp connector.

Second, ActiveMQ as Message Poducer.

In the last example ActiveMQ has been used as message producer and camel is forwarding to next endpoint.

If you don’t like to read long long text,Here is complete video tutorial for Apache Camel ActiveMQ example.

NOTE:This tutorial is not for complete fresher of ActiveMQ/Camel. You must have at-least some prior knowledge of both

So Explanation of the tutorial goes here:

In this post we are going to see few examples of Apache Camel with Active MQ.

  • You will be requiring following Setups:
    ActiveMQ
    Maven – Dependency of Camel
    Eclipse As Tool
  • So,Before sending /Receiving message,You can Start ActiveMQ. This example uses default connector settings.Once ActiveMQ has been started With default connector settings.you can access your web console at by default url.
Start ActiveMQ With default Connector Setting

Start ActiveMQ With default Connector Setting

Apache Camel ActiveMQ Example : 1

  • In the below examples ActiveMQ has been used as Messaging Service Provider and Camel has been used as Message Router.
  • Now,Create One Maven Project and Add Camel Dependencies.
  • Once Dependencies have been added ,Create a Java File as shown below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package puretechy.CamelActiveMQ;
 
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 JmsSampleExample1 {
       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("file:data/userData?noop=true")
                    .to("jms:MyUserData");    
                    }
             });
             context.start();
             Thread.sleep(10000);
             context.stop();
}
}
}
  • In this program, we have added JMS Component.Which will be connecting to ActiveMQ running at tcp://0.0.0.0:61616 connector( By default connector).
  • Add Few Data files at “data/userData” location.We have specified that location in our route so camel will be picking up meaages from that location and forward to JMS queue named MyUserData.
  • So Over all your project structure looks like below:

Jms Sample1

  • In Program,we have just specified name of queue if we don’t specify any thing so,it’s going to create queue not topic.
    With the help of camel message taken from specified location will be routed to queue to so you can check ActiveMQ web console and queues.
  • Run above code and check ActiveMQ Web console,there will be queue created and message en-queued:

Message Enqueue in ActiveMQ Queue

 

Apache Camel ActiveMQ Example : 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package puretechy.CamelActiveMQ;
 
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 JmsSampleExample2 {
       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("jms:MyUserData")
                           .to("stream:out")
                           .to("file:data/outbox");  
                    }
             });
             context.start();
             Thread.sleep(10000);
             context.stop();
}
}
  • When you run this program,again we are connecting to running ActiveMQ with default tcp connector.
  • In this example we are using jms end point as message pick point. Messages are picked from this jms queue, ( streamed out for our understanding only)and forwarded to outbox folder with type message.
    As shown below, you will get messages back from queue. Which you en-queue in previous example 1.
Message De-Queue From Active MQ and Stream Out to console

Message De-Queue From Active MQ and Stream Out to console

  • Note that, in order to run stream component, you need to add dependency of camel stream

Apache Camel ActiveMQ Example : 3

  • This program explains testing with the help of mock point and jms component.
  • In this Example,A topic is created in local running ActiveMQ and messages send to topic with help of template.and message forwarded to mock point,for mock point test are set and checked.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package puretechy.CamelActiveMQ;
 
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.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
 
public class JmsSampleExample3 extends CamelTestSupport {
 
       @Override
       protected CamelContext createCamelContext() throws Exception {
             CamelContext context = super.createCamelContext();
             ConnectionFactory connectionFactory =new ActiveMQConnectionFactory("tcp://localhost:61616");
       context.addComponent("jms",JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
             return context;
       }
 
       @Override
       protected RouteBuilder createRouteBuilder() throws Exception {
             return new RouteBuilder() {
                    @Override
                    public void configure() throws Exception {
                           from("jms:topic:MyTestTopic").to("mock:MyMockTest");
                    }
             };
       }
        @Test
       public void testJmsTopic() throws Exception {
             MockEndpoint quote = getMockEndpoint("mock:MyMockTest");
             // verify the message body
             quote.expectedBodiesReceived("www.puretechy.com");
             template.sendBody("jms:topic:MyTestTopic", "www.puretechy.com");
             quote.assertIsSatisfied();
       }

Apache Camel ActiveMQ Example : 4

  • Here JMS message producer has been created with help of ActiveMQ. This program creates a text message and sends to queue named MyUserData. After running this example you can run sample2 again.It will take messages from queue and forward to next point.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package puretechy.CamelActiveMQ;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageProducerSample {
       public static void main(String[] args) {
              try {
            ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("admin", "admin", ActiveMQConnection.DEFAULT_BROKER_URL);
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("MyUserData");
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
 
            TextMessage message ;
            for(int i=0;i<10;i++)
            {
            message = session.createTextMessage("puretechy.com"+ "     & message count is "+i);
            producer.send(message);
            }
 
            session.close();
            connection.close();
            System.out.println("Message sent");
 
        }
        catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
       }
}

Summery for Apache Camel Activemq examples

In this post,First three samples are using jms component in camel route.
Jms component has been used to get/put message to/from queue/topic.And Jms component is connecting with running ActiveMQ with default tcp connector.

In the last example ActiveMQ has been used as message producer and camel is forwarding to next endpoint.

And, settings of POM.XML file:

<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>learn.all.about.java</groupId>
<artifactId>CamelAndActiveMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.7.0</version>
</dependency>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<version>2.16.2</version>
</dependency>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>2.16.2</version>
</dependency>

</dependencies>
</project>

 

Hope you enjoyed the tutorial ! Let us know your views in comments section.

Thank you.

Learn Lots of stuff for free.Checkout Udemy Coupon code. Completely free.save $$.

About

2 thoughts on “Apache Camel ActiveMQ Example : Along with Text, Video Tutorial And Source Code also available for complete Understanding

  1. Great blog right here! Additionally your web site rather a lot up fast!
    Whatt web host are you thee use of? Can I get your associate link for your host?
    I desire my web site loaded up as fast as yours lol

    1. Chandani says:

      Hi,
      Thanks for your views about my blog.
      Well,Its not that much famous hosting i am using.so still checking for server uptime

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website

%d bloggers like this: