Topic
  • 5 replies
  • Latest Post - ‏2013-09-10T05:09:25Z by Lohitashwa
pgr9
pgr9
5 Posts

Pinned topic HelloWorld JMS client, connecting to embedded Liberty JMS server

‏2013-09-03T18:28:29Z |

I would like to author a "hello world" JMS client application, which talks to a properly configured Liberty 8.5.5.0 server.  I have found several related examples, and am successfully publishing / subscribing to a JMS Topic from within the running server. 

I have not found anything that helps me to configure the standalone JMS client.  I am migrating away from ActiveMQ, where I use JNDI lookups to obtain references to the Destination and the ConnectionFactory.  Is this the right way to do it in WLP?

Looking for code something like this:

    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.setProperty("java.naming.factory.initial", "com.ibm.ws.jndi.internal.WSContextFactory");
        properties.setProperty("java.naming.provider.url", "tcp://localhost:7276");

        InitialContext ctx = new InitialContext(properties);
        System.out.println("ctx = " + ctx);

        Destination destination = (Destination) ctx.lookup("jms/libertyTopic");
        System.out.println(destination);

        ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
        System.out.println(factory);

        Connection connection = factory.createConnection();
        System.out.println(connection);

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        logger.info(session);

        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(new TopicListener());
        connection.start();

        while (true) {
            try {
                logger.info("SLEEPING");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
 

(1) What libraries need to be in the classpath of the client application?

(2) What properties are needed by the InitialContext in order to make the connection?

(3) What corresponding "server.xml" configuration is needed in the server?

 

  • Stu Robertson
    Stu Robertson
    4 Posts

    Re: HelloWorld JMS client, connecting to embedded Liberty JMS server

    ‏2013-09-05T17:36:30Z  

    We have this same question.

  • pgr9
    pgr9
    5 Posts

    Re: HelloWorld JMS client, connecting to embedded Liberty JMS server

    ‏2013-09-09T14:59:23Z  

    We have this same question.

    Was able to break down problem into a few pieces.

    (1) what jars should be used on the client side?
    (2) What configuration should be used on the server side?
    (3) How should connection factory be instantiated?
    (4) How to put it all together?

    For (1), I found this post.  (You'll need to obtain the jars from a full Websphere install.)

    https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014940241#77777777-0000-0000-0000-000014940609

    For (2), I installed the JMS test applications from these links into my WLP 8.5.5 instance.  (It was helpful to see any sort of messaging working.)

    https://www.ibmdw.net/wasdev/repo/productsample_jmssampleclient/
    https://www.ibmdw.net/wasdev/repo/productsample_jmssampleserver/

    For (3), this link pointed the way.  (JNDI / InitialContext is a dead end; don't go there.)

    http://pic.dhe.ibm.com/infocenter/wasinfo/v8r5/index.jsp?topic=%2Fcom.ibm.websphere.nd.doc%2Fae%2Ftjj_jmsthcli_connf.html

    For (4), here is the code I came up with.  I can see messages in SampleClient sent via this code, and I can see messages in this code sent by SampleClient.

    [code]
    package thinclient;

    import com.ibm.websphere.sib.api.jms.JmsConnectionFactory;
    import com.ibm.websphere.sib.api.jms.JmsFactoryFactory;

    import javax.jms.*;

    public class ThinClient3 {

        public static void main(String[] args) throws Exception {
            JmsFactoryFactory jmsFactoryFactory = JmsFactoryFactory.getInstance();
            JmsConnectionFactory connectionFactory = jmsFactoryFactory.createConnectionFactory();
            connectionFactory.setProviderEndpoints("localhost:9126");
            connectionFactory.setBusName("bus-name-does-not-matter");
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Destination destination = jmsFactoryFactory.createQueue("QUEUE1");
            if (true) {
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(destination);
                TextMessage hello = session.createTextMessage("hello");
                producer.send(hello);
                session.close();
            }
            if (true) {
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageConsumer consumer = session.createConsumer(destination);
                Message receive = consumer.receiveNoWait();
                while (receive != null) {
                    if (receive instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) receive;
                        System.out.println("textMessage.getText() = " + textMessage.getText());
                    } else {
                        System.out.println("message = " + receive.getClass().getName());
                    }
                    receive = consumer.receiveNoWait();
                }
                session.close();
            }
            connection.stop();
            connection.close();
        }
    }
    [/code]

    Cheers...
     

  • Lohitashwa
    Lohitashwa
    2 Posts

    Re: HelloWorld JMS client, connecting to embedded Liberty JMS server

    ‏2013-09-09T16:20:21Z  
    • pgr9
    • ‏2013-09-09T14:59:23Z

    Was able to break down problem into a few pieces.

    (1) what jars should be used on the client side?
    (2) What configuration should be used on the server side?
    (3) How should connection factory be instantiated?
    (4) How to put it all together?

    For (1), I found this post.  (You'll need to obtain the jars from a full Websphere install.)

    https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014940241#77777777-0000-0000-0000-000014940609

    For (2), I installed the JMS test applications from these links into my WLP 8.5.5 instance.  (It was helpful to see any sort of messaging working.)

    https://www.ibmdw.net/wasdev/repo/productsample_jmssampleclient/
    https://www.ibmdw.net/wasdev/repo/productsample_jmssampleserver/

    For (3), this link pointed the way.  (JNDI / InitialContext is a dead end; don't go there.)

    http://pic.dhe.ibm.com/infocenter/wasinfo/v8r5/index.jsp?topic=%2Fcom.ibm.websphere.nd.doc%2Fae%2Ftjj_jmsthcli_connf.html

    For (4), here is the code I came up with.  I can see messages in SampleClient sent via this code, and I can see messages in this code sent by SampleClient.

    [code]
    package thinclient;

    import com.ibm.websphere.sib.api.jms.JmsConnectionFactory;
    import com.ibm.websphere.sib.api.jms.JmsFactoryFactory;

    import javax.jms.*;

    public class ThinClient3 {

        public static void main(String[] args) throws Exception {
            JmsFactoryFactory jmsFactoryFactory = JmsFactoryFactory.getInstance();
            JmsConnectionFactory connectionFactory = jmsFactoryFactory.createConnectionFactory();
            connectionFactory.setProviderEndpoints("localhost:9126");
            connectionFactory.setBusName("bus-name-does-not-matter");
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Destination destination = jmsFactoryFactory.createQueue("QUEUE1");
            if (true) {
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(destination);
                TextMessage hello = session.createTextMessage("hello");
                producer.send(hello);
                session.close();
            }
            if (true) {
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageConsumer consumer = session.createConsumer(destination);
                Message receive = consumer.receiveNoWait();
                while (receive != null) {
                    if (receive instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) receive;
                        System.out.println("textMessage.getText() = " + textMessage.getText());
                    } else {
                        System.out.println("message = " + receive.getClass().getName());
                    }
                    receive = consumer.receiveNoWait();
                }
                session.close();
            }
            connection.stop();
            connection.close();
        }
    }
    [/code]

    Cheers...
     

    Hi,

    Thanks for posting the code snippet and other links.. Currently, Liberty does not support remote JNDI server, hence stand alone applications cannot do a lookup for JMS resources within the Liberty profile. Hence, as ,mentioned in the above code snippet,  it is correct to use the JmsFactoryFactory instance to create the required factory objects.

            JmsFactoryFactory jmsFactoryFactory = JmsFactoryFactory.getInstance();
            JmsConnectionFactory connectionFactory = jmsFactoryFactory.createConnectionFactory();

     

    Let me know if you are encountering any others issues while connecting to Liberty messaging and I will be happy to assist you further.

    Thanks

    Lohit

  • pgr9
    pgr9
    5 Posts

    Re: HelloWorld JMS client, connecting to embedded Liberty JMS server

    ‏2013-09-09T16:53:30Z  

    Hi,

    Thanks for posting the code snippet and other links.. Currently, Liberty does not support remote JNDI server, hence stand alone applications cannot do a lookup for JMS resources within the Liberty profile. Hence, as ,mentioned in the above code snippet,  it is correct to use the JmsFactoryFactory instance to create the required factory objects.

            JmsFactoryFactory jmsFactoryFactory = JmsFactoryFactory.getInstance();
            JmsConnectionFactory connectionFactory = jmsFactoryFactory.createConnectionFactory();

     

    Let me know if you are encountering any others issues while connecting to Liberty messaging and I will be happy to assist you further.

    Thanks

    Lohit

    This response would have been more helpful 6 days ago, but thanks.

  • Lohitashwa
    Lohitashwa
    2 Posts

    Re: HelloWorld JMS client, connecting to embedded Liberty JMS server

    ‏2013-09-10T05:09:25Z  
    • pgr9
    • ‏2013-09-09T16:53:30Z

    This response would have been more helpful 6 days ago, but thanks.

    Apologies, I was not getting the notification for some reason and did not notice this query.. I have rectified the notification problem and will be monitoring for any further queries. .Please feel free to reach out to me for any messaging related queries.. Thanks

    Lohit