Camel Powers

Luis Trigueiros
4 min readMar 13, 2022

--

Some tools are just worth the time you put into mastering then in gold.

Let us imagine this hipotetical scenario, you have just came back of a lovely break and you boss comes to you with an urgent request, and he says:

You know that release we did last week, the business his happy and now are getting more responsability, we have to sunset this other problematic vendor, so we need a job to clear down a mailbox, we need to deliver this in 3 days”.

So in a scenario like this it is good to have a portfolio of tools that you know and trust that can help you get the job done quickly, with a high degree of quality, in the required amount of time, that you have to implement the new business requirement.

In today’s world writing business software tipicaly means to pipe data information flows, having mastery of a lightweight system integration framework is an incrediable powerfull asset to have, and this what Apache Camel is.

Camel with Jet engine

It is not possible to make justice enumerating all the things that Apache Camel can help you getting done in a short blog, for a better understanding I recomend reading the Apache Camel in Action book, so I will only enumerate where a few that I find particulary relevant in this context.

Apache Camel gives you access to more that 500+ components, protocols, dataformats and the list of integrations is constantly growing.

Architecture diagram of Apache Camel

The above business requirement is delivered with the following bit of code

The above class is what is called in Apache Camel a RouteBuilder and the important parts from line 21 to 26, literaly 4 lines of code where we define the message processing flow that it is supposed to be performed and by the way lines 23 and 24 are not strictly required it is there just for logging pourpose to in fact it is only 2 lines of code, amazing is it it you may ask how it is possible there must be something missing how do we define what mailbox to read from and that up on reading an email it must be deleted. Well ok you are right we need the following additional snippet of configuration application configuration yml:

At that is it, wait only this ?! We don’t have to deal with opening newtork socket connection or managing threads, in reality all of this code still exists but we don’t have to deal with it directly when using Apache Camel, this is handle by the de Mail component, the url’s that you see defined in the configuration snippet drive Apache Camel to instanciate the one email consumer endpoint that will periodicaly poll the given mailbox and up on new email arriving it will consume this message deleting it in the the process.

Bellow is the the unit testing code that asserts correct implementation of this business requirement.

For the convinience of the blog I left out the bit of code that retries on network connection errors, additionaly the example test code is written using a mock of the Java Mail API, this allow the convinience of testing all this code not having to talk to a real email server.

The code example for this blog can be found in my GitHub at:

--

--