级别: 中级 Krishnakumar Balachandar (krishnakumarb@in.ibm.com), 软件工程师,WebSphere Application Server Community Edition Support Team, IBM
2006 年 2 月 09 日 WebSphere Application Server Community Edition 是基于 Apache Geronimo 的开放源代码 J2EE 应用服务器。本文将介绍如何将其与 WebSphere MQ V6 一起使用。
引言
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
WebSphere MQ 资源适配器属性
部署描述符 (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
要配置 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,需要进行以下操作:
- 在 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) 使用它。
- 部署 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 | HTTP |
|---|
参考资料
关于作者  | 
|  |
Krishnakumar Balachandar 是 IBM 印度 Geronimo 和 WebSphere Application Server Community Edition Support Team 的软件工程师。他的技术专长为业务集成和 J2EE 技术。您可以通过 krishnakumarb@in.ibm.com 与 Krishnakumar 联系。 |
对本文的评价
|