Websphere 7 MQueue:如何从 Java 访问队列深度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7759113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:18:15  来源:igfitidea点击:

Websphere 7 MQueue: how to access queue depth from Java?

javawebspheremessagingibm-mq

提问by jeff porter

I'd like to write some code to monitor the queue size on Websphere 7 MQ. This is the code I've come up with

我想编写一些代码来监视 Websphere 7 MQ 上的队列大小。这是我想出的代码

   MQEnvironment.hostname = "10.21.1.19"; 
   MQEnvironment.port = 1414;
   MQEnvironment.channel = "SYSTEM.CDEF.SVRCONN";
   MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);

   MQQueueManager qMgr = new MQQueueManager("MYQMGR");

   MQQueue destQueue = qMgr.accessQueue("PUBLISH", MQC.MQOO_INQUIRE);
   System.out.println(destQueue.getCurrentDepth());
   destQueue.close();
   qMgr.disconnect();

How do I know what the "Channel" is?

我怎么知道“频道”是什么?

How do I know what the queue manager name is that I pass into MQQueueManager?

我如何知道我传递给 MQQueueManager 的队列管理器名称是什么?

Or is there another API that I should look at?

或者我应该查看其他 API 吗?

I need it work with WRS 7 SIB and MQ.

我需要它与 WRS 7 SIB 和 MQ 一起使用。

Thanks Jeff Porter

感谢杰夫·波特

回答by jeff porter

I used the jars from WS 7.0.1.1

我使用了 WS 7.0.1.1 中的罐子

com.ibm.mq.jar com.ibm.mq.jmqi.jar com.ibm.mq.jmqi.system.jar com.ibm.mq.commonservices.jar com.ibm.mq.headers..jar com.ibm.mq.jmqi.remote.jar

com.ibm.mq.jar com.ibm.mq.jmqi.jar com.ibm.mq.jmqi.system.jar com.ibm.mq.commonservices.jar com.ibm.mq.headers..jar com.ibm。 mq.jmqi.remote.jar

I got the Queue Manager name and the Channel name from "IBM Webshpere MQ Explorer" (Client Connection node in the tree)

我从“IBM Webshpere MQ Explorer”(树中的客户端连接节点)获得了队列管理器名称和通道名称

    import com.ibm.mq.MQEnvironment;
    import com.ibm.mq.MQQueue;
    import com.ibm.mq.MQQueueManager;
    import com.ibm.mq.constants.CMQC;
    int openOptions = CMQC.MQOO_INQUIRE + CMQC.MQOO_FAIL_IF_QUIESCING + CMQC.MQOO_INPUT_SHARED;

    MQEnvironment.hostname = "10.2.51.19";
    MQEnvironment.port = 1414;
    MQEnvironment.channel = "SW1_QM_CH1";

    MQQueueManager qMgr = new MQQueueManager("SW1_QM");

    MQQueue destQueue = qMgr.accessQueue("E_RETRY",   openOptions);
    System.out.println("E_RETRY size:" + destQueue.getCurrentDepth());
    destQueue.close();
    qMgr.disconnect();

Hope this helps someone else out!

希望这可以帮助别人!

回答by Chris Aldrich

If you want something that works for both SIBus and MQ implementations you are best to stick with the JMS API's (as these are then also portable to other implementations of JMS as well).

如果您想要一些同时适用于 SIBus 和 MQ 实现的东西,您最好坚持使用 JMS API(因为这些 API 也可以移植到 JMS 的其他实现中)。

So what I'd do is:

所以我要做的是:

//ctx is InitialContext
ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/CF");
Connection conn = cf.createConnection();
conn.start();

Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup("jms/MyQueue");
QueueBrowser qb = session.createBrowser(queue);

//sadly, getting this enum is the best the JMS API can offer.
//but the upside is the code is portable AND it can run over MQ and SIBus
//implementations.
Enumeration queueMessageEnum = qb.getEnumeration();
int count = 0;
while(queueMessageEnum.hasMoreElements()) {
  queueMessageEnum.nextElement();
  count++;
}

回答by Aliti

Here is another way; I used the jars from WS 7.0.1.1.

这是另一种方式;我使用了 WS 7.0.1.1 中的罐子。

import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.pcf.MQCFH;
import com.ibm.mq.pcf.PCFAgent;
import com.ibm.mq.pcf.PCFParameter;


PCFAgent agentNode = new PCFAgent(HOST_NAME, PORT, CHANNEL_NAME);

MQCFH cfh = new MQCFH(agentNode.send(CMQCFC.MQCMD_INQUIRE_Q, {new MQCFST(CMQC.MQCA_Q_NAME, QUEUE_NAME)})[0]);

PCFParameter p;

if (cfh.reason == 0) {
  for (int i = 0; i < cfh.parameterCount; i++) {
     p = PCFParameter.nextParameter(responses[0]);
     int parm = p.getParameter();
     switch (parm) {
         case CMQC.MQIA_CURRENT_Q_DEPTH:
              currentDepth = (Integer) p.getValue();
              break;
         case CMQC.MQIA_MAX_Q_DEPTH:
              maximumDepth = (Integer) p.getValue();
              break;
     }
  }
}