Apache Camel

Paw Dybdahl
Destination AARhus-TechBlog
6 min readOct 21, 2020

Welcome to Apache Camel

A non-intrusive library is for everyone who wants to implement any kind of integration the easy way. When using Camel “integration”, it covers connecting (almost) any kind of source with (almost) any kind of sink — with the opportunity to perform custom processing and transformation of data in between.

In Camel lingua a ‘Route’ is the connection between a source and a sink, and Camel sends messages through the Route in an ‘Exchange’ object.

Besides being a concrete library, Apache Camel is also a framework (or platform), where the open source community can provide components which enables new sources or sinks as Camel endpoints. At the time of writing, Camel consists of 354 components which you can use as connectors to the outside world.

In this blog post, I will show you some simple examples using Camel to integrate common endpoints such as:

·Web services

· Files

· Databases

and hope this will inspire you to investigate Camel further and encourage you to use Camel in your own projects.

At the end of the blog post, I conclude by giving directions to where to go from here.

Hello Camel

Let’s get started by implementing a simple “hello world” example in Camel. This can be done using a lot of different endpoints, but I’ve chosen to expose a rest service with a get-endpoint which says hello:

The route is started with the following method:

where Main is Camel’s helper class for running Camel as a standalone application.

Pointing our browser to the endpoint, Camel says hello:

Consuming rest web services

Now that we have a web service in place, the next example will show how to consume from this web service and save the result to a file.

The file is located here:

and contains the text:

The portion:

is only used to trigger the call to the web service, the real work (and power) lies in these two lines:

where we call a web service and write the result to a file — in only two lines of code!

The two routes are started with the following method:

From file to database

Having the super-secret message from our hello world endpoint safely stored in a file, it would be even better if the message was written to a database — so let’s do that!

The file endpoint can watch a directory for arrival of new files and the jdbc endpoint can execute statements against any database, so within a few lines of code, Camel lets us automatically streaming the content of files into a database:

The line:

watches the directory ‘data/input’ for arrival of new files and starts the routing of a message, which contains the content of the file as its body. In this example, the file is deleted when read.

The line:

defines a jdbc endpoint which accepts messages with SQL-statements as their body.

With the source and the sink in place, we only need something in between, that transforms the content of the file to a SQL-statement:

In the above snippet, the body of the message is transformed from the content of the file (a String) to an insert-statement — which goes into the sink (a.k.a. the jdbc-endpoint).

The example can be run with this class:

which defines all routes, setup a database and binds a data source to the registry (a requirement for the jdbc-endpoint).

Expose the content of the database as a rest service

As a final example, I will show how simple it is to expose the content of a database table as a REST service:

In three lines of code, we have setup a rest endpoint which reads the content of the hello_world table and returns the result as a json-object:

The example can be executed using this method:

Getting serious

Having covered the most basic features of Camel, it’s time to highlight two other features which are essential for any serious piece of software: error handling and logging.

Handling errors

Camel comes with its own framework for handling errors, which you can use to define exactly how you want to handle the different kinds of errors that likely (or unlikely) to occur, when a message is passed through its route.

The framework contains five general purpose error handlers to pick from in your route configuration:

· DefaultErrorHandler

· DeadLetterChannel

· TransactionErrorHandler

· NoErrorHandler

· LoggingErrorHandler

Besides these error handlers, Camel supports the use of exception policies, which allows you to define what action should be taken when a specific exception occurs. For example. when having a route that reads from an external web service, we might get a SocketTimeoutException and choose to handle this exception with the following policy:

Logging

Monitoring your software in use its (almost) all about logging, and Camel includes four different kinds of logging services:

· core logs

· custom logging

· Tracer

· notifications

Using custom logging can be as easy as adding the following line to a route:

which will log the actual content of the exchange and for the above ‘FromFileToDbRoute’, this will result in the following log line:

Next steps

Camel Quarkus — cloud native Camel

The Quarkus project describes itself as A Kubernetes Native Java stack tailored for OpenJDK HotSpot and GraalVM, crafted from the best of breed Java libraries and standards, and (of course) Camel is one of these Java libraries. You should definitely check this project out and see the enormous effect of going native — measured in memory footprint and boot time.

Further readings

Whether you are new to Camel or you are an experienced user, the book ‘Camel in Action’ is a must read.

The home of Camel (camel.apache.org) hosts a lot of useful guides and documentation — including a complete list of components and how to use them.

By Paw Dybdahl
Backend Developer, SOS International

About me

In our team at SOS International, we spent most of hte time integrating internal and external systems and services — and to this end Camel is the most important piece of software in our daily work.

Back in 1987, I finished a master’s in Mathematics and Economics from Aarhus University, and I have been continuously written code (in more or less suited languages) ever since.

In my spare time, I do a lot of climbing and try very hard to solve “problems” at Aarhus Boulders.

--

--