手机版
你好,游客 登录 注册
背景:
阅读新闻

如何在 JBoss 里配置 IBM MQ

[日期:2012-06-17] 来源:oschina  作者:迷途d书童 [字体:大 中 小]

配置入站消息传递:
消息传递配置在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.
 *
 * <A class=referer href="http://www.linuxidc.com" target=_blank>@author</A>  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.
 *
 * <A class=referer href="http://www.linuxidc.com" target=_blank>@author</A>  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可用于连接到远程队列。

linux
相关资讯       Jboss 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款