Social networking has seen explosive growth, as exemplified by networking sites such as Facebook, LinkedIn, and Twitter. Companies are beginning to set up internal social networking applications, and the IBM® Lotus® Connections product makes it easy to set up enterprise-scale social networks.
This article shows you how to use social networking software with an enterprise messaging product such as WebSphere® MQ. The three examples in this article use Twitter, but any social networking site with an API can be used. The three MQ-Twitter examples are:
- A simple queue, from which text messages are retrieved and published direct to Twitter
- Queue manager events, such as creating or deleting a queue
- A publish/subscribe example using WebSphere MQ File Transfer Edition (hereafter called WebSphere MQ FTE)
The examples in this article were developed using message-driven beans (MDBs) deployed to WebSphere Application Server Community Edition. Another approach is to use a standalone Java™ application with a message listener. The code listings below are snippets from a zip file that contains all the source files needed to run the sample. You can download the zip file at the bottom of the article.
Many Java libraries provide an interface to the Twitter API. The examples in this article use the Apache Commons HTTP libraries to communicate with it. The Twitter API is a well documented specification -- for more information, see the Twitter API wiki..
Listing 1 shows the Java method used to tweet a given message. See the downloadable zip file for the complete Java class.
Listing 1. TwitterPlugin.java: sendNotification() method
public void sendNotification(String message) {
if(message.length() > 140) {
System.err.println("Message will 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", 80, "realm"),
new UsernamePasswordCredentials(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 any connection resources used by the method
if(post != null) {
post.releaseConnection();
}
}
}
|
Using simple text messages, you can quickly turn MQ messages into Tweets. This example uses a standard MQ queue. To drive the sample, you can use any application that puts a text message onto the queue, such as the amqsput sample included with WebSphere MQ. Here is the MDB code:
Listing 2. SimpleMQEJB.java: onMessage() method
public void onMessage(Message message) {
if(message instanceof TextMessage) {
try {
/* Put your twitter username and password here */
TwitterPlugin tp = new TwitterPlugin("YOUR_TWITTER_USERNAME",
"YOUR_TWITTER_PASSWORD");
String output = ((TextMessage) message).getText();
tp.sendNotification(output);
} catch(JMSException e) {
e.printStackTrace();
}
} else {
System.err.println("This application requires" +
" a message in TextMessage format");
}
}
|
The code is simple: It takes the payload of the MQ Message, truncates it to 140 characters, and then posts it using the TwitterPlugin.java code. Figure 1 shows the example output on my mq_tweet twitter account:
Figure 1. Simple text message as a tweet

WebSphere MQ provides information about errors, warnings, and other significant actions in a queue manager through MQ Events Messaging. When configured to do so, WebSphere MQ puts a message to a specific queue, from which an application can then consume the message and perform actions based on the message contents. For more information on the types of events that WebSphere MQ see the topic Event monitoring in the WebSphere MQ information center. To enable event messages for an MQ queue manager, follow the instructions under Controlling configuration, command, and logger events in the WebSphere MQ information center.
Listing 3 shows another MDB, but this time it is configured to read from the SYSTEM.ADMIN.CONFIG.EVENT queue. This MDB takes in the message, and using simple logic, decides what message to send to Twitter:
Listing 3. MQEventMDB.java: onMessage() method
public void onMessage(Message msg) {
if(msg == null)return;
if (msg instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) msg;
try {
System.out.println("Received TextMessage: " + txtMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
if (msg instanceof BytesMessage) {
JMSBytesMessage bytesMsg = (JMSBytesMessage) msg;
int bodySize;
try {
bodySize = (int) bytesMsg.getBodyLength();
byte[] data = new byte[bodySize];
bytesMsg.readBytes(data);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInput dataInput = new DataInputStream(bais);
PCFMessage response = new PCFMessage(dataInput);
int reason = response.getReason();
int type = response.getIntParameterValue(MQConstants.MQIACF_OBJECT_TYPE);
if(type == MQConstants.MQOT_Q) {
publishQueueEvent(response, null);
} else {
System.out.println("Object Type received: " + reason);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
Because the MQ Event messages are published in PCF format, you must convert the JMSBytesMessage object into a PCFMessage object. You do this conversion by reading the bytes of the incoming message and creating a DataInput object to pass to the PCFMessage's constructor. Figure 2 is a sample of the output that is generated when a queue is created:
Figure 2. Queue creation event message as a tweet

WebSphere MQ FTE Transfer Message MDB
WebSphere MQ FTE is a new edition of WebSphere MQ that manages secure and reliable file transfers, and publishes messages that provide an audit log of the transfers. For information on the XML schema for these audit messages, see the topic Message formats in the WebSphere MQ FTE information center. Using a combination of an MDB and some JAXB code, you can enable Twittering about the start and end of file transfers, and even include some hash tags defined inside the metadata of a transfer.
Listing 4 is the WebSphere MQ FTE-specific MDB. Instead of connecting to a queue, you are connecting to an FTE topic. Metadata associated with the file transfer is appended to the status message if the metadata
name starts with the keyword Twitter. For example, Twitter.tag=SimpleTweet results in #SimpleTweet being appended to the message that is sent:
Listing 4. MQEventMDB.java: onMessage() method
public void onMessage(Message message) {
if(message instanceof TextMessage) {
try {
TwitterPlugin tp = new TwitterPlugin("YOUR_USERNAME", "YOUR_PASSWORD");
String output = ((TextMessage) message).getText();
String notification = null;
if(output.contains("TransferLog.xsd")) {
if(output.contains("started")) {
Transaction transaction = generateTransaction(output);
String transferName = getJobName(transaction);
String twitterTags = getTwitterTags(transaction);
notification = "Transfer started: " + transferName;
if(notification.length() "<" (140 - twitterTags.length())) {
notification = notification + " #ftetweet" + " " + twitterTags;
}
} else if(output.contains("completed")) {
Transaction transaction = generateTransaction(output);
String transferName = getJobName(transaction);
String twitterTags = getTwitterTags(transaction);
String transferFailed = "";
if(transaction.getStatus().getResultCode() != 0) {
transferFailed = " Transfer Failed (RC=" +
transaction.getStatus().getResultCode()+")";
}
notification = "Transfer complete: " + transferName + transferFailed;
if(notification.length() "<" (140 - twitterTags.length())) {
notification = notification + " #ftetweet" +
" " + twitterTags;
}
}
}
if(notification != null) {
tp.sendNotification(notification);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.err.println("This application requires a message in TextMessage format");
}
}
|
Figures 3 and 4 below show the tweets generated by a file transfer starting and completing:
Figure 3. WebSphere MQ FTE file transfer starting

Figure 4. WebSphere MQ FTE file transfer completing

This article has shown you how connecting WebSphere MQ queue managers or WebSphere MQ applications with Twitter is both simple to do and very effective for broadcasting status messages to interested parties. An open-source Twitter-style implementation called StatusNet (formerly called Laconica) can be deployed inside the enterprise, and uses exactly the same API as Twitter, enabling companies to quickly set up and deploy a social networking infrastructure. You can easily substitute Twitter for Status.net and start publishing information about your company's work, without the worry of confidential or sensitive information being published into the public domain.
| Description | Name | Size | Download method |
|---|---|---|---|
| Code sample | TwitterMQ.zip | 90 KB | HTTP |
Information about download methods
- Twitter API
Twitter API wiki, with Twitter documentation, downloads, forums, and other resources. - WebSphere MQ developer resources page
Technical resources to help you design, develop, and deploy messaging middleware with WebSphere MQ to integrate applications, Web services, and transactions on almost any platform. - WebSphere MQ product page
Product descriptions, product news, training information, support information, and more. - WebSphere MQ documentation library
WebSphere MQ product manuals. - WebSphere MQ V7 information center
A single Web portal to all WebSphere MQ V7 documentation, with conceptual, task, and reference information on installing, configuring, and using WebSphere MQ V7. - IBM Redbook: WebSphere MQ V7 features and enhancements
Describes the fundamental concepts and benefits of message queuing technology, describes the new features in V7, and provides a business scenario that shows those features in action. - Download a free trial version of WebSphere MQ V7.0.1
A 90-day, full featured, no-charge trial of WebSphere MQ V7.0.1. - WebSphere MQ support page
A searchable database of support problems and their solutions, plus downloads, fixes, problem tracking, and more. - WebSphere MQ public newsgroup
A non-IBM forum where you can get answers to your WebSphere MQ technical questions and share your WebSphere MQ knowledge with other users. - WebSphere MQ FTE Information Center
A single Web portal to all WebSphere MQ FTE V7 documentation, with conceptual, task, and reference information on installing, configuring, and using WebSphere MQ FTE - WebSphere MQ FTE forum
Get answers to your WebSphere MQ FTE technical questions and share your WebSphere MQ FTE knowledge with other users. - Download a free trial version of WebSphere MQ FTE V7.0.2
A 90-day, full featured, no-charge trial of WebSphere MQ FTE V7.0.2. - WebSphere MQ FTE blog on developerWorks
A forum where you can interact with the IBM team that develops WebSphere MQ FTE, including Rich Cumbers, author of this article. - Download a free trial version of WebSphere Application Server Community Edition
A 90-day, full featured, no-charge trial of WebSphere Application Server Community Edition. - developerWorks WebSphere developer resources
Technical information and resources for developers who use WebSphere products. developerWorks WebSphere provides product downloads, how-to information, support resources, and a free technical library of more than 2000 technical articles, tutorials, best practices, IBM Redbooks, and online product manuals. - developerWorks WebSphere application connectivity developer resources
How-to articles, downloads, tutorials, education, product info, and other resources to help you build WebSphere application connectivity and business integration solutions. - Most popular WebSphere trial downloads
No-charge trial downloads for key WebSphere products. - WebSphere forums
Product-specific forums where you can get answers to your technical questions and share your expertise with other WebSphere users. - developerWorks WebSphere weekly newsletter
The developerWorks newsletter gives you the latest articles and information only on those topics that interest you. In addition to WebSphere, you can select from Java, Linux, Open source, Rational, SOA, Web services, and other topics. Subscribe now and design your custom mailing. - WebSphere-related books from IBM Press
Convenient online ordering through Barnes & Noble. - WebSphere-related events
Conferences, trade shows, Webcasts, and other events around the world of interest to WebSphere developers. - developerWorks blogs
Join a conversation with developerWorks users and authors, and IBM editors and developers. - developerWorks Webcasts
Free technical sessions by IBM experts that can accelerate your learning curve and help you succeed in your most difficult software projects. Sessions range from one-hour Webcasts to half-day and full-day live sessions in cities worldwide. - developerWorks on Twitter
Check out recent Twitter messages and URLs.

Rich Cumbers is a Software Developer on the WebSphere MQ File Transfer Edition (FTE) team at the IBM Hursley Software Lab in the UK. He has worked at IBM for four years on both WebSphere MQ and WebSphere MQ FTE. His career to date has spanned most software development disciplines including development, test, and performance. You can contact Rich at rich.cumbers@uk.ibm.com.




