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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4487615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:35:45  来源:igfitidea点击:

connecting to a remote JMS provider from a Message Driven Bean (MDB)

javajakarta-eejmsglassfish-3

提问by Bogdan

From an EJB or a POJO deployed on Glassfish I can connect to HornetMQ with the following code, after I add to classpath the necessary hornet specific jars:

从部署在 Glassfish 上的 EJB 或 POJO 中,我可以使用以下代码连接到 HornetMQ,在我将必要的 hornet 特定 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();
    // ...

But I want to do the same from a Message Driven Bean.

但我想从消息驱动的 Bean 中做同样的事情。

With a MDB it's very easy if I use the embedded Glassfish provider; but how do I configure GF to use a remote provider?

有了 MDB,如果我使用嵌入式 Glassfish 提供程序就很容易了;但是如何配置 GF 以使用远程提供程序?

Any ideas? Thank you!

有任何想法吗?谢谢!

EDIT: to make things a little clearer; a typical MDB looks something like this:

编辑:让事情更清楚一点;一个典型的 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) {

    }
}

But in this case, the MDB will look for "/queue/exampleQueue" on the local server not the remote one.

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

Basically my question is how do I configure GF to look for the remote server (as in the first snippet) when using a MDB?

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

回答by Bogdan

After some more digging I found a solution on these forums

经过更多挖掘,我在这些论坛上找到了解决方案

To enable a MDB to "talk" to a remote HornetQ provider follow these steps:

要使 MDB 能够与远程 HornetQ 提供者“交谈”,请执行以下步骤:

  • Deploy hornetq-ra.rarto glassfish using admin console (you'll find it in the "libs" folder in the hornetq folder)
  • find <gf_install_dir>/domains/<your_dmain>/applications/hornetq-ra/META-INF/ra.xmland comment out the following section:
  • 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>


Apparently leaving server-idwill cause an exception on deployment time.

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

  • Copy the following jars to GF domain lib folder: hornetq-core-client.jar, hornetq-jms-client.jar, hornetq-logging.jarand netty.jar
  • Create an xml descriptor for the MDB you want to use with HornetQ (sun-ejb-jar.xml):
  • 将以下 jars 复制到 GF 域 lib 文件夹:hornetq-core-client.jar, hornetq-jms-client.jar,hornetq-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>
  • Assuming your MDB looks smth like this, you should be receiving messages:
  • 假设您的 MDB 看起来像这样,您应该会收到消息:


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

  public MessageProcessor() {
  }

  public void onMessage(Message message) {

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

  }
}

回答by Preston

You're trying to configure a remote JMS provider. There's a good article here

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

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

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

However, I'm not sure it will work with HornetMQ, you might have to use a remote instance of OpenMQ

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