Monday, March 10, 2014

Apache Camel tutorial : Quick start from scratch (Console App , without Spring)

Apache Camel is one great implementation of the EIP pattern, If you are not a seasoned Spring/Java developer , you might found it’s hard to get started, the goal of this post is simplify that process and show you how easy to get started without remember any code snippet. we will use plain console app to host and start one camel route.

to get started, simply open eclipse and create one maven project (the purpose of maven here is to add those dependency automatically), if you don’t have any maven experience, you can simply download the required jar and add to you build path.

Create one Maven project , using the simple project type, we get one project with maven ready,
image

Create one simple class with main method,
image

Now , right click pom.xml, click maven menu,  add a dependency to camel-core, you can simply search camel-core

image

Now, we can start coding,
image

we’ve initialized the camelcontext, next step is adding our routes by xml or java code.

let’s start from java code first, since it has intelligence and life is little bit easier,

image

it looks like we can just add one RoutesBuilder to the context, is there any existing one, or we have to build our own.
ask the eclipse create a local variable for the routesbuilder, let’s see whether there is any existing one,

image

it turns out Routesbuilder is a interface, and have RoutesBuilder as an implementation, so let’s extends that one,
image

then the code looks like this,

image

in the configure , we just inject the code snippets listed on the camel component page, here is one demo, that dump out some message every 2 seconds,

image

if we run this, we can see the message get shown in the console every 2 seconds.

for the log component, we can’t see the message because it is NOP for slf4j by default, so lets add a slf4j-simple to the pom.xml

image

run again, you can see the message is listed in the console window.
image

if we want to export this as a executable jar, go to the pom.xml ,right click maven menu to add a mvn-assemly-plugin
image

Change the pom.xml a little big , pint to the mainclass , you can copy the xml here
image

then go to the project folder, run mvn compile assemlby : Single

image

you will see a jar in the target folder,
image

Now, we can run the jar directly, by typing java –jar pathtothejar

image

image

for the source code, check it out here, https://github.com/ryandh/apacheCamelTutorial/tree/DemoConsole

1 comment:

Ryan said...

log.getRootLogger().setLevel=Debug to dump out all the interactions mesages

 
Locations of visitors to this page