Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of consumers connected to Websphere MQ queue from Java

Tags:

java

ibm-mq

I am trying to get the number of consumers of a particular Websphere MQ queue from Java? I need to know whether someone is going to consume the messages before placing them on the queue.

like image 814
Mensur Avatar asked Sep 30 '22 20:09

Mensur


1 Answers

First, it is worth noting that the design proposed is a very, VERY bad design. The effect is to turn async messaging back into synchronous messaging. This couples message producers to consumers, introduces location and resolution dependencies, breaks clustering, defeats WMQ's load distribution and balancing, embeds network topology into the application, and makes the whole system brittle. Please do not blame WMQ for not working correctly after intentionally defeating all its best features except the actual queue/dequeue operations.

However, to answer your question more directly, use the getOpenInputCount method of the queue object to obtain the number of open input handles. Here's how:

MQQueue outQ = qMgr.accessQueue(qName,
                                openOptions,
                                null,           // default q manager
                                null,           // no dynamic q name
                                null);          // no alternate user id

int inCount = outQ.getOpenInputCount(); 

Note that you can only inquire the input handles on a local queue. If the queue is hosted on a QMgr other than the one where the message sender is connected, this method will not work. Of course it is the normal case that the message sender and receiver would reside on different QMgrs. However since you do not mention much about the design, I'll assume for purposes of this answer that connections from the message producer and consumer attach to the same QMgr. If that's not the case, we need to have a discussion about PCF and even stronger warnings about the design.

like image 167
T.Rob Avatar answered Nov 03 '22 15:11

T.Rob