Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Learn more about how to Prevent Slow or Broken APIs From Affecting Your Bottom Line.

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.

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-guice</artifactId>
    <version>2.17.7</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-ftp</artifactId>
    <version>2.17.7</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-stax</artifactId>
    <version>2.17.7</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-flatpack</artifactId>
    <version>2.17.7</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-csv</artifactId>
    <version>2.17.7</version>
</dependency>

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 {

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

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

Task

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.

Implementation

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

from(
"ftp://localhost/?antExclude=**%2FArchive%2F*
&antInclude=**%2Fprices*.txt
&binary=true
&consumer.delay=5000
&consumer.initialDelay=60000
&move=%24%7Bfile%3Aparent%7D%2FArchive%2F%24%7Bfile%3A
onlyname.noext%7D_%24%7Bdate%3Anow%3AyyyyMMddHHmm%7D.%24%7Bfile%3Aext%7D.archive
&password=xxxxxx
&recursive=true
&username=scp"
).to("seda:route-1?multipleConsumers=true");

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.setDefinition("classpath:/assortment.pzmap.xml");
fp.setDelimiter(' ');
fp.setFixed(false);
fp.setAllowShortLines(true);
fp.setIgnoreExtraColumns(true);
fp.setIgnoreFirstRecord(false);
fp.setParserFactory(AdvancedDefaultParserFactory.getInstance());

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.

Conclusion

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

Learn about the Five Steps to API Monitoring Success with Runscope

Topics:
apache camel ,integration

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}