Getting the depth of an MQSeries queue from Java
The JMS spec does not provide a way to easily obtain the number of messages on a certain queue. You could create a browser and browse through the entire queue and keep count, but this is very slow.
The java client for MQSeries (or WebsphereMQ as it is called nowadays) does provide this functionality, so if you don’t mind using IBM specific classes, you could do it like this:
public int getDepth(String queueName) throws MQException { // Build quemanager (this should be done in another method) // and not every time in a real life application MQEnvironment.channel = "CHANNELNAME"; MQEnvironment.port = 1414; MQEnvironment.hostname = "qmgrhost.yourdomain.com"; MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES); MQQueueManager qmgr = new MQQueueManager("YourQueueManagerName"); // access the queue to query its depth com.ibm.mq.MQQueue queue = qmgr.accessQueue(queueName, MQC.MQOO_INQUIRE | MQC.MQOO_INPUT_AS_Q_DEF, null, null, null); return queue.getCurrentDepth(); }
The com.ibm.mq.MQQueue class exposes some other interesting methods to get more information about a queue, like the maximum depth and the maximum message length.
0 comments Wednesday 11 Jun 2008 | Guy Mahieu | java(t) , mqseries(t) , tips&tricks(t)