Tuesday, January 17, 2012

Standalone MQ Queue Consumer Code

This is small piece of code shows how to connect to MQ with help of MQQueueConnectionFactory and without using JNDI or any context lookups.
Code will create own factory instance and assign the values.
This is easy when you are using Java standalone application without using any application server.


This class also has a consumer example for implementation.




package com.xyz;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Session;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueueConnectionFactory;

public class MQDirectQueueConsumer2 {

 private String localQueueName;
 private String hostName;
 private int port;
 private String queueManagerName;
 private String channelName;

 private int sessionProperty = 0;
 private QueueConnection mqConnection = null;
 private QueueSession mqSession = null;

 private MQQueueConnectionFactory mqQCF = null;

 /**
  * MQDirectQueueConsumer2 constructor. Also calls connectToMQ() which returns QueueConnection
  * Object for further action.
  * @param hostName
  * @param port
  * @param queueManagerName
  * @param channelName
  * @param localQueueName
  * @throws Exception
  */
 public MQDirectQueueConsumer2(String hostName,
   int port,
   String queueManagerName,
   String channelName,
   String localQueueName) throws Exception {
  this.hostName = hostName;
  this.port = port;
  this.queueManagerName = queueManagerName;
  this.channelName = channelName;
  this.localQueueName = localQueueName;
  connectToMQ();

 }
 /**
  * connectToMQ() creates a connection with queue manager.
  * Make sure you have all necessary information to connect to QMGR
  * @return QueueConnection
  * @throws Exception
  */
 private QueueConnection connectToMQ() throws Exception {
  try {
   mqQCF = new MQQueueConnectionFactory();
   mqQCF.setHostName(hostName);
   mqQCF.setPort(port);
   mqQCF.setQueueManager(queueManagerName);
   mqQCF.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
   mqQCF.setChannel(channelName);
 
   mqConnection = mqQCF.createQueueConnection();
   if(sessionProperty == 0){
    mqSession = mqConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
   }else{
    mqSession = mqConnection.createQueueSession(true, sessionProperty);
   }
   mqConnection.start();
  } catch (JMSException e) {
   e.printStackTrace();
   throw new Exception("Exception in connectToMQ()",e);
  }
  return mqConnection;
 }

 /**
  * startConsumer() registers and starts the consumer for localQueue passed to constructor
  * @throws Exception
  */
 public void startConsumer() throws Exception{
  if(mqConnection == null){
   connectToMQ();
  }
  try{
    Destination queueToConsume    = mqSession.createQueue(localQueueName);
    MessageConsumer consumer = mqSession.createConsumer(queueToConsume);
    DirectQueueConsumer queueConsumer = new DirectQueueConsumer();
    mqConnection.setExceptionListener(queueConsumer);
    consumer.setMessageListener(queueConsumer);
   
  }catch(Exception exe){
   throw new Exception("Exception in initialize()"+exe);
  }
 }

 /**
  * Inner Static private class DirectQueueConsumer is
  * a MessageListener and ExceptionListener for localQueue
  * received message will be passed to onMessage() method provided.
  * @author THD
  *
  */
 private static class DirectQueueConsumer implements MessageListener, ExceptionListener
 {
  @Override
  public void onMessage(Message arg0) {
   System.out.println("Received Message :"+ arg0);
   // TODO Application Logic to handle the message
 
  }
  @Override
  public void onException(JMSException arg0) {
   // TODO Application Logic to handle the message
 
  }
 }
}

No comments:

Post a Comment