Analyzing twitter using JBossESB

23 Mar, 2012
Xebia Background Header Wave

Most ESB example start from some HelloWorld use case, where you have to send a message to the ESB and it will print it to the console. In this post I would like to show that you can do a lot more with an ESB, for example reading and analyzing data from social media like twitter.

The example below will demonstrate how you can use JBossESB to retweet every message containing a certain hashtag. If you want you can extend the example and store all the messages to database, or keep track of those users who have posted the most amount of messages with a certain content.


Step 1) Create a scheduler using JBossESB
When you want to retweet all messages containing a certain hashtag, you need some kind of scheduler that is fired every second, minute or hour. In the jboss-esb.xml file you can do this by using a ‘schedule-provider’.

    <schedule-provider name="schedule">
                <cron-schedule scheduleid="cron-trigger" cronExpression="0 0 * * * ?" />

This cron-trigger can be used in a schedule-listener to invoke a certain action or chain of actions which is explained in the next step. The cron-expression above will be fired every hour.

Step 2) Create a listener that creates an ESB aware message
Each time the above trigger is fired, a listener should be triggered that can catch the event and process it. In this example the event processor will create a new ESB aware message and put the hashtag in it to look for.


   <scheduled-listener name="cron-schedule-listener"
      event-processor="org.xebia.soa.esb.twitter.TwitterConfigurationReader" />


public Message composeMessage() throws SchedulingException {
      // TODO This should be read from a configuration file or database.
      Message message = MessageFactory.getInstance().getMessage();
      return message;

In this case we put “Xebia” as the hashtag to look for. If you want you can configure some property file for example that contains all the hashtags to look for.

Step 3) Create an action that searches for the tweets and reposts them if needed
Next step is to create a service that contains an action that will search for all tweets containing the string that was put in the message body in step 2 and retweets those messages if they are new.


   <service category="ServiceSocialMedia" name="TwitterService" description="Fetching Twitter Messages">
         <scheduled-listener name="cron-schedule-listener"
            event-processor="org.xebia.soa.esb.twitter.TwitterConfigurationReader" />
         <action name="action" class="org.xebia.soa.esb.twitter.ConverterAction" />


Twitter twitter = new TwitterFactory(;<br />
// Query latest tweets of current user
Query queryTweeted = new Query("from:" + USER_NAME);
QueryResult tweeted =;<br />
// Execute search based on hashtag provided in message
String qrySearch = "#" + message.getBody().get();
Query query = new Query(qrySearch);
QueryResult result =;<br />
// Add number of results to ESB message for further processing
message.getBody().add("tweets", result.getTweets().size());<br />
for (Tweet tweet : result.getTweets()) {
   // retweet it
}<br />
return message;

For the configurationBuilder you will have to create the OAuth Access token yourself, by registering your application on the twitter website.
First we query for the last tweeted message by the current user. The id of that tweet will be used in the next query to fetch only those tweets with a higher id.
For each tweet found, we retweet that by passing the original id. Notice that we only send the id, and not the whole message.

Step 4) Deploy and have fun
You can use maven to create a jar file out of it and deploy this to the JBoss AS. Once deployed this application will start retweeting all tweets containing #Xebia to your own twitter account.

Typical ESB provider these days support connectors for webservices, file systems, JMS, ftp, … The next step would be to create connectors with social media like Twitter, Facebook, …
Although you can do almost everything with JBoss ESB what you want, a big improvement would be to have a more user friendly designer tool. Currently you quickly have to fall back on hard coding xml files, which can be difficult. It would be nice to be able to rewrite this whole blog post, by using for example only drag and drop components 😉
Possible extensions: Of course you can add additional actions that analyze the contents of the messages.

  • Only tweeting positive messages
  • Change messages before retweeting. Example, always add #like
  • Forward tweets to other social media
  • Organize a game where you keep track of who has send the most tweets
  • Convert all tweets to email
  • Anything else you would like to do…

All code snippets can be found in this zip file
Joris De Winne recently moved from Belgium to the Netherlands, and is now working for Xebia as an IT architect, with a lot of experience in open source and Agile/Scrum consultancy.

IT Architect @ Xebia

Get in touch with us to learn more about the subject and related solutions

Explore related posts