简体   繁体   中英

connecting to a remote JMS provider from a Message Driven Bean (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?

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");

  }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM