Wednesday, January 18, 2017

Sample java client for IBM Webspear MQ v8

The following sample java client can be used to produce the message to the IBM MQ.

 package test;  
 import com.ibm.msg.client.wmq.*;  
 import com.ibm.mq.jms.*;  
 import javax.jms.*;  
 public class WMQClient {  
       public static void main (String[] args)  
              throws Exception  
             {  
             // Create the QueueConnectionFactory  
             MQQueueConnectionFactory queueConnectionFactory = new MQQueueConnectionFactory();  
             //Configure the connection properties  
             queueConnectionFactory.setHostName ("localhost");  
             queueConnectionFactory.setPort (1414);  
             queueConnectionFactory.setQueueManager ("ESBQManager");  
             queueConnectionFactory.setChannel ("mychannel");  
             queueConnectionFactory.setTransportType (WMQConstants.WMQ_CM_CLIENT);  
             //  Started the queue connection  
             QueueConnection queueConnection = queueConnectionFactory.createQueueConnection ("mqm", "1qaz2wsx@");  
             queueConnection.start();  
             // Set JMS configuration for queue and session  
             Queue queue = new MQQueue ("LocalQueue1");  
             QueueSession session = queueConnection.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);  
             QueueSender sender = session.createSender (queue);  
             //Set the message/property and send to the queue  
             TextMessage message = session.createTextMessage("test");  
                message.setStringProperty("MyCustomProperty", "DUMMY_PROPERTY_VALUE");  
                System.err.println("Sending message:" + message.getText());  
                sender.send (message);  
             System.out.println ("sent message: " + message);   
             //close the session and connection  
             session.close();  
             queueConnection.close();  
             }  
 }  

After producing the messages by the client, the following listener proxy can be used to listen to the queue.

 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
     name="MyJMSProxy"  
     transports="jms"  
     statistics="disable"  
     trace="disable"  
     startOnLoad="true">  
   <target>  
    <inSequence>  
      <log level="full"/>  
      <log level="custom">  
       <property name="JMS_PROPERTY----"  
            expression="get-property('transport','MyCustomProperty')"/>  
      </log>  
      <drop/>  
    </inSequence>  
   </target>  
   <parameter name="transport.jms.Destination">LocalQueue1</parameter>  
   <description/>  
 </proxy>  

The output of the above client and the listener proxy can be seen as follows.

java client log

 Sending message:test  
 sent message:   
  JMSMessage class: jms_text  
  JMSType:     null  
  JMSDeliveryMode: 2  
  JMSDeliveryDelay: 0  
  JMSDeliveryTime: 1484743690101  
  JMSExpiration:  0  
  JMSPriority:   4  
  JMSMessageID:   ID:414d5120455342514d616e6167657220e40e7f5802650320  
  JMSTimestamp:   1484743690101  
  JMSCorrelationID: null  
  JMSDestination:  queue:///LocalQueue1  
  JMSReplyTo:    null  
  JMSRedelivered:  false  
   JMSXAppID: test.WMQClient         
   JMSXDeliveryCount: 0  
   JMSXUserID: mqm       
   JMS_IBM_PutApplType: 28  
   JMS_IBM_PutDate: 20170118  
   JMS_IBM_PutTime: 12481011  
   MyCustomProperty: DUMMY_PROPERTY_VALUE  
 test  

ESB carbon log 

 [2017-01-18 17:33:28,198] INFO - LogMediator To: , WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: ID:414d5120455342514d616e6167657220e40e7f5802fa0220, Direction: request, Envelope: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><axis2ns19:text xmlns:axis2ns19="http://ws.apache.org/commons/ns/payload">test</axis2ns19:text></soapenv:Body></soapenv:Envelope>  
 [2017-01-18 17:33:28,199] INFO - LogMediator JMS_PROPERTY---- = DUMMY_PROPERTY_VALUE  

No comments:

Post a Comment