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

发布于 2024-10-08 09:50:02 字数 1595 浏览 0 评论 0原文

在将必要的 hornet 特定 jar 添加到类路径后,我可以使用以下代码从部署在 Glassfish 上的 EJB 或 POJO 连接到 HornetMQ:

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();
    // ...

但我想从消息驱动 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”。

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

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:

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.

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?

Any ideas? Thank you!

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

@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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的奇迹 2024-10-15 09:50:03

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

,使 MDB 能够“交谈”到远程 HornetQ 提供商,请按照以下步骤操作:

  • 使用管理控制台将 hornetq-ra.rar 部署到 glassfish(您可以在 hornetq 文件夹的“libs”文件夹中找到它)
  • 找到 < gf_install_dir>/domains//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.jar 和 netty.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");

  }
}

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:

  • Deploy hornetq-ra.rar to 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.xml and comment out the following section:

 <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-id will cause an exception on deployment time.

  • Copy the following jars to GF domain lib folder: hornetq-core-client.jar, hornetq-jms-client.jar, hornetq-logging.jar and netty.jar
  • Create an xml descriptor for the MDB you want to use with HornetQ (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:

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

  public MessageProcessor() {
  }

  public void onMessage(Message message) {

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

  }
}
漫雪独思 2024-10-15 09:50:03

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

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

但是,我不确定它是否可以与 HornetMQ 一起使用,您可能必须使用 OpenMQ 的远程实例

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文