What is Apache Camel

Now a day’s most of enterprise application are design and develop on SOA. When we talk about SOA we are talking about an enterprise application composed of different components. Each component may be develop in different language and technology and will be using a different protocol for communication. With protocol you can think of language which a service will understand. In such a heterogeneous environment first challenge is about effective interaction of different Services/component with each other. Just thinking it seems to be complex enough. It became more complex when we think about other attribute like security, performance and scalability associate with service communication.

Simply thinks of a dining table seated with people speaking different languages, how they can interact with each other. We will definitely needs a middle man who understand each language and help different people to interact. This common man is Apache Camel. Apache Camel is an open source service integration framework. Apache Camel is written in Java and provides a routing engine which can easily transfer a message from one service to another service. While piping a message, Camel can also transform/convert the message in suitable format accepted by target service. This conversion handles by component supported by Camel. There are around 80 components supported by Camel like ftp, file, JMS, cxf etc. You can also define your own components and extend Camel API. Components are simple class which acts as a factory for creating endpoints.

Camel recognizes a service by an endpoint URI. A URI have three parts as components, endpoints and options. In URI ftp://vcjain.com/orders?username=user&password=pass
ftp is component,
vcjain.com/orders is an endpoint and
username and passwords are options

The Endpoints in Camel provides a way through which Services can exchange their messages. You can consider Endpoints as an address to a service.

Now we have an understanding of URI, Let’s see how to create routes using Camel API. Writing a routing logic in Camel is very simple. We only need to extends class RouteBuilder and provide implementation of configure method. Below code will give you a sense of routing logic written in Camel

Public class MyRouter extends RouteBuilder{
  Public void configure(){
    from(ftp://vcjain.com/orders?username=user&password=pass).to(jms:queue:inbound)
  }
}

Above one line in configure method will be enough to transfer a file from FTP server to a JMS Queue. Here both from and to are method with having URI as argument. Camel understands a service using a URI. The URI in from() method have component as FTP. Camel’s FTP component encapsulate all functionality to get data from an FTP server. Location within a FTP server is denoted by endpoint like ‘/orders’ and connecting to FTP server using options username and password in above URI.

A component in camel provides all sort of functionality to tackle a message of a kind. You can see more details for specific components at page http://camel.apache.org/components.html. There are other integration frameworks also available in the market but Apache Camel is more popular because it provides us flexibility to write Routing logic in different language like Java, Scala, Groovy, XML etc. Below is an example of above Java logic in Spring XML style

<route id="my_Sample_Camel_Route_with_CXF">
        <from uri="file:src/data?noop=true"/>
        <to uri=" jms:queue:inbound"/>
</route>

In order to activate a route you need to instantiate a Camel Context object. You can add your Route to camel context using addRoutes () method. Once your routes added in context you can start context using start() method and your program will start picking up messages from FTP, convert them to JMS message format and send it to queue. You can start and stop camel context using start () and stop () methods.

If you want to perform some alteration on message before sending it to JMS queue, you can add processor in camel context. You can create a processor by implement a Processor interface define a single method process(Exchange exchange).
from("ftp://vcjain.com/orders?username=user&password=pass").
process(new Processor() {
public void process(Exchange exchange) throws Exception {
System.out.println("We just downloaded: "
+ exchange.getIn().getHeader("CamelFileName"));
}
}).
to("jms:incomingOrders");
You can add a Processor at any point in a route. Above route will print the filename processed from FTP server before sending it to JMS queue.

I have created a sample project which takes a file from file system and sends it to Stock Quote web service. You can download src from https://github.com/vcjain/apachecamel.git and can run program using maven command mvn camel:run

Camel has implemented many enterprise integration patterns for messaging like Content based routing, message filter, multicast, recipient list, wire tap etc. You can find complete list of supported enterprise integration pattern at http://camel.apache.org/enterprise-integration-patterns.html.


Conclusion: Camel is very effective open source integration framework. It can quickly configured and easily integrate with enterprise application. It provides implementation for various enterprise integration patterns which will make integrating different platform application easy. The open source Enterprise Bus Service Apache ServiceMix toolkit internally uses Apache Camel. I will provide more information on Apache ServiceMix in my next blog.

Comments

Popular posts from this blog

OAuth Simplified

Profiling Java Application using annotation

Dive into WSDL