从消息驱动Bean(MDB)连接到远程JMS提供程序

时间:2010-12-20 07:17:54

标签: java java-ee jms glassfish-3

从我在Glassfish上部署的EJB或POJO,我可以使用以下代码连接到HornetMQ,之后我向classpath添加必要的特定于大黄蜂的jar:

Properties properties = new Properties();
  properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
  // server name
  properties.put("java.naming.provider.url", "jnp://hostname:1099");
  properties.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

  InitialContext initialContext = new InitialContext(properties);
  // queue name
  Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
  // connection factory
  ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
  Connection conn = connectionFactory.createConnection();

  Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

  conn.start();
    // ...

但我想从Message Driven Bean中做同样的事情。

使用MDB,如果我使用嵌入式Glassfish提供程序,则非常容易;但如何配置GF以使用远程提供商?

有什么想法吗?谢谢!

编辑:让事情变得更清晰;典型的MDB看起来像这样:

@MessageDriven(mappedName = "/queue/exampleQueue", activationConfig =  {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
    })
public class MessageProcessor implements MessageListener {

    public MessageProcessor() {
    }


    public void onMessage(Message message) {

    }
}

但在这种情况下,MDB将在本地服务器上查找“/ queue / exampleQueue”而不是远程服务器。

基本上我的问题是如何在使用MDB时配置GF以查找远程服务器(如第一个代码段中所示)?

2 个答案:

答案 0 :(得分:3)

经过多次挖掘后,我在these forums

找到了解决方案

要使MDB与远程HornetQ提供商“对话”,请按照以下步骤操作:

  • 使用管理控制台将hornetq-ra.rar部署到glassfish(您可以在hornetq文件夹的“libs”文件夹中找到它)
  • 找到<gf_install_dir>/domains/<your_dmain>/applications/hornetq-ra/META-INF/ra.xml并注释掉以下部分:

 <config-property>
     <description>The transport configuration. These values must be in the form of key=val;key=val;</description>
     <config-property-name>ConnectionParameters</config-property-name>
     <config-property-type>java.lang.String</config-property-type>
     <config-property-value>server-id=0</config-property-value>
  </config-property>

显然离开server-id会导致部署时间异常。

  • 将以下jar复制到GF域lib文件夹:hornetq-core-client.jarhornetq-jms-client.jarhornetq-logging.jarnetty.jar
  • 为要与HornetQ一起使用的MDB创建一个xml描述符(sun-ejb-jar.xml):

  <ejb-name>MessageProcessor</ejb-name> <!-- MDB class name -->
  <jndi-name>ExampleMDB</jndi-name>
  <mdb-resource-adapter>
    <!-- The resource adapter mid element ties the generic ra for JMS
        with this particular MDB -->
    <resource-adapter-mid>hornetq-ra</resource-adapter-mid>
    <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>/queue/exampleQueue</activation-config-property-value>
      </activation-config-property>
      <activation-config-property>
        <activation-config-property-name>ConnectorClassName</activation-config-property-name>
        <activation-config-property-value>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</activation-config-property-value>
      </activation-config-property>
      <activation-config-property>
        <activation-config-property-name>ConnectionParameters</activation-config-property-name>
        <activation-config-property-value>host=hostname;port=5445</activation-config-property-value>
      </activation-config-property>
<!--
      <activation-config-property>
        <activation-config-property-name>UserName</activation-config-property-name>
        <activation-config-property-value>user</activation-config-property-value>
      </activation-config-property>
      <activation-config-property>
        <activation-config-property-name>Password</activation-config-property-name>
        <activation-config-property-value>pass</activation-config-property-value>
      </activation-config-property>
-->
    </activation-config>
  </mdb-resource-adapter>
</ejb>
  • 假设您的MDB看起来像这样,您应该收到消息:

@MessageDriven(mappedName = "ExampleMDB")
public class MessageProcessor implements MessageListener {

  public MessageProcessor() {
  }

  public void onMessage(Message message) {

    System.out.println("message received");

  }
}

答案 1 :(得分:1)

您正在尝试配置远程JMS提供程序。这里有一篇好文章

http://www.packtpub.com/article/configuring-jms-resources-in-glassfish-1

但是,我不确定它是否适用于HornetMQ,您可能必须使用OpenMQ的远程实例