Wednesday, January 20, 2016

MQ java api code to retrieve all msgs

private void receiveAllMQmsgs()
{
   int openOptions = CMQC.MQOO_INQUIRE + CMQC.MQOO_INPUT_SHARED + CMQC.MQOO_FAIL_IF_QUIESCING;
   MQGetMessageOptions getOptions = new MQGetMessageOptions();
   getOptions.options = CMQC.MQGMO_NO_WAIT + CMQC.MQGMO_FAIL_IF_QUIESCING;
   boolean getMore = true;
   MQQueueManager qMgr = null;
   MQQueue queue = null;
   MQMessage receiveMsg = null;

   try
   {
      qMgr = new MQQueueManager(qManager);
      queue = qMgr.accessQueue(inputQName, openOptions);

      while(getMore)
      {
         try
         {
            receiveMsg = new MQMessage();
            queue.get(receiveMsg, getOptions);
            byte[] b = new byte[receiveMsg.getMessageLength()];
            receiveMsg.readFully(b);
            System.out.println("Message-->" + new String(b));
         }
         catch (MQException e)
         {
            if ( (e.completionCode == CMQC.MQCC_WARNING) &&
                 (e.reasonCode == CMQC.MQRC_NO_MSG_AVAILABLE) )
            {
               System.out.println("Bottom of the queue reached.");
               getMore = false;
            }
            else
            {
               System.err.println("MQRead CC=" +e.completionCode + " : RC=" + e.reasonCode);
               getMore = false;
            }
         }
         catch (IOException e)
         {
            System.out.println("MQRead " +e.getLocalizedMessage());
         }
      }
   }
   catch (MQException e)
   {
      System.err.println("MQRead CC=" +e.completionCode + " : RC=" + e.reasonCode);
   }
   finally
   {
      try
      {
         if (queue != null)
            queue.close();
      }
      catch (MQException e)
      {
         System.err.println("MQRead CC=" +e.completionCode + " : RC=" + e.reasonCode);
      }
      try
      {
         if (qMgr != null)
            qMgr.disconnect();
      }
      catch (MQException e)
      {
         System.err.println("MQRead CC=" +e.completionCode + " : RC=" + e.reasonCode);
      }
   }
}

No comments:

Post a Comment