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

https://github.com/singhmarut/camelSample

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>”;

@Override

public void configure() throws Exception {

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

onException(Exception.class).logStackTrace(true).handled(true);

//Time interval of polling

int delay = 60; //in seconds

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

“&delay=60&consumerKey=%s&consumerSecret=%s&accessToken=%s&accessTokenSecret=%s”,

consumerKey, consumerSecret, accessToken, accessSecret);

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

from(twitterUrl)//.to(“file://home/mytweets.txt”);

//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){

System.out.println(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();

myCamelContext.addRoutes(twitterRoute);

myCamelContext.start();

System.in.read();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

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

Published by Marut Singh

Welcome to my blog. I am software architect, mentor, corporate trainer. Developing software for last 15 years in various domains..I work in different stacks and software architecture is my area of speciality..worked in c++, java, c#, scala, play vert.x, spring, nosql blah blah blah. And offcourse cloud technologies. Software Engineer cant imagine life without cloud :-) Always exploring new languages and tools to make sure that I do not loose touch, to ensure that I delivery high quality software in reasonable cost at all times. Love TDD, BDD, Agile and more than anything simplicity.. Normally I am very helpful so if you need some help do get in touch.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: