IBM® WebSphere® Application Server Community Edition(以下称为 Community Edition)使用 ActiveMQ(一个开放源代码消息传递提供程序)来提供消息传递支持。部署在 Community Edition 上的 J2EE 应用程序可以使用在 ActiveMQ 中创建的队列和主题,而且消息驱动 Bean (MDB) 可以作为一个端点来从队列或主题提取消息。本文将向您介绍如何将 WebSphere MQ 作为 Community Edition 的消息传递提供程序来加以使用。我们将通过 J2EE Connector Architecture (JCA) 资源适配器来利用 WebSphere MQ 提供的 JMS 支持。该资源适配器提供与队列和主题的出站通信以及入站通信(因为该资源适配器从队列中提取消息并将其发送到消息 Bean)。
以下图表显示用于集成的 Community Edition 和 WebSphere MQ 的组件:
- WebSphere MQ 中的队列/主题
- WebSphere MQ 资源适配器
- 用于 WebSphere MQ 的 JNDI 文件绑定
- 用于入站通信的消息驱动 Bean
- 用于出站通信的 Servlet
部署描述符 (ra.xml) 中列出了资源适配器属性。以下部分显示适配器连接到 WebSphere MQ 所需要的出站、入站和受管理对象配置属性。
出站资源适配器配置属性:
-
url - JNDI 提供程序 URL,例如:
ldap://test/o=test,c=com,file:/C:/JNDI-Directory -
icf - 初始上下文工厂,例如:
com.sun.jndi.fscontext.RefFSContextFactory -
name - 工厂名称,例如:
wasceQCF
清单 1. ra.xml 中的出站配置
<outbound-resourceadapter>
<config-property>
<description>JNDI provider url</description>
<config-property-name>url</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
<config-property>
<description>Initial Context Factory</description>
<config-property-name>icf</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
<config-property>
<description>Factory Name</description>
<config-property-name>name</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
</outbound-resourceadapter>
|
入站资源适配器配置属性:
-
url - JNDI 提供程序 URL
-
icf - 初始上下文工厂
-
name - 工厂名称
-
destination - 从中提取消息的目的地,例如队列名称
清单 2. ra.xml 中的入站配置
<inbound-resourceadapter>
<activationspec>
<required-config-property>
<config-property-name>url</config-property-name>
</required-config-property>
<required-config-property>
<config-property-name>icf</config-property-name>
</required-config-property>
<required-config-property>
<config-property-name>name</config-property-name>
</required-config-property>
<required-config-property>
<config-property-name>destination</config-property-name>
</required-config-property>
</activationspec>
</inbound-resourceadapter>
|
- 物理名称
- 在 Geronimo 的 JNDI 中创建的队列或主题的名称
清单 3. ra.xml 中的受管理对象
<adminobject>
<adminobject-interface>javax.jms.Queue</adminobject-interface>
<config-property>
<config-property-name>PhysicalName</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
</adminobject>
<adminobject>
<adminobject-interface>javax.jms.Topic</adminobject-interface>
<config-property>
<config-property-name>PhysicalName</config-property-name>
<config-property-type>java.lang.String</config-property-type>
</config-property>
|
RA 的属性映射到一个特定于 Community Edition 的 RA 计划 (wasce-mq.xml),它包含出站连接和受管理对象的特定于实例的属性。在此文件中,我们可以为出站 RA 配置属性和管理对象提供值。例如:
-
url -
ldap://test/o=test,c=com OR file:/C:/JNDI-Directory -
icf -
com.sun.jndi.fscontext.RefFSContextFactory -
name -
wasceQCF -
physical name for queue -
SYSTEM.DEFAULT.LOCAL.QUEUE -
physical name for topic -
SampleTopic
清单 4. 特定于 Geronimo 的计划
<outbound-resourceadapter>
<connection-definition>
<connectiondefinition-instance>
<name>MQQueueFactory</name>
<config-property-setting name="url"></config-property-setting>
<config-property-setting name="icf"></config-property-setting>
<config-property-setting name="name"></config-property-setting>
<connectionmanager>
</connection-definition>
<adminobject>
<adminobject-instance>
<message-destination-name>ivtQ</message-destination-name>
<config-property-setting name="PhysicalName">
SYSTEM.DEFAULT.LOCAL.QUEUE
</config-property-setting>
</adminobject-instance>
</adminobject>
<adminobject>
<adminobject-instance>
<message-destination-name>ivtT</message-destination-name>
<config-property-setting name="PhysicalName">
SampleTopic
</config-property-setting>
</adminobject-instance>
</adminobject>
|
要与 WebSphere MQ 通信,可以使用 J2EE 构件,例如 Servlet 或会话 Bean。下面的清单 5 显示了一个企业应用程序计划,它由用于与 WebSphere MQ 进行出站和入站通信的 Web 模块和 Bean 模块组成。geronimo-application.xml 中添加了 Community Edition 特定配置。Web 模块包含一个指向 ra/MQQueueFactory 和 ra/MQTopicFactory 的引用,而 Bean 模块包含一个指向 RA 实例 MQRA_1 的引用。来自 RA 的入站通信所必需的 Activation Spec 属性是在消息 Bean Activation 配置中指定的。
清单 5. Geronimo 应用程序计划
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application"
configId="wasce/mq.ear" parented=”wasce/mq.rar”>
<module>
<web>WASCEMQWAR.war</web>
<web-app xmlns="http://geronimo.apache.org/xml/ns/web"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming" configId="wasce/mq.war"
parentId="wasce/mq.ear">
<context-priority-classloader>false</context-priority-classloader>
<naming:resource-ref xmlns="http://geronimo.apache.org/xml/ns/naming">
<naming:ref-name>ra/MQQueueFactory</naming:ref-name>
<naming:resource-link>MQQueueFactory</naming:resource-link>
</naming:resource-ref>
<naming:resource-ref xmlns="http://geronimo.apache.org/xml/ns/naming">
<naming:ref-name>ra/MQTopicFactory</naming:ref-name>
<naming:resource-link>MQTopicFactory</naming:resource-link>
</naming:resource-ref>
</web-app>
</module>
<module>
<ejb>WASCEMQEJB.jar</ejb>
<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming" configId="wasce/mq.ejb"
parentId="wasce/mq.ear">
<enterprise-beans>
<message-driven>
<ejb-name>MQMDB</ejb-name>
<resource-adapter>
<resource-link>MQRA_1</resource-link>
</resource-adapter>
<activation-config>
<activation-config-property>
<activation-config-property-name>url</activation-config-property-name>
<activation-config-property-value>
file://C:/JNDI-Directory
</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>icf</activation-config-property-name>
<activation-config-property-value>
com.sun.jndi.fscontext.RefFSContextFactory
</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>name</activation-config-property-name>
<activation-config-property-value>
wasceQCF
</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>
SYSTEM.DEFAULT.LOCAL.QUEUE
</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
</openejb-jar>
</module>
<ext-module>
<connector>MQRA_1</connector>
<external-path>mq/rars/wascemq.rar</external-path>
<connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector" configId="wasce/mq.rar.1"
parentId="wasce/mq.ear">
<resourceadapter>
<resourceadapter-instance>
<resourceadapter-name>MQRA_1</resourceadapter-name>
<workmanager>
<gbean-link>DefaultWorkManager</gbean-link>
</workmanager>
</resourceadapter-instance>
<outbound-resourceadapter>
...( Same as Listing 4 )
</connector>
<ext-module>
</application>
|
为 Community Edition 配置 Websphere MQ
以上部分描述了资源适配器和用于资源适配器和 J2EE 应用程序的应用程序计划。下一部分将介绍如何配置 WebSphere MQ 和 Community Edition,以及运行示例。
- WebSphere MQ V6.0
- WebSphere Application Server Community Edition V1.0
要配置 WebSphere MQ,需要进行以下操作:
- 为 Queue 创建一个 Queue Manager (
WASCE_Q)。缺省创建的一个名为SYSTEM.DEFAULT.LOCAL.QUEUE的本地 Queue。 - 如果您在创建 Queue Manager 时没有创建和启动一个在端口 1414 上侦听的侦听器,则现在就创建一个。
- 创建和启动一个 Server Connection Channel
CHANNEL1。 - 为 Topic 创建一个 Queue Manager (
WASCE_T) 并启动它,将其作为来自 Service 的代理。 - 为此 QueueManager 创建和启动一个在端口 1434 上侦听的侦听器。
- 创建和启动一个 Server Connection Channel
CHANNEL1。 - 使用文件
MQJMS_PSQ.mqsc运行 MQSC,其中包含行:runmqsc WASCE_T < MQJMS_PSQ.mqsc。 - 使用 JMSAdmin 工具来为
QueueConnectionFactory和TopicConnectionFactory创建 JNDI 绑定。设置JMSAdmin.config以使用 File Context JNDI,并指向C:/JNDI-Directory。运行 JMSAdmin 工具并创建绑定:QueueConnectionfactory DEFINE QCF(wasceQCF) HOST(localhost) PORT(1414) CHANNEL(CHANNEL1) QMGR(WASCE_Q) TRAN(client) TopicConnectionFactory DEFINE TCF(wasceTCF) HOST(localhost) PORT(1434) CHANNEL(CHANNEL1)qmgr(WASCE_T) TRAN(client) Subscriber DEFINE T(wasceTopic) TOPIC(SampleTopic)
要配置 Community Edition,需要进行以下操作:
- 在 Community Edition 内创建以下文件夹结构,并复制以下 WebSphere MQ JAR 和 RAR 文件。MQ JAR 文件位于 WebSphere MQ
home/java/lib directory中:
- 使用 Java:
<community edition home>/bin/java -jar server.jar启动服务器。不要使用 Windows 快捷方式。 - 使用命令行或控制台将服务器范围内的 Resource Adapter 部署到该服务器。现在,应用程序可以创建此 RA 的实例并在应用程序范围内 (
<CE HOME>/bin/deploy deploy wasce-mq.xml<path>/wascemq.rar) 使用它。
XML error: The image is not displayed because the width is greater than the maximum of 580 pixels. Please decrease the image width.
- 部署 EAR,该 EAR 包含来自 WebSphere MQ 的入站通信的 MDB、与 Queue/Topic 进行出站通信的 WAR,以及在步骤 2 中部署的 RA 的实例:
要运行示例并检查与 WebSphere MQ 的出站和入站通信,需要进行以下操作:
- 使用已部署的 WAR 来检查与 Queue 或 Topic 的出站通信:
- 转到
http://localhost:8080/mq:
- 输入详细信息并单击 Queue 或 Topic
- 该消息发送到 Queue:
SYSTEM.DEFAULT.LOCAL.QUEUE in WASCE_Q. - 对于 Topic,该消息发送到 SampleTopic:
- 启动一个示例订户以检查是否收到该消息。WebSphere MQ 有一个称为
JMSPubSub.java的示例程序,您可以使用它来运行 SampleTopic 的订户。它驻留在<MQ-DIR>/Tools/Java/jms/PubSub.java中。在运行订户之后,您可以看到寄送到该 Topic 的消息:
- 将测试消息寄送到 WebSphere MQ 中的 Queue (
SYSTEM.DEFAULT.LOCAL.QUEUE)。 - MDB 的 onMessage 应该会被调用 ,并且您应该会在 Community Edition 控制台中看到以下消息。可以通过添加另一个用于 Topic 的 ActivationSpec 来将其扩展到 Topic。
本文介绍了如何配置 WebSphere MQ 和 WebSphere Application Server Community Edition。我们可以向资源适配器添加 XA 事务,以便在 Community Edition 和 WebSphere MQ 中支持分布式事务。
| 描述 | 名字 | 大小 | 下载方法 |
|---|---|---|---|
| Code samples in zip format | wasce_mq.zip | 56 KB | FTP |
- 您可以参阅本文在 developerWorks 全球站点上的 英文原文。
-
developerWorks WebSphere Application Server Community Edition 专区
对于开发人员,可以访问“How-to”文章、下载、教程、教育内容、产品信息和支持信息等等。 -
WebSphere Application Server Community Edition 入门
Community Edition 是基于 Apache Geronimo 的开放源代码 Java 应用服务器。这篇文章描述该产品,有助于您快速掌握和运行选择正确的下载包、设置环境和部署应用程序等指令。 -
Apache Geronimo 项目
来自 Apache Software Foundation 的开放源代码 J2EE 应用服务器的项目站点。 -
WebSphere MQ 产品页
产品说明、产品新闻、培训信息和支持信息等等。 -
WebSphere MQ 文档库
PDF 格式的 WebSphere MQ 手册。 -
WebSphere MQ V6 Information Center
一个基于 Eclipse 的 Web 界面,用于查看所有 WebSphere MQ V6 文档。 -
Introduction to J2EE Connector Architecture
首先详细介绍 JCA(包括体系结构的基本元素),接着进行举例说明,并提供了一个示例应用程序来阐释遵循和支持 JCA 的系统的各个部分如何一起工作。 -
为 WebSphere MQ 开发独立的 Java 应用程序
介绍如何开发一个 Java 应用程序来使用 WebSphere MQ 通过标准的 JMS 和 JNDI API 发送和接收消息。 -
developerWorks WebSphere Business Integration 专区
对于开发人员,可以访问“How-to”文章、下载、教程、教育内容、产品信息和支持信息等等。 -
WebSphere Business Integration 产品页
对于企业和技术用户,这是一个触手可及的对所有 WebSphere Business Integration 产品的概述。 -
IBM 软件产品试用版下载
免费下载精选的 IBM® DB2®、Lotus®、Rational®、Tivoli® 和 WebSphere® 产品的试用版。 -
最流行的 WebSphere 产品试用版下载
免费下载主要的 WebSphere 产品试用版。 -
Safari Bookshelf:为开发人员设计的电子书库
只需交纳一次订阅费即可搜索和访问数千种技术图书。新订户可以免费试用。 -
WebSphere 论坛
这是一个针对特定产品的论坛,在这个论坛中,您可以提问并与其他 WebSphere 用户分享您的观点。 -
developerWorks 博客
这是一个即时更新的自由论坛,由软件专家主持,您可以在其中加入您的评论。浏览 Grady Booch 关于软件体系结构的博客。

Krishnakumar Balachandar 是 IBM 印度 Geronimo 和 WebSphere Application Server Community Edition Support Team 的软件工程师。他的技术专长为业务集成和 J2EE 技术。您可以通过 krishnakumarb@in.ibm.com 与 Krishnakumar 联系。