Camel Integration Framework

Camel is an integration framework that aims to make your integration projects pro- ductive and fun.

At the core of the Camel framework is a routing engine, or more precisely a routing- engine builder. It allows you to define your own routing rules, decide from which sources to accept messages, and determine how to process and send those messages to other destinations. Camel uses an integration language that allows you to define com- plex routing rules, akin to business processes.

One of the fundamental principles of Camel is that it makes no assumptions about the type of data you need to process. This is an important point, because it gives you, the developer, an opportunity to integrate any kind of system, without the need to convert your data to a canonical format.

Camel offers higher-level abstractions that allow you to interact with various sys- tems using the same API regardless of the protocol or data type the systems are using. Components in Camel provide specific implementations of the API that target differ- ent protocols and data types. Out of the box, Camel comes with support for over 80 protocols and data types. Its extensible and modular architecture allows you to imple- ment and seamlessly plug in support for your own protocols, proprietary or not. These architectural choices eliminate the need for unnecessary conversions and make Camel not only faster but also very lean. As a result, it’s suitable for embedding into other projects that require Camel’s rich processing capabilities. Other open source projects, such as Apache ServiceMix and ActiveMQ, already use Camel as a way to carry out enterprise integration.

 

  • Routing and mediation engine
  • Domain-specific language (DSL)
  • Payload-agnostic router
  • POJO model
  • Automatic type converters
  • Test kit
  • Enterprise integration patterns (EIPs)
  • Extensive component library
  • Modular and pluggable architecture
  • Easy configuration
  • Lightweight core
  • Vibrant community

 

sample

public class FileCopierWithCamel {
    public static void main(String args[]) throws Exception {

}

    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {
        public void configure() {
            from("file:data/inbox?noop=true")
                .to("file:data/outbox");

} });

    context.start();
    Thread.sleep(10000);
    context.stop();
}

Sample to copy file from one location to another using camel 


RouteBuilder

The abstract org.apache.camel.builder.RouteBuilder class in Camel is one that you’ll see frequently. You’ll need to use it any time you create a route in Java.

To use the RouteBuilder class, you extend a class from it and implement the con- figure method, like this:

        class MyRouteBuilder extends RouteBuilder {
            public void configure() throws Exception {

… }

}


You then need to add the class to the CamelContext with the addRoutes method:

        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new MyRouteBuilder());

Alternatively, you can combine the RouteBuilder and CamelContext configuration by adding an anonymous RouteBuilder class directly into the CamelContext, like this:

        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {
            public void configure() throws Exception {
                ...

} });

 

ADDING A PROCESSOR

The Processor interface in Camel is an important building block of complex routes. It’s a simple interface, having a single method:

public void process(Exchange exchange) throws Exception;

This gives you full access to the message exchange, letting you do pretty much what- ever you want with the payload or headers.

All EIPs in Camel are implemented as processors. You can even add a simple pro- cessor to your route inline, like so:

from("ftp://rider.com/orders?username=rider&password=secret").
process(new Processor() {
    public void process(Exchange exchange) throws Exception {
        System.out.println("We just downloaded: "
            + exchange.getIn().getHeader("CamelFileName"));

} }).

to("jms:incomingOrders");


Using Spring with Camel

To load up a CamelContext in Spring, you can do the following:

<beans xmlns=”http://www.springframework.org/schema/beans&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd”&gt;

<camelContext xmlns="http://camel.apache.org/schema/spring"/>

</beans>

 

 

          <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
            <property name="connectionFactory">
              <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="vm://localhost" />
              </bean>
            </property>

</bean>

          <camelContext xmlns="http://camel.apache.org/schema/spring">
            <route>
              <from
        uri="ftp://rider.com/orders?username=rider&password=secret"/>
              <to uri="jms:incomingOrders"/>
            </route>
          </camelContext>
        </beans>

This will automatically start a SpringCamelContext, which is a subclass of the DefaultCamelContext you used for the Java DSL.

 

content-based router

As the name implies, a Content-Based Router (CBR) is a message router that routes a message to a destination based on its content. The content could be a message header, the payload data type, part of the payload itself—pretty much anything in the message exchange.

using CBR

from("jms:incomingOrders")
.choice()

.when(predicate) .to(“jms:xmlOrders”)

.when(predicate) .to(“jms:csvOrders”);

You may have noticed that we didn’t fill in the predicates required for each when method. A predicate in Camel is a simple interface that only has a matches method:

public interface Predicate {
    boolean matches(Exchange exchange);

}

 

multicasting

Often in enterprise applications you’ll need to send a copy of a message to several dif- ferent destinations for processing. When the list of destinations is known ahead of time and is static, you can add an element to the route that will consume messages from a source endpoint and then send the message out to a list of destinations

 

With Camel, you can use the multicast method in the Java DSL to implement this

solution:

from("jms:xmlOrders").multicast().to("jms:accounting", "jms:production");


PARALLEL MULTICASTING

Sending messages in parallel using the multicast involves only one extra DSL method: parallelProcessing. Extending the previous multicast example, you can add the parallelProcessing method as follows:

from("jms:xmlOrders")
    .multicast().parallelProcessing()
    .to("jms:accounting", "jms:production");


you can take advantage of the stopOnException feature of the multicast. When enabled, this feature will stop the multicast on the first exception caught, so you can take any necessary action.

To enable this feature, use the stopOnException method as follows:

from("jms:xmlOrders")
    .multicast().stopOnException()
    .parallelProcessing().executorService(executor)
    .to("jms:accounting", "jms:production");


WireTap - to audit orders

By using the wireTap method in the Java DSL, you can send a copy of the exchange to a secondary destination without affecting the behavior of the rest of the route:

from("jms:incomingOrders")
.wireTap("jms:orderAudit")
.choice()
    .when(header("CamelFileName").endsWith(".xml"))
        .to("jms:xmlOrders")
    .when(header("CamelFileName").regex("^.*(csv|csl)$"))
        .to("jms:csvOrders")
    .otherwise()
        .to("jms:badOrders");


Data Transformation with Camel 

Data transformation is a broad term that covers two types of transformation:

  • Data format transformation—The data format of the message body is transformed from one form to another. For example, a CSV record is formatted as XML.
  • Data type transformation—The data type of the message body is transformed from one type to another. For example a java.lang.String is transformed into a javax.jms.TextMessage.