jboss EAP 6.2 + Message Drive Bean(MDB) 整合IBM Webshpere MQ 7.5

时间:2021-06-22 11:14:22

上一篇我们知道了消息驱动Bean的基本用法,实际大型分布式企业应用中,往往会采用高性能的商业Queue产品,比如IBM Webshpere MQ(目前最新版本是7.5 ),下面讲解下如何在Jboss EAP 6.2 版本上整合Webshpere MQ 7.5

一、修改jboss的standalone-full.xml

a) 添加IBM的resource-adapters

找到<subsystem xmlns="urn:jboss:domain:resource-adapters:1.1"/> 改成下面这样(注:里面的参数值,大家根据实际情况,自行修改)

 <subsystem xmlns="urn:jboss:domain:resource-adapters:1.1">
<resource-adapters>
<resource-adapter id="wmq.jmsra.rar">
<archive>
wmq.jmsra.rar
</archive>
<transaction-support>NoTransaction</transaction-support>
<connection-definitions>
<connection-definition class-name="com.ibm.mq.connector.outbound.ManagedConnectionFactoryImpl" jndi-name="java:/MyConnectionFactoryCnblogs" use-java-context="true" pool-name="connectionfactorypoolcnblogs">
<config-property name="port">
1414
</config-property>
<config-property name="channel">
DC.SVRCONN
</config-property>
<config-property name="hostName">
localhost
</config-property>
<config-property name="transportType">
CLIENT
</config-property>
<config-property name="queueManager">
QM_APPLE
</config-property>
</connection-definition>
</connection-definitions>
</resource-adapter>
</resource-adapters>
</subsystem>

b) 找到<subsystem xmlns="urn:jboss:domain:ejb3:1.4">下的<mdb>节点,改成

             <mdb>
<resource-adapter-ref resource-adapter-name="wmq.jmsra.rar"/>
<bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
</mdb>

二、部署wmq.jmsra.rar

IBM MQ的安装目录 C:\Program Files (x86)\IBM\WebSphere MQ\java\lib\jca 下有一个wmq.jmsra.rar文件,把它复制到

%JBOSS_HOME%\standalone\deployments 下,jboss启动后,将自动部署该rar

注:rar包的版本必须与MQ相符(即:如果你要监听MQ 7.5的队列消息,则该rar必须是MQ 7.5自带的)

附: 7.5版wmq.jmsra.rar的下载地址 http://pan.baidu.com/s/1jG5bWAM

三、MDB端的配置
a) 注解方式

 import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import util.LoggerUtil; @MessageDriven(name = "WebSphereMQMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "false"),
@ActivationConfigProperty(propertyName = "hostName", propertyValue = "172.21.126.177"),
@ActivationConfigProperty(propertyName = "port", propertyValue = "1414"),
@ActivationConfigProperty(propertyName = "channel", propertyValue = "DC.SVRCONN"),
@ActivationConfigProperty(propertyName = "queueManager", propertyValue = "QM_APPLE"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "Q1"),
@ActivationConfigProperty(propertyName = "transportType", propertyValue = "CLIENT") })
public class HelloWorldMDB implements MessageListener {

注:HelloWorldMDB的onMessage方法处理跟上一篇完全相同,就不重复了。

b) jboss-ejb3.xml 方式

 <?xml version="1.0" encoding="UTF-8"?>
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="urn:clustering:1.0"
xmlns:r="urn:resource-adapter-binding"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
version="3.1" impl-version="2.0">
<enterprise-beans>
<message-driven>
<ejb-name>WebSphereMQMDB</ejb-name>
<ejb-class>mdb.HelloWorldMDB</ejb-class>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>useJNDI</activation-config-property-name>
<activation-config-property-value>false</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>hostName</activation-config-property-name>
<activation-config-property-value>172.21.126.177</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>port</activation-config-property-name>
<activation-config-property-value>1414</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>channel</activation-config-property-name>
<activation-config-property-value>DC.SVRCONN</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>queueManager</activation-config-property-name>
<activation-config-property-value>QM_APPLE</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>Q1</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>transportType</activation-config-property-name>
<activation-config-property-value>CLIENT</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
</jboss:ejb-jar>

这二种配置方式完全等效

四、测试验证

a) 以standalone-full.xml 配置启动jboss

%jboss_home%\standalone\configuration>move standalone.xml standalone_backup.xml

%jboss_home%\standalone\configuration>copy standalone-full.xml standalone.xml

%jboss_home%\standalone\configuration>..\..\bin\standalone.bat

b) 在MQ所在服务器上,用WebShpere MQ资源管理器,向Q1放入一条测试消息

jboss EAP 6.2 + Message Drive Bean(MDB)  整合IBM Webshpere MQ 7.5

顺利的话,Jboss控制台上,会马上显示已收到消息

jboss EAP 6.2 + Message Drive Bean(MDB)  整合IBM Webshpere MQ 7.5

同时在%JBOSS_HOME%\standalone\log\server.log日志里,也能找到相关记录

12:11:12,559 INFO  [class util.LoggerUtil] (default-threads - 2) Received Message from queue: TEST MESSAGE
12:11:12,560 INFO  [stdout] (default-threads - 2) Received Message from queue: TEST MESSAGE