转自http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1003_cumbers/1003_cumbers.html
社交网络已经取得了爆炸式的发展,下面所列就是一些社交网络的站点:Facebook、LinkedIn 和 Twitter。各企业现在也开始着手建立内部的社交网络,而 IBM 的 ® Lotus® Connections 产品可以让我们很方便地建立企业规模的社交网络。
本文将向您展示如何在一个企业消息产品,比如 WebSphere® MQ 内使用社交网络软件。本文中的三个示例使用的是 Twitter,但也可以使用其他的具有 API 的社交网络站点。这三个 MQ-Twitter 的例子是:
- 一个简单的队列,文本消息将从这里被检索并直接发布给 Twitter
- 队列管理器事件,例如创建或删除队列
- 一个使用了 WebSphere MQ File Transfer Edition(后面简称为 WebSphere MQ FTE)的发布/订阅示例
本文的示例是用部署到 WebSphere Application Server Community Edition 的消息驱动 bean (MDB) 开发的。另一种方式是使用一个具有消息侦听器的独立 Java™ 应用程序。下面将要介绍的这些代码清单均截取自一个 zip 文件,这个 zip 文件包括了运行这个示例所需的所有源文件。您可以在本文的末尾 下载这个 zip 文件。
很多 Java 库都提供一个到 Twitter API 的接口。本文中的示例使用 Apache Commons HTTP 库与 Twitter API 通信。Twitter API 是一个很好的规范 —— 要了解更多信息,请参见 Twitter API wiki。
清单 1 内显示的这个 Java 方法可用来 tweet 一个给定消息。要获得完整的 Java 类,请下载并参考上述的 zip 文件。
清单 1. TwitterPlugin.java: sendNotification() 方法
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();
}
}
}
|
使用简单的文本消息,可以快速地将 MQ 消息转换为 Tweet。本例使用了一个标准的 MQ 队列。要想驱动这个示例,可以使用任何能将一个文本消息放到队列上的应用程序,比如随 WebSphere MQ 所带的 amqsput 示例。以下是 MDB 代码:
清单 2. SimpleMQEJB.java: onMessage() 方法
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 utput = ((TextMessage) message).getText();
tp.sendNotification(output);
} catch(JMSException e) {
e.printStackTrace();
}
} else {
System.err.println("This application requires" +
" a message in TextMessage format");
}
}
|
上述代码很简单:它获取 MQ Message 的有效负载,将它截短到 140 个字符,然后用 TwitterPlugin.java 代码张贴它。图 1 显示了此示例在我的 mq_tweet twitter 帐户上的输出:
图 1. 简单的文本消息作为一个 tweet

WebSphere MQ 通过 MQ Events Messaging 提供了一个队列管理器内的错误、警告和其他重大动作的相关信息。在配置了以上功能后,WebSphere MQ 会将一个消息放到一个特定队列上,然后应用程序可以从这个队列中使用这个消息并根据这个消息的内容执行动作。有关 WebSphere MQ 监视的事件类型的更多信息,请参见 WebSphere MQ 信息中心中的 Event monitoring。若要为一个 MQ 队列管理器启用事件消息,请遵循 WebSphere MQ 信息中心中的 Controlling configuration, command, and logger events 中的指导。
清单 3 显示了另一个 MDB,但这次它被配置为从 SYSTEM.ADMIN.CONFIG.EVENT 队列中读取消息。这个 MDB 获取消息并用简单的逻辑决定将什么消息发送给 Twitter:
清单 3. MQEventMDB.java: onMessage() 方法
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();
}
}
}
|
由于 MQ Event 消息是以 PCF 格式发布的,所以必须要将 JMSBytesMessage 对象转换成 PCFMessage 对象。可以按照以下的步骤来完成这个转换:读取入向消息的字节并创建一个 DataInput 对象,然后将它传递给 PCFMessage 的构造函数。图 2 中所示的是当创建一个队列时所生成的一个输出示例:
图 2. 队列创建事件消息作为一个 tweet

WebSphere MQ FTE Transfer Message MDB
WebSphere MQ FTE 是 WebSphere MQ 的一个新版本,它可以设法实现安全可靠的文件传输,并能发布关于传输审计日志的消息。有关这些审计消息的 XML 模式的信息,请参见 WebSphere MQ FTE 信息中心的 Message formats。联合使用 MDB 及一些 JAXB 代码,可以对文件传输的开始和结束进行 Twitter,甚至可以包括在一次传输的元数据中定义的一些散列标签。
清单 4 是特定于 WebSphere MQ FTE 的 MDB。这里,没有连接到一个队列,而是连接到一个 FTE 主题。如果元数据的名称是以关键词 Twitter 开头的,那么与这个文件传输相关联的这个元数据将被追加到状态消息。例如,Twitter.tag=SimpleTweet 会导致 #SimpleTweet 被追加到这个被发送的消息:
清单 4. MQEventMDB.java: onMessage() 方法
public void onMessage(Message message) {
if(message instanceof TextMessage) {
try {
TwitterPlugin tp = new TwitterPlugin("YOUR_USERNAME", "YOUR_PASSWORD");
String utput = ((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");
}
}
|
下面的图 3 和 图 4 显示了一个文件传输开始和结束时所生成的 tweet:
图 3. WebSphere MQ FTE 文件传输开始

图 4. WebSphere MQ FTE 文件传输结束

本文向您展示了连接 Twitter 与 WebSphere MQ 队列管理器或是 WebSphere MQ 应用程序是多么容易实现以及它对于将状态消息广播给感兴趣的群体是多么地有效。在企业中可以部署一个名为 StatusNet(以前被称为 Laconica)的 Twitter-风格的开源实现,该实现使用了与 Twitter 完全相同的 API,可以让企业能够快速地建立并部署一个社交网络基础设施。您可以很容易地用 Status.net 代替 Twitter 来开始发布有关您企业工作的信息,而同时无需担心有任何的保密和敏感信息被发布到公共领域。