WebSphere MQ JMS Messaging on Liberty

This article shows you how to do JMS messaging using Liberty and WebSphere MQ. The sample does the following:

  • Looks up a connection factory and a queue from JNDI
  • Sends a message to the queue
  • Reads the message back from the queue

This sample can be installed onto runtime versions 8.5.5.0 and later.

Instructions

Pre-requisites

For this sample we are assuming that:

Setup

Updating server.xml to point to the MQ Resource Adapter

  • Open the server.xml file and look for the following code:
        <variable name="wmqJmsClient.rar.location" value="${server.config.dir}/../wmq.wlp.rar"/>
  • This is used to point to the location of the MQ Resource Adapter. In this server.xml file, we are assuming the MQ Resource Adapter was placed in the wlp/usr/servers directory.

Update server.xml to point to your MQ configuration

  • Open the server.xml file and look for the following code:
        <jmsConnectionFactory jndiName="MyCF1" connectionManagerRef="ConMgr">
          <properties.wmqJms 
    	  hostName="localhost" 
    	  port="1414"
    	  channel="MQClient"
    	  queueManager="qm1"/>
        </jmsConnectionFactory>
        <connectionManager id="ConMgr" maxPoolSize="10"/>
        
        <jmsQueue id="MyQueue1" jndiName="MyQueue1">
          <properties.wmqJms baseQueueName="Q1" baseQueueManagerName="qm1"/>
        </jmsQueue> 
  • For the JMS Connection Factory the hostname, port, channel and queueManager properties should point to your MQ configuration. Likewise for the JMS Queue the baseQueueName and baseQueueManagerName should point to your MQ queue and queuemanager
  • Start the server

  • To start the server run the following command from the wlp/bin directory
        server start WMQClientSample
  • Test the servlet

    The servlet itself can be accessed via the URL http://localhost:9117/MQSample (assuming you are trying to access the servlet from your local machine and that you retain the httpEndpoint values specified in the server.xml). You should see output like the following:

    Initial context found!
    Sent message:
    
      JMSMessage class: jms_text
      JMSType:          null
      JMSDeliveryMode:  2
      JMSExpiration:    0
      JMSPriority:      4
      JMSMessageID:     ID:414d5120514d5f46524f4e54464f4f548ea7c95120002702
      JMSTimestamp:     1372252962959
      JMSCorrelationID: null
      JMSDestination:   queue://QM_FRONTFOOTLAPTOP/Q1
      JMSReplyTo:       null
      JMSRedelivered:   false
        JMSXAppID: jar                         
        JMSXDeliveryCount: 0
        JMSXUserID: pnickoll    
        JMS_IBM_PutApplType: 28
        JMS_IBM_PutDate: 20130626
        JMS_IBM_PutTime: 13224298
    Sample message - random ID 462
    Received message:
    
      JMSMessage class: jms_text
      JMSType:          null
      JMSDeliveryMode:  2
      JMSExpiration:    0
      JMSPriority:      4
      JMSMessageID:     ID:414d5120514d5f46524f4e54464f4f548ea7c95120002702
      JMSTimestamp:     1372252962959
      JMSCorrelationID: null
      JMSDestination:   queue://QM_FRONTFOOTLAPTOP/Q1
      JMSReplyTo:       null
      JMSRedelivered:   false
        JMSXAppID: jar                         
        JMSXDeliveryCount: 1
        JMSXUserID: pnickoll    
        JMS_IBM_Character_Set: UTF-8
        JMS_IBM_Encoding: 273
        JMS_IBM_Format: MQSTR   
        JMS_IBM_MsgType: 8
        JMS_IBM_PutApplType: 28
        JMS_IBM_PutDate: 20130626
        JMS_IBM_PutTime: 13224298
    Sample message - random ID 462
    SUCCESS

    Sample Structure

    • wlp
      • usr
        • servers
          • WMQClientSample
            • Copyright.txt
            • apps
              • wmq_servlet.war
            • readme.html
            • server.xml