client for remote JMS queue

2019-03-19 11:32发布

问题:

I have a JMS queue configured on remote glassfish server. I'm trying to connect this queue from my local machine. Is it possible to connect directly to this server or I need to connect via some broker/agent? How does it work? (I'm fresh in jms area) Many thanks

回答1:

If your client application is running outside Glassfish here is a simple code example for an open mq client.

To get it to work you will need to reference 2 openmq jars from the glassfishInstall/mq/lib directory - imq.jar and jms.jar

import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.ConnectionFactory;
import com.sun.messaging.Queue;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

public class TestJmsClientStandalone2 {

    public static void main( String[] args ) throws JMSException
    {
        ConnectionFactory connFactory = new ConnectionFactory();
        connFactory.setProperty(ConnectionConfiguration.imqAddressList, "remotehostip:7676");

        Queue myQueue = new Queue("myRemoteQueue");

        try (Connection connection = connFactory.createConnection(); 
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
                MessageProducer producer = session.createProducer(myQueue)) {

            Message message = session.createTextMessage("this is my test message");
            producer.send(message);
        }
    }
}


回答2:

Is your client application running in a local glassfish instance and trying to connect to remote glassfish instance's JMS resources?

If yes then I've found 2 ways to do this. For both options set up the same connection factory and destination (queue) JMS Resources in the remote and local glassfish instances.

1) Set the jms connection factory property "addressList"

In the clients glassfish admin console go to Resources->JMS Resources->Connection Factories->jms/YourConnectionFactory->Additional Properties

Add an additional property with the name addressList and value XX.XX.XX.XX:YYYY where the value is the IP address of the remote machine and port number which the JMS service is running on.

or

2) Set the client glassfish Java Message Service to connect to the remote glassfish

In the clients glassfish admin console go toConfigurations->server-config->Java Message Service

  • Set JMS Service Type: REMOTE (click save)
  • Set JMS Hosts->default_JMS_host IP Address and port to be the IP Address and port of the remote glassfish JMS service

I have tested both options to work with Glassfish 4. Hope it helps.



回答3:

I have no experience with Glassfish, but this scenario does work with JBoss (which integrates JBossMQ), and it should be generally applicable as well:

Server:

  • Server configuration: Create the queue, and bind it to a name to be visible in JNDI
  • Server configuration: Make sure that the connection factory is visible in JNDI as well

Client:

  • Lookup both the connection factory and the queue using JNDI. This possibly requires some values to be put into the properties for the InitialContext
  • On top of the connection factory and the queue, you can build the other objects (queue connection/session/receiver).

As for JBoss it looks like this:

    final Properties initialContextProperties = new Properties();
    initialContextProperties.put("java.naming.factory.initial",
            "org.jnp.interfaces.NamingContextFactory");
    initialContextProperties.put("java.naming.provider.url",
            "jnp://localhost:1099");

    //

    final InitialContext ic = new InitialContext(initialContextProperties);

    final QueueConnectionFactory qcf = (QueueConnectionFactory) ic
            .lookup("XAConnectionFactory");
    final Queue queue = (Queue) ic.lookup("queue/A");

So the broker/agent to go through is JNDI.