MQSeries

##################################################################################################

MQ Queue Cleaner 

Below is the java code to retreive and clean the messages in the Queue.

import com.ibm.mq.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class queuclean
{

public queueclean()
{
}
try
{
mQMgr = new MQQueueManager(SERVER_NAME);
int openInputOptions = MQC.MQOO_FAIL_IF_QUIESCING |
MQC.MQOO_INPUT_SHARED | MQC.MQOO_BROWSE;

outputQueue=mQMgr.accessQueue(“QUEUE NAME“, openInputOptions);
MQMessage retrievedMessage = new MQMessage();
MQGetMessageOptions gmo=new MQGetMessageOptions();
gmo.options=MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_FIRST;
gmo.matchOptions=MQC.MQMO_NONE;
gmo.waitInterval=10000;

while(true) {
if( Msgcount>0 ) {
gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_NEXT;
}

outputQueue.get(retrievedMessage, gmo);
msg=retrievedMessage.readString(retrievedMessage.g etMessageLength());
System.out.println(“************************ My Message is “+c+” ********************** ” + retrievedMessage.persistence);
System.out.println(“RETRIEVED MESSAGE: “+msg);
System.out.println(“REMOVING…………………………….”);
gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_MSG_UNDER_CURSOR;
outputQueue.get(retrievedMessage, gmo);
c++;
}
}

catch (MQException mqe)
{

if(mqe.reasonCode == mqe.MQRC_NO_MSG_AVAILABLE)
{
System.out.println(“NO MORE MESSAGES AVAILABLE, RETRIEVED”+c );
return;
}
mqe.printStackTrace();
}
catch (java.io.IOException ioe)
{
ioe.printStackTrace(System.out);
return;
}

finally
{
try
{
System.out.println(“CLOSING A QUEUE & MANAGER”);
if(outputQueue != null) { outputQueue.close(); }

if(mQMgr != null) { mQMgr.disconnect(); }
}

catch (MQException ex) {
ex.printStackTrace();
}
}

 

Leave a comment