社交网络已经取得了爆炸式的发展,下面所列就是一些社交网络的站点: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 output = ((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 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");
}
}
|
下面的图 3 和 图 4 显示了一个文件传输开始和结束时所生成的 tweet:
图 3. WebSphere MQ FTE 文件传输开始

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

本文向您展示了连接 Twitter 与 WebSphere MQ 队列管理器或是 WebSphere MQ 应用程序是多么容易实现以及它对于将状态消息广播给感兴趣的群体是多么地有效。在企业中可以部署一个名为 StatusNet(以前被称为 Laconica)的 Twitter-风格的开源实现,该实现使用了与 Twitter 完全相同的 API,可以让企业能够快速地建立并部署一个社交网络基础设施。您可以很容易地用 Status.net 代替 Twitter 来开始发布有关您企业工作的信息,而同时无需担心有任何的保密和敏感信息被发布到公共领域。
| 描述 | 名字 | 大小 | 下载方法 |
|---|---|---|---|
| 示例代码 | TwitterMQ.zip | 90 KB | HTTP |
学习
- WebSphere MQ developer 资源页面
技术资源,可帮您用 WebSphere MQ 设计、开发和部署消息中间件来集成几乎所有平台上的应用程序、Web 服务以及事务。 - WebSphere MQ V7 信息中心
面向所有 WebSphere MQ V7 文档的一站式 Web 门户,内含有关安装、配置和使用 WebSphere MQ V7 的概念、任务和参考信息。 - IBM Redbook: WebSphere MQ V7 特性和增强
介绍了消息排队技术的基本概念和好处,概述了 V7 内的新特性并给出了一个展示这些特性的业务场景。 - WebSphere MQ FTE 信息中心
WebSphere MQ FTE V7 文档的一站式 Web 门户,内含有关安装、配置和使用 WebSphere MQ FTE 的概念、任务和参考信息 - WebSphere MQ FTE 论坛
获得您对 WebSphere MQ FTE 的技术问题的解答并与其他用户分享您的 WebSphere MQ FTE 知识。 - 下载 WebSphere MQ FTE V7.0.2 的免费试用版
WebSphere MQ FTE V7.0.2 的一个功能全面的 90 天免费试用版。 - developerWorks 上的 WebSphere MQ FTE blog
一个论坛,在这里您可以与负责开发 WebSphere MQ FTE 的 IBM 团队交流,这其中包括本文的作者 Rich Cumbers。 - developerWorks WebSphere developer 资源
面向使用 WebSphere 产品的开发人员的技术信息和资源。developerWorks WebSphere 提供了产品下载、how-to 信息、支持资源以及包括 2000 多篇技术文章、教程、最佳实践、IBM 红皮书和在线产品手册在内的免费技术库。 - developerWorks WebSphere application connectivity developer 资源
可用来帮助您构建 WebSphere 应用程序连接及业务集成解决方案的 How-to 文章、下载、教程、培训、产品信息以及其他资源。 - developerWorks 时事通讯
developerWorks 简报为您提供您所感兴趣的那些主题的最新文章和信息。除 WebSphere 之外,您还可以从 Java、Linux、Open source、Rational、SOA、 Web services 等主题中选择。立即订阅并设计您自己的定制邮寄。 - 来自 IBM 出版社的与 WebSphere 相关的书籍
通过 Barnes & Noble 可方便地在线订购。
获得产品和技术
- WebSphere MQ 产品页面
产品描述、产品新闻、培训信息、支持信息等。 - WebSphere MQ 文档库
WebSphere MQ 产品手册。 - 下载 WebSphere MQ V7.0.1 的免费试用版
WebSphere MQ V7.0.1 的一个功能全面的 90 天免费试用版。 - 最受欢迎的 WebSphere 试用软件下载
下载关键 WebSphere 产品的免费试用版。 - WebSphere MQ 支持页面
支持问题及解决方案的一个可搜索数据库,外加下载、修复、问题跟踪等。 - 下载 WebSphere Application Server Community Edition 的免费试用版
WebSphere Application Server Community Edition 的一个功能全面的 90 天免费试用版。 - 最流行的 WebSphere 试用版下载
关键 WebSphere 产品的免费试用版下载。
讨论
- WebSphere 论坛
特定于产品的论坛,在这里您可以获得您对技术问题的解答,也可以与其他的 WebSphere 用户分享您的经验。 - WebSphere-相关活动
面向感兴趣的 WebSphere 开发人员的全球范围内的会议、展览、网络广播和其他活动。 - developerWorks blogs
与 developerWorks 用户和作者以及 IBM 编辑和开发人员进行交流。 - developerWorks Webcasts
IBM 专家的免费技术会话,可加速您的学习曲线并能帮助您在最复杂的软件项目中取得成功。广播的时长从半个小时到半天或一整天不等,并且是在全世界范围内的。 -
My developerWorks BPM 群组
为开发人员设立的 BPM 群组,了解业务流程管理解决方案的最新技术资源。
