Blog

Apache Camel

Mika Laitinen Integration Specialist, Solita

Published 09 Feb 2021

Reading time 5 min

Apache Camel is an open source integration framework. It provides concrete implementation building blocks for Enterprise Integration Patterns (EIPs) along with about 300 components for automating and easing the integration to external systems. It handles the message routing, type conversion, and background logic so that the integration developer can focus on the integration.

Quite often Camel is regarded to be something that it is not; an Enterprise Service Bus (ESB)*. Instead of being one it actually provides the integration capabilities to a number of ESBs like ServiceMix, Red Hat Fuse, and Talend ESB.

Doing integrations with Camel doesn’t require you to install one of those ESBs as Camel is quite flexible. Camel enables you to create and run Camel integrations independently, as a Java application (packaged or not) or in a Docker container. Or you can run Camel integrations in Kubernetes with Camel K, extend Apache Kafka with Camel Kafka Connector, or turn Camel integration into native code with Quarkus and Camel Quarkus for fast boot times and reduced memory footprint.

OpenHub lists Camel to have had its first commit in 2007 and after that over 50 000 commits, over 7800 for the last 12 months, and the latest being within a day (as of writing). And since it powers a number of ESBs the present and future looks really solid.

With its flexibility and freedom Camel is a perfect integration framework where you would want a lot of room for customisation or where ESB would be too big, or too small.

Easy start

Despite Camel lacking a fancy UI** where you can drag and drop integration components it is very approachable for a developer with some knowledge of Java or XML if Spring XML is preferred.

Let’s start with a practical example on how to get started with Camel as a standalone Java application. To do this, you need:

We will use the latest (as of writing) Camel version, 3.7.2, and a Maven archetype from Camel to create the template example project.

mvn archetype:generate \

-DarchetypeGroupId=org.apache.camel.archetypes \
-DarchetypeArtifactId=camel-archetype-java \
-DarchetypeVersion=3.7.2 \
-DgroupId=fi.solita.blog \
-DartifactId=integrations-with-camel \
-Dversion=1.0.0 \
-DinteractiveMode=false

This generates a basic Camel Java project with simple MainApp.java and MyRouteBuilder.java with an example route. The MainApp.java is responsible for configuring, starting and running the Camel routes. MyRouteBuilder.java contains the Camel integration routes, currently one, that do the actual work of the integration.

The archetype contains an example integration route*** that reads files from the project folder, routes them based on the content of the file and writes them out. The ‘from’ works as a consumer that uses the file -component to consume the files, the ‘choice’ -component with ‘when’ and ‘otherwise’ is our if-else, the ‘log’ -component does the logging and finally ‘to’ being our producer to store the files in defined output folders.

// here is a sample which processes the input files
// (leaving them in place – see the ‘noop’ flag)
// then performs content based routing on the message
using XPath
from(“file:src/data?noop=true”)
.choice()
.when(xpath(“/person/city = ‘London'”))
.log(“UK message”)
.to(“file:target/messages/uk”)
.otherwise()
.log(“Other message”)
.to(“file:target/messages/others”);

The example route takes advantage of one of the many components Camel provides; the file -component. And it solves one of the integration problems, how to filter files based on their content, with Content Based Router -EIP.

Content Based Router EIP

To run the sample we execute this in the integrations-with-camel -folder where the pom.xml is.

The command will tell maven to clean any generated files, compile the project, and finally run it within the console. To stop the integration use Control+C key combination.

mvn clean compile camel:run

From the logs you can see the router doing its thing by the log messages for the two files copied.

[1) thread #1 – file://src/data] route1 INFO Other message

[1) thread #1 – file://src/data] route1 INFO UK message

Rabbit hole

As you can see above, starting an integration project and experimenting with Camel is rather simple.

But to really understand Camel and take advantage of its capabilities we need to do some learning. Camel has extensive features for properties handling, testing, and route templates, to name a few that we can’t cover all in one blog post.

If you are looking for a more detailed introduction check out Camel’s own documentation.

Also there are few books, also mentioned in the Camel documentation, that are a good read.

Enterprise Integration Patterns provide solution patterns to most integration problems. Camel has implemented almost all of the EIPs defined as Camel based its functionality on them. I would recommend you to check them out first.

Camel in Action, 2nd edition book written by core developers of Camel.

* “Camel isn’t an enterprise service bus (ESB), although some call Camel a lightweight ESB because of its support for routing, transformation, orchestration, monitoring, and so forth.”

Excerpt From: “Camel in Action, 2nd edition”.

** Some ESB providers that use Camel in their product have developed their own UIs for defining Camel routes.

*** Some terminology might be in order:

  • Route: definition of integration steps for the integration; start – input, possible processing, and end – output.
  • Consumer: a service that receives external data or trigger, for example file -component acts as a consumer when it reads a file
  • Producer: a service that sends data to external systems, again as an example the same file -component acts as a producer when it writes a file.
  1. Tech