Apache Camel

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.
  4. Coding and compiling the route

    Here is the Java source code for our basic file copier route; it is a single Java source file called FileCopier.java.
    /*
    Simple Camel file copier 
    
    (c)2013 Kevin Boone
    */
    import org.apache.camel.*;
    import org.apache.camel.impl.*;
    import org.apache.camel.builder.*;
    
    public class FileCopier
      {
      public static void main(String args[]) throws Exception
        {
        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder()
          {
          public void configure()
            {
            // Set up the route — from the intput directory, to
            //  the output directory, with no other processing
            from ("file:/tmp/in?noop=true").to("file:/tmp/out");
            }
          });
        //
        // Start the Camel route
        context.start();
    
        // Wait five minutes, then stop
        Thread.sleep (60*5*1000);
        context.stop ();
        }
      }
    
    This code obtains a CamelContext — a representation of the Camel routing engine — and adds a route to it. The route is specified in the form of an anonymous class that implements theRouteBuilder interface. The route itself consists of one line of code:
    from ("file:/tmp/in?noop=true").to("file:/tmp/out");
    
    which sets up an assication between two directory endpoints. The action happens from as soon as we call context.start() and continues until (you guessed it) the call to context.stop(). The relevant directories are /tmp/in and /tmp/out; of course you can change these if necessary.
    To compile this route, we simply compile this Java source, with a class search path that references the camel-core JAR. For Camel version 2.12.1, the relevant JAR is called camel-core-2.12.1.jar, in the Camel lib directory.
    To compile the class at the command line:
    $ javac -classpath=/path/to/camel/lib/camel-core-2.12.1.jar FileCopier.java
    
    This should produce FileCopier.class.

No comments:

Post a Comment