Search code examples
javamonitoringibm-mqmetrics

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?


Solution

  • 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 .......
    }