【问题标题】:I need to browse through IBM MQ and get a particular type of message and then remove the message from Queue我需要浏览 IBM MQ 并获取特定类型的消息,然后从队列中删除该消息
【发布时间】:2019-09-04 02:05:51
【问题描述】:

使用以下帖子中提供的解决方案时:Browse, read, and remove a message from a queue using IBM MQ classes

我得到以下错误 原因:com.ibm.mq.jmqi.JmqiException: CC=2;RC=2495;AMQ8568: 未找到本机 JNI 库 'mqjbnd64'。对于客户端安装,这是预期的。 [3=mqjbnd64]

我是 IBM MQ 的新手,但我认为它正在尝试在我的本地机器上查找 MQ 库,但我想在远程服务器上连接,因为我的机器上没有安装 MQ

【问题讨论】:

  • 对于在本地计算机上运行的应用程序,您仍然需要应用程序使用的 MQ 客户端库。不幸的是,我认为您指出的代码使用了绑定模式,因此只能在安装了 MQ 的机器上运行。您需要从在客户端模式下运行的示例应用程序开始。有几种可用。向下滚动页面以获取以下链接,然后选择适当的语言 API - developer.ibm.com/messaging/learn-mq/mq-tutorials/…

标签: java ibm-mq


【解决方案1】:

这是一个 Java/MQ 程序,它将连接到 (1) 远程队列管理器(客户端模式)或 (2) 本地队列管理器(绑定模式),打开队列,循环以从队列中检索所有消息然后关闭并断开连接。

import java.io.EOFException;
import java.io.IOException;
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
 *  MQTest12L
 *
 * Description
 *  This java class will connect to a remote queue manager with the
 *  MQ setting stored in a HashTable, loop to retrieve 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 MQTest12L
{
   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 MQTest12L()
   {
      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 testReceive()
   {
      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_WAIT + CMQC.MQGMO_FAIL_IF_QUIESCING;
      gmo.waitInterval = 5000;  // wait up to 5 seconds for a message.
      MQMessage receiveMsg = null;
      int msgCount = 0;
      boolean getMore = true;

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

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

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

            try
            {
               // get the message on the queue
               queue.get(receiveMsg, gmo);
               msgCount++;

               if (CMQC.MQFMT_STRING.equals(receiveMsg.format))
               {
                  String msgStr = receiveMsg.readStringOfByteLength(receiveMsg.getMessageLength());
//                  MQTest12L.logger("["+msgCount+"] " + msgStr);
               }
               else
               {
                  byte[] b = new byte[receiveMsg.getMessageLength()];
                  receiveMsg.readFully(b);
//                  MQTest12L.logger("["+msgCount+"] " + new String(b));
               }
            }
            catch (MQException e)
            {
               if ( (e.completionCode == CMQC.MQCC_FAILED) &&
                    (e.reasonCode == CMQC.MQRC_NO_MSG_AVAILABLE) )
               {
                  // All messages read.
                  getMore = false;
                  break;
               }
               else
               {
                  MQTest12L.logger("MQException: " + e.getLocalizedMessage());
                  MQTest12L.logger("CC=" + e.completionCode + " : RC=" + e.reasonCode);
                  getMore = false;
                  break;
               }
            }
            catch (IOException e)
            {
               MQTest12L.logger("IOException:" +e.getLocalizedMessage());
            }
         }
      }
      catch (MQException e)
      {
         MQTest12L.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
      }
      finally
      {
         MQTest12L.logger("read " + msgCount + " messages");

         try
         {
            if (queue != null)
            {
               queue.close();
               MQTest12L.logger("closed: "+ inputQName);
            }
         }
         catch (MQException e)
         {
            MQTest12L.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
         }
         try
         {
            if (qMgr != null)
            {
               qMgr.disconnect();
               MQTest12L.logger("disconnected from "+ qMgrName);
            }
         }
         catch (MQException e)
         {
            MQTest12L.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)
   {
      MQTest12L write = new MQTest12L();

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

      System.exit(0);
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多