Thursday, August 6, 2015

[Java] IBM MQ Listener/Reader

Below is a sample code to listen and read message in IBM Websphere MQ.


MQQueueConnection connection = null;

MQQueueConnectionFactory cf = new MQQueueConnectionFactory();

cf.setHostName("LOCALHOST");

cf.setPort(1414);
cf.setQueueManager("MYQMGR");
cf.setChannel("SYSTEM.DEF.SVRCONN");

//You can specify this WMQ_CM_CLIENT only if the channel is a server-connection channel

cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
connection = (MQQueueConnection) cf.createQueueConnection(<user>,<password>);

MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);


MQQueue queue1 = (MQQueue) session.createQueue("queue://MY.LOCAL.QUEUE");

// Start the connection
connection.start();

MQQueueReceiver rcvr = (MQQueueReceiver) session.createReceiver(queue1);

//Read remote queue.

while (isListening){
LOG.info(">>Is listening to a queue? ::: " + isListening);
LOG.info(">>Reading remote queue...");

TextMessage txtMsg = (TextMessage)rcvr.receive();
Thread.sleep(50);
 LOG.info(">> ::: " + (txtMsg.getText());

//Suspend.
synchronized(this) {
  while(suspended) {
wait();
}
 }
}


No comments:

Post a Comment