Issue reading all the messages from MQ from Java

sparker :

I have developed a Java application to read the messages from MQ. The Java application has to read all the messages from the MQ, put them in to the list and return the list. I am using while loop to read the messages one by one and breaking out if I caught the 2033 exception and return the list.

My issue is I am getting 2033 exception after reading the single message. For example, I have pushed some 10 messages to the queue and run my application, the first loop reads the first message and put it in to the list. But in the second loop it throwing 2033 exception while getting the second message. Then I need to run the application to read the second message and same thing happens.

Since I am new to MQ, I cant find the route cause why its happening. I am using Java8 and IBM MQ core library. Below is my code..

package com.reciever.mq;


import com.ibm.mq.*;
import com.ibm.mq.constants.MQConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;


public class MQReceiver {

    private static final Logger LOGGER = LoggerFactory.getLogger(MQReceiver.class);

    private static int GET_OPTIONS_CONSTANT = MQConstants.MQGMO_WAIT |
    MQConstants.MQGMO_PROPERTIES_COMPATIBILITY |
    MQConstants.MQGMO_ALL_SEGMENTS_AVAILABLE |
    MQConstants.MQGMO_COMPLETE_MSG |
    MQConstants.MQGMO_ALL_MSGS_AVAILABLE |
    MQConstants.MQGMO_SYNCPOINT;

    public static MQQueueManager queueManager;

    public static void main(String[] args) throws MQException {

        Hashtable<String, Object> mqOptions = new Hashtable<>();

        mqOptions.put(MQConstants.HOST_NAME_PROPERTY, "host.name"); //just a placeHolder
        mqOptions.put(MQConstants.CHANNEL_PROPERTY, "channelName");
        mqOptions.put(MQConstants.USER_ID_PROPERTY, "userName");
        mqOptions.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES);
        mqOptions.put(MQConstants.PORT_PROPERTY, 1980);

        queueManager = new MQQueueManager("queueManager", mqOptions);
        int options = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT
        | MQConstants.MQOO_FAIL_IF_QUIESCING | MQConstants.MQOO_PASS_ALL_CONTEXT | MQConstants.MQOO_INQUIRE;

        MQQueue mq = queueManager.accessQueue("queueNametoRead", options);
        MQException.logExclude(2033);

        MQMessage mqMessage = new MQMessage();
        MQGetMessageOptions getOptions = new MQGetMessageOptions();
        getOptions.options = GET_OPTIONS_CONSTANT;
        getOptions.waitInterval = 1;

        //byte[] buffer = null;

        List<byte[]> msgList = new ArrayList<>();

        System.out.println("Current depth : " + mq.getCurrentDepth());

        boolean hasMsges = true;

        while(hasMsges){

            byte[] buffer = null;

            try{

                mq.get(mqMessage, getOptions);

                buffer = new byte[mqMessage.getDataLength()];
                mqMessage.readFully(buffer);

                queueManager.commit();

                System.out.println("Recieved Message....");
                msgList.add(buffer);

            } catch (MQException e) {
                if((e.completionCode == MQConstants.MQCC_FAILED) &&
                        (e.reasonCode == MQConstants.MQRC_NO_MSG_AVAILABLE)){
                    System.out.println(" No more messages.. : " + e.getReason());

                    break;
                }else if (e.getReason() != 2033) {
                    LOGGER.error("Error getting message from the queue: " + "", e);
                }
            } catch (IOException e) {
                LOGGER.error("Error reading the message from the queue: " + "", e);
            }
            /* finally {
            if(mq != null) mq.close();
                if(queueManager != null) queueManager.disconnect();
            }*/

        }

        System.out.println("Final List size : " + msgList.size());

    }

}

Below are my sysouts after running the program

Current depth : 9
Recieved Message...
 No more messages.. : 2033
Final List size : 1

From the above output, its printed current depth is 9. But its breaking out during the second loop.

Morag Hughson :

The message ID and correlation ID fields are populated when a message is got from the queue. If you use the same MQMessage to get the next message then you must first reset those two fields. So just before you call the get method add these two lines.

 mqMessage.messageId     = MQConstants.MQMI_NONE;
 mqMessage.correlationId = MQConstants.MQCI_NONE;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=117217&siteId=1