如何在WebSphere中使用Activation Specifications连接MDB并部署消费代码
WebSphere中基于Activation Specifications的MQ消息消费配置示例
以下是完整的配置示例,包含消息驱动Bean(MDB)代码、标准EJB描述符、WebSphere绑定文件,以及部署注意事项:
1. 消息驱动Bean(MDB)代码
实现javax.jms.MessageListener接口处理消息:
import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class MQMessageConsumerMDB implements MessageListener { @Override public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage textMsg = (TextMessage) message; System.out.println("Received MQ message: " + textMsg.getText()); } } catch (Exception e) { e.printStackTrace(); } } }
2. ejb-jar.xml 标准配置
在META-INF/ejb-jar.xml中定义MDB和Activation Spec的基础配置:
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1"> <enterprise-beans> <message-driven> <ejb-name>MQMessageConsumerMDB</ejb-name> <ejb-class>com.example.MQMessageConsumerMDB</ejb-class> <messaging-type>javax.jms.MessageListener</messaging-type> <activation-config> <!-- 通用JMS配置 --> <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> <!-- IBM MQ专属配置,根据实际环境修改 --> <activation-config-property> <activation-config-property-name>hostName</activation-config-property-name> <activation-config-property-value>mq-server.example.com</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>queueManager</activation-config-property-name> <activation-config-property-value>QMGR01</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>channel</activation-config-property-name> <activation-config-property-value>SYSTEM.DEF.SVRCONN</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>destination</activation-config-property-name> <activation-config-property-value>QUEUE01</activation-config-property-value> </activation-config-property> </activation-config> </message-driven> </enterprise-beans> </ejb-jar>
3. ibm-ejb-jar-bnd.xmi WebSphere绑定配置
在META-INF/ibm-ejb-jar-bnd.xmi中完成WebSphere特定的绑定,有两种常见方式:
方式1:绑定到WebSphere控制台预创建的Activation Specification
如果已在控制台创建Activation Spec(JNDI名称为jms/MQActivationSpec),使用以下配置:
<?xml version="1.0" encoding="UTF-8"?> <ejbbnd:EJBJarBinding xmlns:xmi="http://www.omg.org/XMI" xmlns:ejbbnd="ejbbnd.xmi" xmlns:ejb="ejb.xmi" xmi:version="2.0"> <ejbJar href="META-INF/ejb-jar.xml#ejb-jar_ID"/> <messageDrivenBinding ejbName="MQMessageConsumerMDB"> <activationSpecBinding resourceRefBindingName="jms/MQActivationSpec"/> </messageDrivenBinding> </ejbbnd:EJBJarBinding>
方式2:直接在绑定文件中指定Activation Spec属性
若不想在ejb-jar.xml中硬编码参数,可在绑定文件中配置:
<?xml version="1.0" encoding="UTF-8"?> <ejbbnd:EJBJarBinding xmlns:xmi="http://www.omg.org/XMI" xmlns:ejbbnd="ejbbnd.xmi" xmlns:ejb="ejb.xmi" xmlns:wasJms="wasJms.xmi" xmi:version="2.0"> <ejbJar href="META-INF/ejb-jar.xml#ejb-jar_ID"/> <messageDrivenBinding ejbName="MQMessageConsumerMDB"> <activationSpecBinding> <specProperties xmi:id="Prop1" name="hostName" value="mq-server.example.com"/> <specProperties xmi:id="Prop2" name="port" value="1414"/> <specProperties xmi:id="Prop3" name="queueManager" value="QMGR01"/> <specProperties xmi:id="Prop4" name="channel" value="SYSTEM.DEF.SVRCONN"/> <specProperties xmi:id="Prop5" name="destination" value="QUEUE01"/> <specProperties xmi:id="Prop6" name="userName" value="mquser"/> <specProperties xmi:id="Prop7" name="password" value="mqpass"/> </activationSpecBinding> </messageDrivenBinding> </ejbbnd:EJBJarBinding>
4. 部署要点
- 确认WebSphere服务器已部署IBM MQ客户端资源适配器(
wmq.jmsra.rar),可在资源 > 资源适配器中查看。 - 若使用预创建的Activation Spec,需在WebSphere控制台的资源 > JMS > 激活规范中创建,指定对应的MQ连接参数、队列等。
- 将MDB类、ejb-jar.xml、ibm-ejb-jar-bnd.xmi打包为EJB模块(ejb.jar),或包含在EAR包中部署。
- 部署后可在应用程序 > 企业应用程序 > [你的应用] > 消息驱动Bean中验证和调整Activation Spec配置。
内容的提问来源于stack exchange,提问作者arul sounder
相关产品推荐
相关产品推荐

