Camel Routes

DZone 's Guide to

Camel Routes

Learn how to configure and use Apache Camel, an open source Java tool for building a file processing and integration architecture.

· Integration Zone ·
Free Resource

This article is about Apache Camel Java DSL basic functions.

Camel is a very strong tool in the Java Open Source Ecosystem to do integration or create an integration layer easily in a readable and solid format manner. Most of the case is one liner syntax.

My code examples come from real life and proven examples.

Adding Apache Camel Libraries

We added some of the Camel artifacts to our pom.xml if you use Maven for your project build.


Configuration of a Route

First of all, you need to define a route by creating a class which extends RouteBuilder, and you override the configure() method.

public class ImportRouteBuilder extends RouteBuilder {

    public void configure() throws Exception {
    // define the route

Inside configure() you can use from(),to() method like below.


Let's suppose you want to download incoming files from an ftp server and want to process its files' contents, then move the file to an archive directory.


The basic configuration of the route is very easy: Camel uses a special URI syntax which can contain the symbols '&','?',':','//'. Like so:


This is a simple recursive ftp polling by 5 sec time with an initial 1 min waiting configuration where, at the end of the process, the loaded file will be moved to an archive directory, adding to the file name a timestamp and .archive ending.

t.bean(dataImportService, getImportMethod("importMasterData"));

If you want to use FlatPack parser with XML pzmap definition, you can define the parser like this:

final FlatpackDataFormat fp = new FlatpackDataFormat();
fp.setDelimiter(' ');

Logging is easy between two steps using log() from Camel Context. Here, you can use simple language to use some dynamic variables.

from(routeUri).log("ftping $simple{in.header.CamelFileName}").unmarshal(fp);

Here the key method is unmarshal() which does the processing of the message via FTP.

Tracing the message flow is possible with the trace(true) from the Camel Context.

Let's say you want to parse an XML file to list of Articles where an Article is a Java bean generated with JAXB or other XML parser from XML Scheme. You can do it like this:

t = from(routeUri).split(stax(Articles.class, false));

If you want to do more processing on this route, you can do it like this:

t.bean(dataImportService, "importMasterData( *, ${headers.CamelFilePath})");

Where dataImportService is the service object name and importMasterData is the method that will be called on.

Start/Stop Camel Job

We need a DefaultCamelContext and to call start() or stop() on it.

To avoid calling this by hand, we used Google Guice, which offers GuiceCamelContext for us. A common solution to override this is by adding your Camel batches to the start/stop method. Guice can start when the application is starting and stops when the application will be destroyed.


As you can see with some basic methods, a full file processing and integration architecture can be built. Enjoy!

apache camel ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}