Today, social networks are ubiquitous-to connect with friends, to keep up with the times, or to keep people informed of the latest developments in topics of common interest. Social networking is also useful in businesses. This article will show you how to quickly and easily use social networking software (such as Twitter) in your WebSphere MQ application to send status and problem information to a wide range of system administrators or end users, or even to other applications or middleware. The examples in this article use the WebSphere MQ and Twitter APIs for the JEE technology (simple message-driven bean) for the WebSphere application Server Community Edition runtime.
Brief introduction
Social networks have exploded, and the following are the sites of social networks: Facebook, LinkedIn and Twitter. Companies are now starting to build internal social networks, and IBM's ®lotus®connections products make it easy to build social networks of corporate size.
This article will show you how to use social networking software in an enterprise messaging product, such as WEBSPHERE®MQ. The three examples in this article use Twitter, but you can also use other social networking sites with APIs. Examples of these three mq-twitter are:
A simple queue in which text messages are retrieved from here and posted directly to Twitter
Queue Manager events, such as creating or deleting queues
An example of a publish/subscribe using WebSphere MQ File Transfer Edition (later referred to as WebSphere MQ FTE)
The example in this article was developed with a message-driven bean (MDB) deployed to the WebSphere application Server Community Edition. Another way is to use a stand-alone Java™ application with a message listener. The code listings that are described below are intercepted from a ZIP file that includes all the source files needed to run this example.
Twitter API
Many Java libraries provide an interface to the Twitter API. The example in this article uses the Apache Commons HTTP Library to communicate with the Twitter API. The Twitter API is a great specification-see the Twitter API Wiki for more information.
The Java method shown in Listing 1 can be used to tweet a given message. To obtain a complete Java class, download and refer to the zip file above.
Listing 1. TwitterPlugin.java:sendNotification () method
public void sendnotification (String message) {
if (message.length () > 140) {
System.err.pri NTLN ("message would be truncated from:"
+ message + "to:" + message.substring (0, 140));
Postmethod post = null;
try {
HttpClient client = new HttpClient ();
Client.getparams (). Setauthenticationpreemptive (True);
Client.getstate (). SetCredentials (New Authscope ("Twitter.com", "" Realm "),
New usernamepasswordcredent IALs (GetUserName (), GetPassword ()));
Post = new Postmethod ("Http://twitter.com/statuses/update.xml");
Post.setquerystring (Uriutil.encodequery ("status=" +message));
Post.setdoauthentication (TRUE);
//execute the get
int status = Client.executemethod (post);
Print the status and Response
System.out.println ("Status:" + status);
} catch (Uriexception e) {
System.err.println (e.getmessage ());
} CatCH (httpexception e) {
System.err.println (e.getmessage ());
catch (IOException e) {
System.err.println (e.getmessage ());
finally {
//release no connection used by the method
if (post!= null) {
Post.re Leaseconnection ();
}
}
}