以编程方式清除IBM MQ队列

问题描述 投票:1回答:2

有什么方法可以以编程方式清除IBM MQ Queue?我的队列中没有几则消息,但是当我使用消费者代码读取消息时,消息仍然存在于队列中。我假设队列中存在一些未提交的消息。我无权访问MQ Explorer,因此我想以编程方式清除队列。 (通过JMS代码或IBM MQ实现方式)

当前,我的使用者有jar文件com.ibm.mq-6.0.2.1.jar因此,我更喜欢使用WMQ类而不是JMS。

java jms ibm-mq
2个回答
0
投票

这是一个名为'EmptyQ.java'的功能齐全的Java / MQ程序,它将删除队列中的所有消息,直到队列为空。注意:这是我发布的here示例MQ / Java程序之一。

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);
   }
}

0
投票

这样简单的东西怎么样?

© www.soinside.com 2019 - 2024. All rights reserved.