woensdag 21 maart 2012

Analyzing twitter using JBossESB

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 an store all the message to database, or keep track of those users who have posted the most amount of messages with a certain content.

Prerequisites

Step 1) Create a scheduler using JBossESB
When you want to retweet all messages containing a certain hasshtag, 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'.
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.


xml-configuration

event-processor
In this case we put "Xebia" as the hashtag to look for. If you want you can configure some property file for example that contain 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.


xml-configuration
action


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.

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.

Geen opmerkingen: