Wednesday, July 24, 2013

How to use Apache Camel with Eclipse...

What is Apache Camel?

As per its description on wikipediaApache Camel is a rule-based routing and mediation engine which provides a Java object-based implementation of the Enterprise Integration Patterns using an API (or declarative Java Domain Specific Language) to configure routing and mediation rules. The domain-specific language means that Apache Camel can support type-safe smart completion of routing rules in an integrated development environment using regular Java code without large amounts of XML configuration files, though XML configuration inside Spring is also supported.
Camel is often used with Apache ServiceMixApache ActiveMQ and Apache CXF in service-oriented architecture infrastructure projects.
It is a very useful  integration framework based on known Enterprise Integration Patterns. It is also used in Redhat's JBoss Fuse ESB (and open source ESB).

In this blog, I will introduce how we can setup Apache Camel on Eclipse IDE and run simple examples. Our example will use Camel Route builder to queue a message and then consume it.

  1. You will need to download Apache camel from its website. I have Camel v 2.10.1, however newer versions are available.
  2. Need to have Eclipse IDE.
  3. Java 6 or 7 installed.

The example I will be illustrating will use ActiveMQ for sending a message. Hence you will also need activemq-core jar. Take the latest version.
Incase you are planning to use Fuse ESB, then you can download and install Fuse ESB software. It come with activemq jars.

How Camel works with Eclipse:-

Download all the software and setup a new testCamelProj. Add Camel, activemq and other jms required jars.

We are going to use an example, where the Camel RouteBuilder will create a message and put into an activemq jms queue. Then we will use camel-stream to stream it out on console.

Write the Camel RouteBuilder

import org.apache.camel.builder.RouteBuilder;

public class TestSimpleBuilder extends RouteBuilder {
    
    
    @Override
    public void configure() throws Exception {
        // TODO Auto-generated method stub
        
        from("timer://foo?repeatCount=1")
            .setHeader("Header1", constant("Header1"))
            .setBody().simple("Test body")
        .to("jms:queue:incomingA");
                
          
        
        from("jms:queue:incomingA")
            .to("stream:out");
         
     }  
}

In the example above we are using timer to trigger off the route. Setting the header and body and sending the message to a jms queue incomingA. We are using ActiveMQ for queuing.

Then from incoming A queue, we are removing the message and sending to stream out.

Now to run the RouteBuilder

To run this route builder, I have written a simple java application that uses main method to launch it.

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;
private static void testsimplejms(){
        
 {
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
            CamelContext context = new DefaultCamelContext();
            context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
            
            
            context.addRoutes(new TestSimpleBuilder());
            
            context.start();
            Thread.sleep(10000);
            context.stop();
                        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }

The code above, will create a ActiveMQConnectionFactory using vm and also create CamelContext. Then it will add the JMS component created.
Now we add our RouteBuilder to the context. And then start/stop it.

This will run the route and our simple example of putting an element in queue and taking it out.

Hope this was simple to create and run. 

Similar to Apache Camel, you could also use Spring Integration to use EIP features.

1 comment: