Search code examples
javaglassfishjmsglassfish-3

client for remote JMS queue


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


Solution

  • 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);
            }
        }
    }