Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browse, read, and remove a message from a queue using IBM MQ classes

Tags:

java

queue

ibm-mq

I'm writing a simple Java application using MQ classes for Java with Eclipse.

Right now I'm able to browse a remote queue without removing the messages stored.
Here is the code of the reading cycle:

MQQueueManager QMgr = new MQQueueManager(qManager); //<-- qManager is a String with the QMgr name

int openOptions = MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INPUT_SHARED | MQC.MQOO_BROWSE;  

MQQueue queue = QMgr.accessQueue(queueName, openOptions);

MQMessage theMessage    = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
    gmo.options=MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_FIRST;
    gmo.matchOptions=MQC.MQMO_NONE;
    gmo.waitInterval=5000;

boolean thereAreMessages=true;
while(thereAreMessages){
    try{
        //read the message          
        queue.get(theMessage,gmo);  
        //print the text            
        String msgText = theMessage.readString(theMessage.getMessageLength());
        System.out.println("msg text: "+msgText);

                 // <--- Solution code Here

        //move cursor to the next message               
        gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_NEXT;

    }catch(MQException e){

        if(e.reasonCode == e.MQRC_NO_MSG_AVAILABLE) {
            System.out.println("no more message available or retrived");
        }

        thereAreMessages=false;
    } catch (IOException e) {
        System.out.println("ERROR: "+e.getMessage());
    }
}

Main question: After the read message line and before moving the cursor to the next message how can I remove the message from the queue?

Secondary question: Eclispe is warning me that all the costants used for the options are deprecated; which are the correct ones to use?


Solution:

Here the solution I'm really looking for:

// set te cursor to remove the message from the queue
gmo.options = CMQC.MQGMO_MSG_UNDER_CURSOR;
queue.get(theMessage, gmo);

these lines have to be inserted in the question code

I've found it here: http://www.velocityreviews.com/forums/t124676-mq-series-messages-browse-and-delete.html

like image 994
Mave751 Avatar asked Sep 02 '12 13:09

Mave751


People also ask

How do I delete a single message in MQ queue?

In MQEdit you can browse a list of messages on a queue, then from that list, select an individual message (or multiple messages if you need to). Then press the Delete button, or select Delete from the right-mouse button context menu.

How do I move messages from one queue to another in MQ?

From the Navigation Panel, navigate to the relevant queue manager. Click the arrow icon to the left of the relevant queue, and from the displayed list of options select Move Messages. Alternatively, browse the relevant queue and select the Move Messages option from the Messages view.


1 Answers

Solution:

Here the solution I'm really looking for:

// set te cursor to remove the message from the queue
gmo.options = CMQC.MQGMO_MSG_UNDER_CURSOR;
queue.get(theMessage, gmo);

these lines have to be inserted in the question code

I've found it here: http://www.velocityreviews.com/forums/t124676-mq-series-messages-browse-and-delete.html

like image 124
Mave751 Avatar answered Oct 06 '22 20:10

Mave751