简体   繁体   English

使用 java 监控 IBM MQ

[英]Monitoring IBM MQ with java

How do I discover the Queue depth, and other metrics about queues, from IBM MQ using java with standard IBM MQ libraries?如何使用 java 和标准 IBM MQ 库从 IBM MQ 发现队列深度和有关队列的其他指标?

Try out the below sample code snippet for fetching the queue depth.试试下面的示例代码片段来获取队列深度。

String mqQMgr = "";
String mqQueue = "";

MQEnvironment.hostname = "";
MQEnvironment.port = "";
MQEnvironment.channel = "";    
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);

MQQueueManager qMgr = new MQQueueManager(mqQMgr);
MQQueue destQueue = qMgr.accessQueue(mqQueue, openOptions);

int depth = destQueue.getCurrentDepth();
destQueue.close();
qMgr.disconnect();

A full code version (Change your parameters accordingly, like Bindings or client mode, options etc ):完整的代码版本(相应地更改您的参数,如绑定或客户端模式、选项等):

import com.ibm.mq.*;

public class QueueManager {

    private final String host;
    private final int port;
    private final String channel;
    private final String manager;
    private final MQQueueManager qmgr;

    public QueueManager(String host, int port, String channel, String manager) throws MQException {
        this.host = host;
        this.port = port;
        this.channel = channel;
        this.manager = manager;
        this.qmgr = createQueueManager();
    }


    private MQQueueManager createQueueManager() throws MQException {
        MQEnvironment.channel = channel;
        MQEnvironment.port = port;
        MQEnvironment.hostname = host;
        MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
        return new MQQueueManager(manager);
    }

    // This method will return the Queue Depth
    public int queueDepth(String queueName) {
       int depth = -1;
       int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING;
       MQQueue queue = null;
       try {
          queue = qmgr.accessQueue(queueName, openOptions);
          depth = queue.getCurrentDepth();
       }
       catch (MQException e)
       {
          System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
       }
       finally {
          try {
             if (queue != null)
                queue.close();
          }
          catch (MQException e) {
             System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
          }
       }

       return depth;
    }

    ........ Other Methods .......
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM