在本文中,我将告诉大家如何在JBoss里安装、配置WebSphere MQ资源适配器。
安装WebSphere MQ资源适配器:
从安装目录复制wmq.jmsra.rar文件到服务器的部署目录中,例如:<安装路径>/server/default/deploy。此时资源适配器将会自动选择服务器。
安装WebSphere MQ扩展事务客户端:
WebSphere MQ扩展事务客户端允许你使用XA分布式事务,并用客户端模式连接到WebSphere MQ队列管理器。要将客户端安装在JBoss,需要从安装目录复制com.ibm.mqetclient.jar文件到服务器的lib目录中,例如<安装路径>/server/default/lib。
为你的应用程序配置资源适配器:
WebSphere MQ资源适配器允许你定义一些全局的属性。在JBoss中JCA出站流的资源定义在-ds.xml文件,名为wmq.jmsra-ds.xml。
此文件的概要如下:
1 | <? xml version = "1.0" encoding = "UTF-8" ?> |
3 | <!-- mbeans defining JCA administered objects --> |
5 | <!-- JCA Connection factory definitions --> |
6 | < tx-connection-factory /> |
从上可以看出JCA连接工厂的定义在<tx-connection-factory>元素,而且JMS队列和主题被定义在JCA的管理对象mbean里面。
简单的两个队列-入站和出站的完整示例如下:
01 | <? xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?> |
03 | < tx-connection-factory > |
04 | < jndi-name >jms/MyAppConnectionFactory</ jndi-name > |
05 | < rar-name >wmq.jmsra.rar</ rar-name > |
06 | < use-java-context >true</ use-java-context > |
07 | < connection-definition >javax.jms.QueueConnectionFactory</ connection-definition > |
08 | < min-pool-size >8</ min-pool-size > |
09 | < max-pool-size >36</ max-pool-size > |
10 | < config-property type = "java.lang.String" name = "channel" >${channel}</ config-property > |
11 | < config-property type = "java.lang.String" name = "hostName" >${hostName}</ config-property > |
12 | < config-property type = "java.lang.String" name = "port" >1414</ config-property > |
13 | < config-property type = "java.lang.String" name = "queueManager" >${queueManager}</ config-property > |
14 | < config-property type = "java.lang.String" name = "transportType" >CLIENT</ config-property > |
15 | < config-property type = "java.lang.String" name = "username" >munish</ config-property > |
16 | < security-domain-and-application >JmsXARealm</ security-domain-and-application > |
18 | </ tx-connection-factory > |
19 | < mbean name = "imq.queue:name=MY.APP.INBOUND.QUEUE" code = "org.jboss.resource.deployment.AdminObject" > |
20 | < attribute name = "JNDIName" >jms/IncomingQueue</ attribute > |
21 | < depends optional-attribute-name = "RARName" >jboss.jca:name='wmq.jmsra.rar',service=RARDeployment</ depends > |
22 | < attribute name = "Type" >javax.jms.Queue</ attribute > |
23 | < attribute name = "Properties" > |
24 | baseQueueManagerName=${queueManager} |
25 | baseQueueName=MY.APP.INBOUND.QUEUE |
28 | < mbean name = "imq.queue:name=MY.APP.OUTBOUND.QUEUE" code = "org.jboss.resource.deployment.AdminObject" > |
29 | < attribute name = "JNDIName" >jms/OutgoingQueue</ attribute > |
30 | < depends optional-attribute-name = "RARName" >jboss.jca:name='wmq.jmsra.rar',service=RARDeployment</ depends > |
31 | < attribute name = "Type" >javax.jms.Queue</ attribute > |
32 | < attribute name = "Properties" > |
33 | baseQueueManagerName=${queueManager} |
34 | baseQueueName=MY.APP.OUTBOUND.QUEUE |
37 | </ connection-factories > |
在JBoss启动脚本中占位符属性被定义为:
-Dchannel=MQTEST -DhostName=localhost -DqueueManager=TST_MGR
配置入站消息传递:
消息传递配置在jboss.xml文件中,这个文件的内容会有所不同,但大致如下:
jboss.xml
01 | < jboss xmlns = "http://www.jboss.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
02 | xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee |
03 | http://www.jboss.org/j2ee/schema/jboss_5_0.xsd" version = "5.0" > |
06 | < ejb-name >MyMessageBean</ ejb-name > |
07 | <!-- Make sure following System properties are defined in JBoss before deploying the application --> |
09 | < activation-config-property > |
10 | < activation-config-property-name >DestinationType</ activation-config-property-name > |
11 | < activation-config-property-value >javax.jms.Queue</ activation-config-property-value > |
12 | </ activation-config-property > |
13 | < activation-config-property > |
14 | < activation-config-property-name >destination</ activation-config-property-name > |
15 | < activation-config-property-value >MY.APP.INBOUND.QUEUE</ activation-config-property-value > |
16 | </ activation-config-property > |
17 | < activation-config-property > |
18 | < activation-config-property-name >channel</ activation-config-property-name > |
19 | < activation-config-property-value >${channel}</ activation-config-property-value > |
20 | </ activation-config-property > |
21 | < activation-config-property > |
22 | < activation-config-property-name >hostName</ activation-config-property-name > |
23 | < activation-config-property-value >${hostName}</ activation-config-property-value > |
24 | </ activation-config-property > |
25 | < activation-config-property > |
26 | < activation-config-property-name >port</ activation-config-property-name > |
27 | < activation-config-property-value >1414</ activation-config-property-value > |
28 | </ activation-config-property > |
29 | < activation-config-property > |
30 | < activation-config-property-name >queueManager</ activation-config-property-name > |
31 | < activation-config-property-value >${queueManager}</ activation-config-property-value > |
32 | </ activation-config-property > |
33 | < activation-config-property > |
34 | < activation-config-property-name >transportType</ activation-config-property-name > |
35 | < activation-config-property-value >CLIENT</ activation-config-property-value > |
36 | </ activation-config-property > |
37 | < activation-config-property > |
38 | < activation-config-property-name >username</ activation-config-property-name > |
39 | < activation-config-property-value >munish</ activation-config-property-value > |
40 | </ activation-config-property > |
42 | <!-- instruct the MDB to use the WebSphere MQ resource adapter --> |
43 | < resource-adapter-name >wmq.jmsra.rar</ resource-adapter-name > |
必须指定目标类型和名称属性。其他属性都是可选的,如果省略,将使用其默认值。
一个简单的MDB定义入站消息如下所示:
02 | * This message driven bean is used to collect asynchronously received messages. |
04 | * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> Munish Gogna |
06 | @MessageDriven (mappedName = "jms/IncomingQueue" ) |
07 | public final class MyMessageBean implements javax.jms.MessageListener { |
08 | private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MyMessageBean. class ); |
09 | public void onMessage( final javax.jms.Message message) { |
10 | LOGGER.info( "Received message: " + message); |
11 | // TODO - do processing with the message here |
一个简单的java类发送消息到输出队列如下所示:
02 | * This class is used to send text messages to a queue. |
04 | * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> Munish Gogna |
06 | public final class MessageSender { |
07 | private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MessageSender. class ); |
09 | * A coarse-grained method, nothing fancy. In actual applications please re factor the code and |
10 | * handle the exceptions properly. |
14 | public void send( final String message) throws Exception { |
15 | if (message == null ) { |
16 | throw new IllegalArgumentException( "Parameter message cannot be null" ); |
18 | javax.jms.QueueConnection connection = null ; |
19 | javax.jms.Session session = null ; |
21 | javax.naming.Context fContext = new javax.naming.InitialContext(); |
22 | javax.jms.QueueConnectionFactory fConnectionFactory = (javax.jms.QueueConnectionFactory) fContext |
23 | .lookup( "java:jms/MyAppConnectionFactory" ); |
24 | connection = fConnectionFactory.createQueueConnection(); |
25 | session = connection.createSession( false , javax.jms.Session.AUTO_ACKNOWLEDGE); |
26 | final javax.naming.Context jndiContext = new javax.naming.InitialContext(); |
27 | final javax.jms.Destination destination = (javax.jms.Destination) jndiContext.lookup( "jms/OutgoingQueue" ); |
28 | final javax.jms.MessageProducer messageProducer = session.createProducer(destination); |
29 | final javax.jms.TextMessage textMessage = session.createTextMessage(message); |
30 | messageProducer.send(textMessage); |
31 | messageProducer.close(); |
32 | } catch (Exception nex) { |
35 | if (session != null ) { |
38 | } catch (JMSException e) { |
39 | LOGGER.error( "Failed to close JMS session" , e); |
42 | if (connection != null ) { |
45 | } catch (JMSException e) { |
46 | LOGGER.error( "Failed to close JMS connection" , e); |
最终RFHUTILC.EXE可用于连接到远程队列
原文地址:http://www.oschina.net/question/157182_58109