Java – Programmatically clear IBM MQ queues

Programmatically clear IBM MQ queues… here is a solution to the problem.

Programmatically clear IBM MQ queues

Is there a way to programmatically clear the IBM MQ Queue? I have few messages in the queue, but when I read the messages using Consumer code, the messages are still in the queue. I assume there are some uncommitted messages in the queue. I don’t have access to MQ Explorer, so I want to clear the queue programmatically. (implemented in JMS code or IBM MQ

).

Currently my consumer has jar file com.ibm.mq-6.0.2.1.jar so I prefer to use WMQ classes instead of JMS.

Solution

This is a full-featured Java/MQ program called “EmptyQ.java” that deletes all messages in the queue until the queue is empty. Note: This is one of the sample MQ/Java programs I published here

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;

/**
 * Program Name: 
 *  EmptyQ
 *
 * Description: 
 * This java class will connect to a remote queue manager with the
 * MQ setting stored in a HashTable, loop to retrieve (delete) all messages from
 * a queue then close and disconnect.
 *
 * Sample Command Line Parameters: 
 * bindings mode: 
 *  -m MQA1 -q TEST. Q1 
 *  
 * client mode:
 *  -m MQA1 -q TEST. Q1 -h 127.0.0.1 -p 1414 -c TEST. CHL -u UserID -x Password
 *
 * @author Roger Lacroix
 */
public class EmptyQ
{
   private static final SimpleDateFormat LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss. SSS");

private Hashtable<String, String> params;
   private Hashtable<String, Object> mqht;

/**
    * The constructor
    */
   public EmptyQ()
   {
      super();
      params = new Hashtable<String, String>();
      mqht = new Hashtable<String, Object>();
   }

/**
    * Make sure the required parameters are present.
    * 
    * @return true/false
    */
   private boolean allParamsPresent()
   {
      boolean b = params.containsKey("-m") && params.containsKey("-q");

if (params.containsKey("-c"))
      {
         b = b && params.containsKey("-c") && params.containsKey("-h") && params.containsKey("-p");
      }

if (b)
      {
         try
         {
            if (params.containsKey("-p"))
               Integer.parseInt((String) params.get("-p"));
         }
         catch (NumberFormatException e)
         {
            b = false;
         }
      }

return b;
   }

/**
    * Extract the command-line parameters and initialize the MQ HashTable.
    * 
    * @param args
    * @throws IllegalArgumentException
    */
   private void init(String[] args) throws IllegalArgumentException
   {
      int port = 1414;
      if (args.length > 0 && (args.length % 2) == 0)
      {
         for (int i = 0; i < args.length; i += 2)
         {
            params.put(args[i], args[i + 1]);
         }
      }
      else
      {
         throw new IllegalArgumentException();
      }

if (allParamsPresent())
      {
         if (params.containsKey("-c"))
         {
            try
            {
               port = Integer.parseInt((String) params.get("-p"));
            }
            catch (NumberFormatException e)
            {
               port = 1414;
            }

mqht.put(CMQC. CHANNEL_PROPERTY, params.get("-c"));
            mqht.put(CMQC. HOST_NAME_PROPERTY, params.get("-h"));
            mqht.put(CMQC. PORT_PROPERTY, new Integer(port));
            if (params.containsKey("-u"))
               mqht.put(CMQC. USER_ID_PROPERTY, params.get("-u"));
            if (params.containsKey("-x"))
               mqht.put(CMQC. PASSWORD_PROPERTY, params.get("-x"));
         }

 I don't want to see MQ exceptions at the console.
         MQException.log = null;
      }
      else
      {
         throw new IllegalArgumentException();
      }
   }

/**
    * Connect, open queue, loop and get all messages then close queue and
    * disconnect.
    *
    */
   private void receive()
   {
      String qMgrName = (String) params.get("-m");
      String inputQName = (String) params.get("-q");
      MQQueueManager qMgr = null;
      MQQueue queue = null;
      int openOptions = CMQC. MQOO_INPUT_AS_Q_DEF + CMQC. MQOO_INQUIRE + CMQC. MQOO_FAIL_IF_QUIESCING;
      MQGetMessageOptions gmo = new MQGetMessageOptions();
      gmo.options = CMQC. MQGMO_FAIL_IF_QUIESCING + CMQC. MQGMO_ACCEPT_TRUNCATED_MSG;
      MQMessage receiveMsg = null;
      int msgCount = 0;
      boolean getMore = true;

try
      {
         if (params.containsKey("-c"))
            qMgr = new MQQueueManager(qMgrName, mqht);
         else
            qMgr = new MQQueueManager(qMgrName);
         EmptyQ.logger("successfully connected to " + qMgrName);

queue = qMgr.accessQueue(inputQName, openOptions);
         EmptyQ.logger("successfully opened " + inputQName);

while (getMore)
         {
            receiveMsg = new MQMessage();

try
            {
                get the message on the queue - request only 1 byte - make it go as fast as possible.
               queue.get(receiveMsg, gmo, 1);
               msgCount++;
            }
            catch (MQException e)
            {
               if ( (e.completionCode == CMQC. MQCC_FAILED) && 
                    (e.reasonCode == CMQC. MQRC_NO_MSG_AVAILABLE) )
               {
                   All messages read.
                  getMore = false;
                  break;
               }
               else if ( (e.completionCode == CMQC. MQCC_WARNING) && 
                         (e.reasonCode == CMQC. MQRC_TRUNCATED_MSG_ACCEPTED) )
               {
                  msgCount++;
               }
               else
               {
                  EmptyQ.logger("MQException: " + e.getLocalizedMessage());
                  EmptyQ.logger("CC=" + e.completionCode + " : RC=" + e.reasonCode);
                  getMore = false;
                  break;
               }
            }
         }
      }
      catch (MQException e)
      {
         EmptyQ.logger("CC=" + e.completionCode + " : RC=" + e.reasonCode);
      }
      finally
      {
         EmptyQ.logger("deleted " + msgCount + " messages");

try
         {
            if (queue != null)
            {
               queue.close();
               EmptyQ.logger("closed: " + inputQName);
            }
         }
         catch (MQException e)
         {
            EmptyQ.logger("CC=" + e.completionCode + " : RC=" + e.reasonCode);
         }
         try
         {
            if (qMgr != null)
            {
               qMgr.disconnect();
               EmptyQ.logger("disconnected from " + qMgrName);
            }
         }
         catch (MQException e)
         {
            EmptyQ.logger("CC=" + e.completionCode + " : RC=" + e.reasonCode);
         }
      }
   }

/**
    * A simple logger method
    * 
    * @param data
    */
   public static void logger(String data)
   {
      String className = Thread.currentThread().getStackTrace()[2].getClassName();

 Remove the package info.
      if ((className != null) && (className.lastIndexOf('.') != -1))
         className = className.substring(className.lastIndexOf('.') + 1);

System.out.println(LOGGER_TIMESTAMP.format(new Date()) + " " + className + ": " + Thread.currentThread().getStackTrace()[2].getMethodName() + ": " + data);
   }

/**
    * main line
    * 
    * @param args
    */
   public static void main(String[] args)
   {
      EmptyQ write = new EmptyQ();

try
      {
         write.init(args);
         write.receive();
      }
      catch (IllegalArgumentException e)
      {
         System.err.println("Usage: java EmptyQ -m QueueManagerName -q QueueName [-h host -p port -c channel] [-u UserID] [-x Password]");
         System.exit(1);
      }

System.exit(0);
   }
}

Related Problems and Solutions