Apache Camel Sample Project

In this very simple project we will show how camel actually works. We will try to download tweets from our own account and print them in Console.

We can write them to a file if we like. We will also show how polling works. I have created a sample maven project which you can download here


Camel’s fundamental unit is route. You define routes and once correctly configured they start doing their work of integration.

A route can only exist between two end points. So what we want to do here is download tweets from “Twitter Account” and “show them in console”. We need a route which actually does this.

So we define a route by extending RouteBuilder class

package org.marut.camel;

import org.apache.camel.builder.RouteBuilder;

//Create a route in Camel Context. A route is a new thread. A Java DSL defines route

//Our objective is to create a route which reads tweets from your twitter account every 1 minute interval and

//sends them to console

public class TwitterRoute extends RouteBuilder {

static final String consumerKey  = “<Your Key>”;

static final String consumerSecret = “<Your secret>”;

static final String accessToken =”<Your access token>”;

static final String accessSecret =”<Your access secret>”;


public void configure() throws Exception {

//Camel’s Java DSL to define a global Exception handler..


//Time interval of polling

int delay = 60; //in seconds

String twitterUrl = String.format(“twitter://timeline/home?type=polling” +


consumerKey, consumerSecret, accessToken, accessSecret);

//You can redirect tweets to file or to a bean


//Bean instantition done by Type, Specify method name which gets called when

.bean(TwitterStore.class, “storeTweet”);




Here is TwitterStore class which actually prints message on console. Needless to say you can have your own implementation here.

public class TwitterStore {


public void storeTweet(String tweet){





That’s it. Once your route is defined you need to register it with Camel Context and start Camel Context


TwitterRoute twitterRoute = new TwitterRoute();

try {

CamelContext myCamelContext = new DefaultCamelContext();




} catch (Exception e) {

// TODO Auto-generated catch block



Above code goes into main.

You can download maven project from above mentioned link. I have hosted project on github. Just drop me a comment if you need more help. In future we will see more sample projects on apache camel

Apache Camel Sample Project

Apache Camel – Why you should learn?

Recently I encountered a problem related to integration with some third party APIs. These days it is pretty common to outsource some activity for a company, integrating with social networking website, integration with third party tools installed on company premises.

These tools can be built on various technologies and integration between different component becomes an issue when people start writing code for individual component and do not target integration as one cohesive process.

Result is cluttered tightly coupled development based on many assumptions. Necessity is to take a comprehensive uniform approach to tackle this problem.

Various open source ESB and commercial solutions are available for this but in my experience I have found that using an ESB right away is a difficult thing for developers to accept as every ESB faces a certain style of development.

So approach should be slowly introduce the concept involved in an ESB during development process.

One such core ingredient in ESB is called  Enterprise Integration Patterns into core modules and let people learn the concept and admire them.

Enterprise Integration Patterns (EIP) are patterns just like GOF patterns but context is “integration” between disparate systems.

These patterns are well documented in the book by Gregor Hohpe and Bobby Woolf.

If you want to have a quick look at what these patterns are then go to http://camel.apache.org/enterprise-integration-patterns.html

Apache Camel is an out of the box solution which implements all patterns described in EIP and provides integration with many more components which can help developers by avoiding pain of writing boilerplate code. It also gives many useful implementations which will bring a consistency in your large code base that everyone will be able to easily understand.

Patterns are a proven way of building robust scalable and easy maintenance software and following them religiously can make a developer’s life easier and make organization save huge amount of money in long term.

Camel not only provides implementation of these EIP but more importantly it’s syntax is so fluent that it brings a paradigm shift the way one writes integration code.

It is an important framework to learn for a Java developer and I do not find any direct alternative in Microsoft world.

Just to give you an idea consider a simple scenario. You want to integrate to a third party API like a social networking, Courier Service, Text to Speech service or something else. It may so happen that while calling this API network glitch occurs and your call does not succeed. What do you want to do?

You guessed it right..All we can do is retry. So probably you will write a while loop which will check the HTTP status of this API and then if it’s not 200 then just retry. OK you can do that but problem is very common to be faced by every developer in your team so all of them are writing same type of code.

If you are a well managed team then you will probably create a shared library and publish it to your local maven repo and inform about it.

Great..you did that. Let’s add another twist to the tale..You want to retry but after some time. What do you do? Put sleep somewhere in your while loop. Again enhance your library and roll out a new version. Over the period you will come up with many such problems and end up writing same code which is there in camel (only better implemented).

Camel on the other hand provides a nice fluent interface which promotes consistent coding style across the board and you have a tool in your hand which solves lot of your problems out of the box.

In the next article we will try to develop a small application for building a fault tolerant application using Apache Camel

Apache Camel – Why you should learn?