Royce
Royce

Reputation: 1595

MQException: MQJE001: Completion Code '2', Reason '2033' while trying to retreive a message from a queue

I try to catch a message from a queue. Note that the message has a xml content.

However, an exception is throw in the block catch (MQException e) while the message is correctly received by the queue.

MQException: MQJE001: Completion Code '2', Reason '2033'

Please find below code used:

import com.ibm.mq.MQMessage
import com.ibm.mq.MQGetMessageOptions
import com.ibm.mq.MQQueueManager
import com.ibm.mq.constants.CMQC
import com.ibm.mq.headers.MQRFH2
import com.ibm.MQException

def mqProps = new Hashtable<String, Object>()
mqProps.put(MQConstants.CHANNEL_PROPERTY, 'mychannel')
mqProps.put(MQConstants.PORT_PROPERTY, myport)
mqProps.put(MQConstants.HOST_NAME_PROPERTY, 'myhost')

def qMgr = new MQQueueManager('myqueuemanager', mqProps)
def openOptions = MQConstants.MQOO_OUTPUT | MQConstants.MQOO_INPUT_AS_Q_DEF

def gmo = new MQGetMessageOptions()
gmo.options = CMQC.MQGMO_WAIT + CMQC.MQGMO_FAIL_IF_QUIESCING

def queue = qMgr.accessQueue('myqueue', openOptions)
MQMessage receiveMsg = null
boolean getMore = true
while(getMore) {
    receiveMsg = new MQMessage()

    try {
        // get the message on the queue
        queue.get(receiveMsg, gmo)
        def rfh2 = new MQRFH2(receiveMsg)
        def strucLen = rfh2.getStrucLength()
        def encoding = rfh2.getEncoding()
        def CCSID = rfh2.getCodedCharSetId()
        def format = rfh2.getFormat()
        def flags = rfh2.getFlags()
        def nameValueCCSID = rfh2.getNameValueCCSID()

        def b = new byte[receiveMsg.getDataLength()]
        receiveMsg.readFully(b)
        System.out.println("Data: " + new String(b))
        queue.close()
   } catch (MQException e) {
      if (e.completionCode == CMQC.MQCC_FAILED && e.reasonCode == CMQC.MQRC_NO_MSG_AVAILABLE) {
         // No message - loop again
      }
      else {
         log.info("MQException: " + e.getLocalizedMessage())
         log.info("CC=" + e.completionCode + " : RC=" + e.reasonCode)
         getMore = false
      }
   } catch (IOException e) {
      log.info("IOException:" + e.getLocalizedMessage())
   }

}

Could you please help me to fix it? Thank you.

Upvotes: 0

Views: 4703

Answers (1)

Roger
Roger

Reputation: 7456

def openOptions = MQConstants.MQOO_OUTPUT | MQConstants.MQOO_INPUT_AS_Q_DEF

There is no reason to open the queue for both input and output. Also, you should include MQOO_FAIL_IF_QUIESCING.

def qMgr = new MQQueueManager('myqueuemanager', mqProps) def queue = qMgr.accessQueue('myqueue', openOptions)

I strongly recommend that you use UPPERCASE, queue manager and queue names. Also, it is an IBM Best Practise.

queue.close()

The close method should be called in the finally clause otherwise you could get memory leak.

I posted MQTEST12L.java (fully functioning) many times to StackOverflow. You can get it from here.

Use MQTest12L as an example on how to correctly code a Java application to get messages from a queue.

Upvotes: 3

Related Questions