上一篇我们知道了消息驱动Bean的基本用法,实际大型分布式企业应用中,往往会采用高性能的商业Queue产品,比如IBM Webshpere MQ(目前最新版本是7.5 ),下面讲解下如何在Jboss EAP 6.2 版本上整合Webshpere MQ 7.5
一、修改jboss的standalone-full.xml
a) 添加IBM的resource-adapters
找到
1 <subsystem xmlns="urn:jboss:domain:resource-adapters:1.1"> 2 <resource-adapters> 3 <resource-adapter id="wmq.jmsra.rar"> 4 <archive> 5 wmq.jmsra.rar 6 archive> 7 <transaction-support>NoTransactiontransaction-support> 8 <connection-definitions> 9 <connection-definition class-name="com.ibm.mq.connector.outbound.ManagedConnectionFactoryImpl" jndi-name="java:/MyConnectionFactoryCnblogs" use-java-context="true" pool-name="connectionfactorypoolcnblogs"> 10 <config-property name="port"> 11 1414 12 config-property> 13 <config-property name="channel"> 14 DC.SVRCONN 15 config-property> 16 <config-property name="hostName"> 17 localhost 18 config-property> 19 <config-property name="transportType"> 20 CLIENT 21 config-property> 22 <config-property name="queueManager"> 23 QM_APPLE 24 config-property> 25 connection-definition> 26 connection-definitions> 27 resource-adapter> 28 resource-adapters> 29 subsystem>
b) 找到
1 <mdb> 2 <resource-adapter-ref resource-adapter-name="wmq.jmsra.rar"/> 3 <bean-instance-pool-ref pool-name="mdb-strict-max-pool"/> 4 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) 注解方式
1 import javax.ejb.ActivationConfigProperty; 2 import javax.ejb.MessageDriven; 3 import javax.jms.JMSException; 4 import javax.jms.Message; 5 import javax.jms.MessageListener; 6 import javax.jms.TextMessage; 7 import util.LoggerUtil; 8 9 @MessageDriven(name = "WebSphereMQMDB", activationConfig = { 10 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), 11 @ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "false"), 12 @ActivationConfigProperty(propertyName = "hostName", propertyValue = "172.21.126.177"), 13 @ActivationConfigProperty(propertyName = "port", propertyValue = "1414"), 14 @ActivationConfigProperty(propertyName = "channel", propertyValue = "DC.SVRCONN"), 15 @ActivationConfigProperty(propertyName = "queueManager", propertyValue = "QM_APPLE"), 16 @ActivationConfigProperty(propertyName = "destination", propertyValue = "Q1"), 17 @ActivationConfigProperty(propertyName = "transportType", propertyValue = "CLIENT") }) 18 public class HelloWorldMDB implements MessageListener {
注:HelloWorldMDB的onMessage方法处理跟上一篇完全相同,就不重复了。
b) jboss-ejb3.xml 方式
1 xml version="1.0" encoding="UTF-8"?> 2 <jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:c="urn:clustering:1.0" 5 xmlns:r="urn:resource-adapter-binding" 6 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" 7 version="3.1" impl-version="2.0"> 8 <enterprise-beans> 9 <message-driven> 10 <ejb-name>WebSphereMQMDBejb-name> 11 <ejb-class>mdb.HelloWorldMDBejb-class> 12 <activation-config> 13 <activation-config-property> 14 <activation-config-property-name>destinationTypeactivation-config-property-name> 15 <activation-config-property-value>javax.jms.Queueactivation-config-property-value> 16 activation-config-property> 17 <activation-config-property> 18 <activation-config-property-name>useJNDIactivation-config-property-name> 19 <activation-config-property-value>falseactivation-config-property-value> 20 activation-config-property> 21 <activation-config-property> 22 <activation-config-property-name>hostNameactivation-config-property-name> 23 <activation-config-property-value>172.21.126.177activation-config-property-value> 24 activation-config-property> 25 <activation-config-property> 26 <activation-config-property-name>portactivation-config-property-name> 27 <activation-config-property-value>1414activation-config-property-value> 28 activation-config-property> 29 <activation-config-property> 30 <activation-config-property-name>channelactivation-config-property-name> 31 <activation-config-property-value>DC.SVRCONNactivation-config-property-value> 32 activation-config-property> 33 <activation-config-property> 34 <activation-config-property-name>queueManageractivation-config-property-name> 35 <activation-config-property-value>QM_APPLEactivation-config-property-value> 36 activation-config-property> 37 <activation-config-property> 38 <activation-config-property-name>destinationactivation-config-property-name> 39 <activation-config-property-value>Q1activation-config-property-value> 40 activation-config-property> 41 <activation-config-property> 42 <activation-config-property-name>transportTypeactivation-config-property-name> 43 <activation-config-property-value>CLIENTactivation-config-property-value> 44 activation-config-property> 45 activation-config> 46 message-driven> 47 enterprise-beans> 48 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控制台上,会马上显示已收到消息
同时在%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