简体   繁体   中英

Read multiple messages at a time from IBM MQ in java

I have a below code snippet which reads a message from IBM MQ. The message is getting retrieved and also the current depth.

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueReceiver;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;

public class HelloConsumer {

    /**
     * @param args
     */
    
    public HelloConsumer(){
        
        try {
            
            com.ibm.mq.MQQueue defaultLocalQueue;
            MQQueueManager qManager=null;
            
            MQEnvironment.hostname = "ctmq005";
            MQEnvironment.channel = "CLIENTCONNECTION";
            MQEnvironment.port = 1414;
            String qMngrStr = "";
            qManager = new MQQueueManager(qMngrStr); 
            int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE | MQC.MQ_Q_NAME_LENGTH; 
            String queueName="test.q01";
            System.out.println("accessing::"+queueName);
            defaultLocalQueue = qManager.accessQueue(queueName, openOptions);
            //set transport properties.
            System.out.println("set MQ props");
            MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
            System.out.println("new Queuemanager");
            //MQMessage putMessage = new MQMessage();
            //String msg = "hello";
           // putMessage.writeUTF(msg);
            
            //specify the message options...
            MQPutMessageOptions pmo = new MQPutMessageOptions(); 
            // accept 
            // put the message on the queue
            //defaultLocalQueue.put(putMessage, pmo);
              
            MQMessage getMessages = new MQMessage();
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            System.out.println("get messages::"+gmo.toString());
            defaultLocalQueue.get(getMessages, gmo);
            int depth = defaultLocalQueue.getCurrentDepth();
            byte[] b = new byte[getMessages.getMessageLength()]; 
            System.out.println(depth);
            getMessages.readUTF();
            System.out.println("Message got from MQ: "+new String(b));
        }
        catch(Exception jex){
            jex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        
        new HelloConsumer();

    }

This code reads only one message at a time from the Queue. I want to read multiple messages at a time from the Queue. For example, if there are 4 messages in the queue, I want to read all of them and so some processing for each message(processing code not attached here). Please suggest how to achieve this.

Why don't you create a simply method ie getAllMessages to do it for you and have it return an ArrayList of MQMessage class.

/**
 * Retrieve the messages as an array
 * @return msgs
 */
public ArrayList<MQMessage> getAllMessages(MQQueue inQ)
{
   private ArrayList<MQMessage> msgs = new ArrayList<MQMessage>();
   MQGetMessageOptions gmo = new MQGetMessageOptions();
   gmo.options = CMQC.MQGMO_NO_WAIT + CMQC.MQGMO_FAIL_IF_QUIESCING;

   MQMessage getMsg = null;
   boolean getMore = true;

   while(getMore)
   {
      try
      {
         getMsg = new MQMessage();
         inQ.get(getMsg, gmo);
         msgs.add(getMsg);
      }
      catch (MQException e)
      {
         if ( (e.completionCode == CMQC.MQCC_FAILED) &&
              (e.reasonCode == CMQC.MQRC_NO_MSG_AVAILABLE) )
         {
            getMore = false;
            break;
         }
         else
         {
            System.out.println("GET Exception: "+e.getMessage());
            System.out.println("getCause()="+e.getCause());
            getMore = false;
            break;
         }
      }

   }

   return msgs;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM