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

橙三吉。 提交于 2019-11-29 12:01:03

I used the jars from 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

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

    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!

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).

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

Here is another way; I used the jars from 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;
     }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!