Analyzing twitter using JBossESB

jdewinne

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.

Prerequisites

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'.


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

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


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

event-processor

public Message composeMessage() throws SchedulingException {
      // TODO This should be read from a configuration file or database.
      Message message = MessageFactory.getInstance().getMessage();
      message.getBody().add("Xebia");
      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.

xml-configuration

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

action

Twitter twitter = new TwitterFactory(configurationBuilder.build()).getInstance();
// Query latest tweets of current user
Query queryTweeted = new Query("from:" + USER_NAME);
QueryResult tweeted = twitter.search(queryTweeted);
// Execute search based on hashtag provided in message
String qrySearch = "#" + message.getBody().get();
Query query = new Query(qrySearch);
query.setSinceId(tweeted.getMaxId());
QueryResult result = twitter.search(query);
// Add number of results to ESB message for further processing
message.getBody().add("tweets", result.getTweets().size());
for (Tweet tweet : result.getTweets()) {
   // retweet it
   twitter.retweetStatus(tweet.getId());
}
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.

Conclusion
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.

Comments (0)

    Add a Comment