简体   繁体   中英

Unable to retrieve message from MQ Queue

I am trying to retrieve message from MQ Queue. Below is the code I am using:

    //Fetching Queue details
    MQEnvironment.hostname=getMqCall().getMqHost();
    MQEnvironment.channel=getMqCall().getMqChannel();
    MQEnvironment.port=getMqCall().getMqPort();
    MQQueueManager qMgr= new MQQueueManager(getMqCall().getMqQueManager());
    MQQueue outputQueue = qMgr.accessQueue(getMqCall().getRespQueue(), MQC.MQOO_INQUIRE | MQC.MQOO_BROWSE | MQC.MQOO_INPUT_AS_Q_DEF);

    //Checking for messages in queue
    int i=outputQueue.getCurrentDepth();
    System.out.println("Number of messages in Assign promo queue:: "+i);

    for(int j=0;j<i;j++){
    MQMessage sampleResponse = new MQMessage();
    MQGetMessageOptions gmo = new MQGetMessageOptions();
    outputQueue.get(sampleResponse, gmo);
    String msgtext = sampleResponse.readStringOfCharLength(sampleResponse.getMessageLength());

    System.out.println("Message read is: "+msgtext);
    System.out.println("Response length: "+sampleResponse.getTotalMessageLength());

    if(msgtext!=null){

    // Some code

    }else{

    System.out.println("Message not read from response queue");

    }

    }

    //Checking for messages in queue after processing
    i=outputQueue.getCurrentDepth();
    System.out.println("Number of messages in queue after processing :: "+i);

    outputQueue.close();
    qMgr.close();
    qMgr.disconnect();

I get msgtext value as null, even though getCurrentDepth() shows me that values are present in the queue. Even the getTotalMessageLength() returns null value. Have verified that messages are present on that queue.

Earlier I was using readString() method in the place of readStringOfCharLength() . However, I was still not getting the response, so I checked with readStringOfCharLength() as readString() method is deprecated.

I have even tried with readLine() method, but still not able to retrieve the message.

Thanks in advance.

PS: I am running the above piece of code via a scheduler which runs every 15 mins.

Never EVER setup a loop based on the current depth of the queue. There are many reasons never to do this and is documented in IBM's MQ Standards document.

Secondly, make sure you catch the MQExceptions thrown by MQ. Otherwise, you will post messages saying "where's my message". The most important thing about an MQException is the Reason Code. All MQ Reason Codes are documented in the MQ Knowledge Center. Bookmark the link and get use to looking up reason codes.

Finally, here is a properly structured Java/MQ program:

private void testReceive()
{
   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);
      }
   }
}

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