Creating a Camel project (Simple)

Exclusive offer: get 50% off this eBook here
Instant Apache Camel Message Routing [Instant]

Instant Apache Camel Message Routing [Instant] — Save 50%

Route, transform, split, multicast messages, and do much more with Camel with this book and ebook

$14.99    $7.50
by Bilgin Ibryam | August 2013 | Java Open Source

This article by Bilgin Ibryam, author of Instant Apache Camel Message Routing, provides a high-level overview of Camel architecture and explains how to create simple message driven applications.

Camel is a Java based application integration framework. It is lightweight and can run as a standalone application, as part of spring applications, or as an OSGI bundle. Camel can easily connect with many different systems using a variety of connectors, and integrate seamlessly, thanks to multiple messaging pattern implementations. In this first Camel application, we are going to connect to the filesystem and copy files from the source folder to a target folder using a polling consumer.

(For more resources related to this topic, see here.)

Getting ready

For the examples in this article, we are going to use Apache Camel version 2.11 (http://maven.apache.org/) and Apache Maven version 2.2.1 or newer (http://maven.apache.org/) as a build tool. Both of these projects can be downloaded for free from their websites. The complete source code for all the examples in this article is available on github at https://github.com/bibryam/camel-message-routing-examples repository. It contains Camel routes in Spring XML and Java DSL with accompanying unit tests. The source code for this tutorial is located under the project: camel-message-routing-examples/creating-camel-project.

How to do it...

  1. In a new Maven project add the following Camel dependency to the pom.xml:

    <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>${camel-version}</version>
    </dependency>

  2. With this dependency in place, creating our first route requires only a couple of lines of Java code:

    public class MoveFileRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
    from("file://source")
    .to("log://org.apache.camel.howto?showAll=true")
    .to("file://target");
    }
    }

  3. Once the route is defined, the next step is to add it to CamelContext, which is the actual routing engine and run it as a standalone Java application:

    public class Main {
    public static void main(String[] args) throws Exception
    {
    CamelContext camelContext = new
    DefaultCamelContext();
    camelContext.addRoutes(new MoveFileRoute());
    camelContext.start();
    Thread.sleep(10000);
    camelContext.stop();
    }
    }

That's all it takes to create our first Camel application. Now, we can run it using a Java IDE or from the command line with Maven mvn exec:java.

How it works...

Camel has a modular architecture; its core (camel-core dependency) contains all the functionality needed to run a Camel application—DSL for various languages, the routing engine, implementations of EIPs, a number of data converters, and core components. This is the only dependency needed to run this application. Then there are optional technology specific connector dependencies (called components) such as JMS, SOAP, JDBC, Twitter, and so on, which are not needed for this example, as the file and log components we used are all part of the camel-core.

Camel routes are created using a Domain Specific Language (DSL), specifically tailored for application integration. Camel DSLs are high-level languages that allow us to easily create routes, combining various processing steps and EIPs without going into low-level implementation details. In the Java DSL, we create a route by extending RouteBuilder and overriding the configure method. A route represents a chain of processing steps applied to a message based on some rules. The route has a beginning defined by the from endpoint, and one or more processing steps commonly called "Processors" (which implement the Processor interface).

Most of these ideas and concepts originate from the Pipes and Filters pattern from the Enterprise Integration Patterns articlee by Gregor Hohpe and Bobby Woolf. The article provides an extensive list of patterns, which are also available at http://www.enterpriseintegrationpatterns.com, and the majority of which are implemented by Camel.

With the Pipes and Filters pattern, a large processing task is divided into a sequence of smaller independent processing steps (Filters) that are connected by channels (Pipes). Each filter processes messages received from the inbound channel, and publishes the result to the outbound channel. In our route, the processing steps are reading the file using a polling consumer, logging it and writing the file to the target folder, all of them piped by Camel in the sequence specified in the DSL. We can visualize the individual steps in the application with the following diagram:

A route has exactly one input called consumer and identified by the keyword from. A consumer receives messages from producers or external systems, wraps them in a Camel specific format called Exchange , and starts routing them. There are two types of consumers: a polling consumer that fetches messages periodically (for example, reading files from a folder) and an event-driven consumer that listens for events and gets activated when a message arrives (for example, an HTTP server). All the other processor nodes in the route are either a type of integration pattern or producers used for sending messages to various endpoints. Producers are identified by the keyword to and they are capable of converting exchanges and delivering them to other channels using the underlying transport mechanism. In our example, the log producer logs the files using the log4J API, whereas the file producer writes them to a target folder.

The route is not enough to have a running application; it is only a template that defines the processing steps. The engine that runs and manages the routes is called Camel Context. A high level view of CamelContext looks like the following diagram:

CamelContext is a dynamic multithread route container, responsible for managing all aspects of the routing: route lifecycle, message conversions, configurations, error handling, monitoring, and so on. When CamelContext is started, it starts the components, endpoints and activates the routes. The routes are kept running until CamelContext is stopped again when it performs a graceful shutdown giving time for all the in-flight messages to complete processing. CamelContext is dynamic, it allows us to start, stop routes, add new routes, or remove running routes at runtime. In our example, after adding the MoveFileRoute, we start CamelContext and let it copy files for 10 seconds, and then the application terminates. If we check the target folder, we should see files copied from the source folder.

There's more...

Camel applications can run as standalone applications or can be embedded in other containers such as Spring or Apache Karaf. To make development and deployment to various environments easy, Camel provides a number of DSLs, including Spring XML, Blueprint XML, Groovy, and Scala. Next, we will have a look at the Spring XML DSL.

Using Spring XML DSL

Java and Spring XML are the two most popular DSLs in Camel. Both provide access to all Camel features and the choice is mostly a matter of taste. Java DSL is more flexible and requires fewer lines of code, but can easily become complicated and harder to understand with the use of anonymous inner classes and other Java constructs. Spring XML DSL, on the other hand, is easier to read and maintain, but it is too verbose and testing it requires a little more effort. My rule of thumb is to use Spring XML DSL only when Camel is going to be part of a Spring application (to benefit from other Spring features available in Camel), or when the routing logic has to be easily understood by many people.

For the routing examples in the article, we are going to show a mixture of Java and Spring XML DSL, but the source code accompanying this article has all the examples in both DSLs. In order to use Spring, we also need the following dependency in our projects:

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>${camel-version}</version>
</dependency>

The same application for copying files, written in Spring XML DSL looks like the following:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http ://www.springframework.org/schema/beans
http ://www.springframework.org/schema/beans/spring-beans.xsd
http ://camel.apache.org/schema/spring
http ://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file://source"/>
<to uri="log://org.apache.camel.howto?showAll=true"/>
<to uri="file://target"/>
</route>
</camelContext>
</beans>

Notice that this is a standard Spring XML file with an additional CamelContext element containing the route. We can launch the Spring application as part of a web application, OSGI bundle, or as a standalone application:

public static void main(String[] args) throws Exception {
AbstractApplicationContext springContext = new ClassPathXmlApplica
tionContext("META-INF/spring/move-file-context.xml");
springContext.start();
Thread.sleep(10000);
springContext.stop();
}

When the Spring container starts, it will instantiate a CamelContext, start it and add the routes without any other code required. That is the complete application written in Spring XML DSL. More information about Spring support in Apache Camel can be found at http://camel.apache.org/spring.html.

Summary

This article provides a high-level overview of Camel architecture, and demonstrates how to create a simple message driven application.

Resources for Article:


Further resources on this subject:


Instant Apache Camel Message Routing [Instant] Route, transform, split, multicast messages, and do much more with Camel with this book and ebook
Published: August 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :


Bilgin Ibryam

Bilgin Ibryam is a software engineer with Master's degree in Computer Science and currently working for BBC in London. He is interested in a variety of technologies including application integration, message-oriented middleware, service-oriented architecture, and ERP systems. He is also an open source enthusiast, Apache OFBiz, and Apache Camel committer. In his spare time, he enjoys contributing to open source projects and blogging
at www.ofbizian.com.

Bilgin can be contacted via Twitter at https://twitter.com/bibryam.

Books From Packt


Camel Enterprise Integration Cookbook
Camel Enterprise Integration Cookbook

 Instant Apache ServiceMix How-to [Instant]
Instant Apache ServiceMix How-to [Instant]

Drools Developer’s Cookbook
Drools Developer’s Cookbook

Drools JBoss Rules 5.0 Developer's Guide
Drools JBoss Rules 5.0 Developer's Guide

Instant OSGi Starter [Instant]
Instant OSGi Starter [Instant]

 OSGi and Apache Felix 3.0 Beginner's Guide
OSGi and Apache Felix 3.0 Beginner's Guide

Web Services Testing with soapUI
Web Services Testing with soapUI

Apache Axis2 Web Services, 2nd Edition
Apache Axis2 Web Services, 2nd Edition


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
w
f
c
Y
e
a
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software