开发者社区> 问答> 正文

如何在 JBoss 里配置 IBM MQ:报错

在本文中,我将告诉大家如何在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。
此文件的概要如下:

<?xml version="1.0" encoding="UTF-8"?>
<connection-factories>
  <!-- mbeans defining JCA administered objects -->
  <mbean/>
  <!-- JCA Connection factory definitions -->
  <tx-connection-factory/>
</connection-factories>

从上可以看出JCA连接工厂的定义在<tx-connection-factory>元素,而且JMS队列和主题被定义在JCA的管理对象mbean里面。

简单的两个队列-入站和出站的完整示例如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<connection-factories>
    <tx-connection-factory>
        <jndi-name>jms/MyAppConnectionFactory</jndi-name>
        <rar-name>wmq.jmsra.rar</rar-name>
        <use-java-context>true</use-java-context>
        <connection-definition>javax.jms.QueueConnectionFactory</connection-definition>
        <min-pool-size>8</min-pool-size>
        <max-pool-size>36</max-pool-size>
        <config-property type="java.lang.String" name="channel">${channel}</config-property>
        <config-property type="java.lang.String" name="hostName">${hostName}</config-property>
        <config-property type="java.lang.String" name="port">1414</config-property>
        <config-property type="java.lang.String" name="queueManager">${queueManager}</config-property>
        <config-property type="java.lang.String" name="transportType">CLIENT</config-property>
        <config-property type="java.lang.String" name="username">munish</config-property>
        <security-domain-and-application>JmsXARealm</security-domain-and-application>
        <xa-transaction/>
    </tx-connection-factory>
    <mbean name="imq.queue:name=MY.APP.INBOUND.QUEUE" code="org.jboss.resource.deployment.AdminObject" >
  <attribute name="JNDIName">jms/IncomingQueue</attribute>
  <depends optional-attribute-name="RARName">jboss.jca:name='wmq.jmsra.rar',service=RARDeployment</depends>
  <attribute name="Type">javax.jms.Queue</attribute>
  <attribute name="Properties">
   baseQueueManagerName=${queueManager}
   baseQueueName=MY.APP.INBOUND.QUEUE
    </attribute>
    </mbean>
    <mbean name="imq.queue:name=MY.APP.OUTBOUND.QUEUE" code="org.jboss.resource.deployment.AdminObject">
        <attribute name="JNDIName">jms/OutgoingQueue</attribute>
        <depends optional-attribute-name="RARName">jboss.jca:name='wmq.jmsra.rar',service=RARDeployment</depends>
        <attribute name="Type">javax.jms.Queue</attribute>
        <attribute name="Properties">
            baseQueueManagerName=${queueManager}
            baseQueueName=MY.APP.OUTBOUND.QUEUE
        </attribute>
    </mbean>
</connection-factories>

在JBoss启动脚本中占位符属性被定义为:
-Dchannel=MQTEST -DhostName=localhost -DqueueManager=TST_MGR

配置入站消息传递:
消息传递配置在jboss.xml文件中,这个文件的内容会有所不同,但大致如下:
jboss.xml

<jboss xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee 
 http://www.jboss.org/j2ee/schema/jboss_5_0.xsd" version="5.0">
 <enterprise-beans>
  <message-driven>
   <ejb-name>MyMessageBean</ejb-name>
    <!--  Make sure following System properties are defined in JBoss before deploying the application   -->
     <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>destination</activation-config-property-name>
                             <activation-config-property-value>MY.APP.INBOUND.QUEUE</activation-config-property-value>
                     </activation-config-property>
                     <activation-config-property>
                             <activation-config-property-name>channel</activation-config-property-name>
                             <activation-config-property-value>${channel}</activation-config-property-value>
                     </activation-config-property>
                     <activation-config-property>
                             <activation-config-property-name>hostName</activation-config-property-name>
                             <activation-config-property-value>${hostName}</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>${queueManager}</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-property>
                             <activation-config-property-name>username</activation-config-property-name>
                             <activation-config-property-value>munish</activation-config-property-value>
                     </activation-config-property>
              </activation-config>
               <!-- instruct the MDB to use the WebSphere MQ resource adapter -->
           <resource-adapter-name>wmq.jmsra.rar</resource-adapter-name>
     </message-driven>
 </enterprise-beans>
</jboss>

必须指定目标类型和名称属性。其他属性都是可选的,如果省略,将使用其默认值。

一个简单的MDB定义入站消息如下所示:

/**
 * This message driven bean is used to collect asynchronously received messages.
 * 
 * @author  Munish Gogna
 */
@MessageDriven(mappedName = "jms/IncomingQueue")
public final class MyMessageBean implements javax.jms.MessageListener {
  private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MyMessageBean.class);
  public void onMessage(final javax.jms.Message message) {
    LOGGER.info("Received message: " + message);
    // TODO - do processing with the message here
  }
}

一个简单的java类发送消息到输出队列如下所示:

/**
 * This class is used to send text messages to a queue.
 * 
 * @author  Munish Gogna
 */
public final class MessageSender {
  private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MessageSender.class);
  /**
   * A coarse-grained method, nothing fancy. In actual applications please re factor the code and
   * handle the exceptions properly.
   * 
   * @throws Exception
   */
  public void send(final String message) throws Exception {
    if (message == null) {
      throw new IllegalArgumentException("Parameter message cannot be null");
    }
    javax.jms.QueueConnection connection = null;
    javax.jms.Session session = null;
    try {
      javax.naming.Context fContext = new javax.naming.InitialContext();
      javax.jms.QueueConnectionFactory fConnectionFactory = (javax.jms.QueueConnectionFactory) fContext
          .lookup("java:jms/MyAppConnectionFactory");
      connection = fConnectionFactory.createQueueConnection();
      session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
      final javax.naming.Context jndiContext = new javax.naming.InitialContext();
      final javax.jms.Destination destination = (javax.jms.Destination) jndiContext.lookup("jms/OutgoingQueue");
      final javax.jms.MessageProducer messageProducer = session.createProducer(destination);
      final javax.jms.TextMessage textMessage = session.createTextMessage(message);
      messageProducer.send(textMessage);
      messageProducer.close();
    } catch (Exception nex) {
      throw nex;
    } finally {
      if (session != null) {
        try {
          session.close();
        } catch (JMSException e) {
          LOGGER.error("Failed to close JMS session", e);
        }
      }
      if (connection != null) {
        try {
          connection.close();
        } catch (JMSException e) {
          LOGGER.error("Failed to close JMS connection", e);
        }
      }
    }
  }
}

最终RFHUTILC.EXE可用于连接到远程队列。

 

英文链接 , OSChina.NET编译

展开
收起
kun坤 2020-06-06 22:15:10 569 0
1 条回答
写回答
取消 提交回答
  • 灰常 不错的。######有什么体会么,说来听听######MDB是打包成jar包部署到应用服务器还是怎么样?如果是打包,那以后要是遇到变更不是每次都要改代码?

    2020-06-06 22:15:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
RocketMQ Client-GO 介绍 立即下载
RocketMQ Prometheus Exporter 打造定制化 DevOps 平台 立即下载
基于 RocketMQ Prometheus Exporter 打造定制化 DevOps 平台 立即下载