I have started getting my hands wet with Apache Camel and since it was for work, I had to jump right into the sea and collect as many shells as possible. This is a reference for myself and for people who are struggling with Camel. If anybody could throw some valuable comments, that will be much appreciated. After all sharing is gaining.
So this is the bare basics of Apache camel and I would like to post all the details about getting started on Camel.
Camel as the documentation says, empowers the developers to define the routing and mediation rules. For a newbie like myself, the first few words in the sentence confuse me enough to back out. Camel is simply a tool to define the routes. Routes,meaning your source and destination definitions. For now lets keep this simple and dive right in. ( I am sure Camel is much more than this)
Reading the sample of Camel in Action Chapter 1 helped a lot but I have to say Camel is much more simpler than that.
I am using Spring so will be using Spring DSL configuration.
Let us imagine a situation where you have to process some files and write the output into another file. Using normal Java, we would have to handle the file procurement, closing open of source and destination files etc. But in camel, you can just configure it in a couple of seconds.
I will not go into the details of installing Camel and setting it up in this blog ( may be later) but assuming you have done all that, all you need is a xml configuration.Let me first show the code and explain you step by step.
(Readers. Please pardon me. XML tagging is not functioning properly in wordpress. Please dont mind the color coding too.Will figure out soon. Meanwhile….)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:camel=”http://camel.apache.org/schema/spring”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd”>
<bean class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”/>
<camelContext xmlns=”http://camel.apache.org/schema/spring”><camel:route>
<camel:from uri=”file:src/data?noop=true”/>
<camel:choice>
<camel:when>
<camel:xpath>/person/city = ‘London'</camel:xpath>
<camel:log message=”UK message”/>
<camel:to uri=”file:target/messages/uk”/>
</camel:when>
<camel:otherwise>
<camel:log message=”Other message”/>
<camel:to uri=”file:target/messages/others”/>
</camel:otherwise>
</camel:choice>
</camel:route>
</camelContext>
</beans>
So as you can see, above is the xml configuration of camel. Camel (not only in xml but in any other DSL) works on basis of a context. This context is called Camel Context.
Since we are dealing with Spring in this example, the xml began with the bean definitions for spring and camel.
As I mentioned earlier, Camel deals with routing. ( and hence the tag <camel:route>).
The problem I am dealing with is some kind of a file processing. So to let camel know that our starting point is a file we said.
<camel:from uri=”file:src/data?noop=true”/>
from – This denotes the starting point
uri – The from function will start from the defined uri.
file – what is that we are processing. File is the type. Yes Camel has several such definitions where you just mention the keyword and the clever camel will know what to do.
: – colon is the delimiter for the keyword from. From this point on we will mention the path.
src/data – the file path
? – Delimiter for the conditions just like in a url.
noop=true – This is an camel option. Which here means, do not delete after reading the file.
The above line meant, “Oh clever camel, please read a file from src/data location and dont delete it after reading it”.
<camel:choice> – conditional statement prefix. To mention a conditional statement, we have mention this prefix.
<camel:when> – conditional statement.(if)
<camel:otherwise. – conditional statement.(else)
<camel:xpath> – camel supports xpath ( XML Path Language). Don’t be intimidated by this. The line <camel:xpath>/person/city = ‘London'</camel:xpath> means, in a person tag if the city tag value is ‘London’
<person ....(blah blah)>
<tag1> (blah blah) </tag1>
<tag2> (blah blah)</tag2>
<tag3> (blah blah)</tag3>
<city>London</city>
<tagY> (blah blah)<tagY>
<tagX> (blah blah)</tagX>
<tagN> (blah blah)</tagN>
</person>
If such a condition exists, then process this <camel:log message="UK message"/> (which means, log the message, where and how depends on your logger settings. You can use log4j. Just set log4j properties and it will automatically wire it) and this
<camel:to uri="file:target/messages/uk"/> (this means , move the file to target/messages/uk path).
Very simple. Huh! Yes it is. Just be defining an xml file with a source folder and a target folder and a very simple condition, the files are processed and moved into respective locations. No file handling mess and no boiler plate coding.
A few things to notice is,
- Camel also processes line by line.
- Camel has no boiler plate code whatsoever. But if you still prefer and are extremely comfortable in other languages like Java(like me), OGNL and many others, you can easily extend this amazing functionality in language of your choice. (.NETters, I think… I think camel doesnt work for you but if I am wrong, I am happy for you. )
- There is quite a bit of syntax to learn, probably not a bad thing at all, since google tells us everything.
- Documentation is poor. As camel can extend in several languages, there is no proper documentation for any language. I am struggling myself and praying my boss gives me ample time when he gives me work on Camel but I should confess, I am loving this power. Feels like riding a S1000RR.
So this is the first post people. I will probably come up with setting up camel pretty soon but before that post hits up, I am gonna write other camel examples as my work demands. Happy cameling.
Camel noobs and experts. Please comment and share your thoughts and camel tips.