Reputation: 832
I am trying to monitor a queue without using any API such as Hermes or GEMS i.e. I want to use purely JAVA. SO in order to browse the queue i.e. check if message has reached the queue or not without actually consuming the message I have written below piece of code:
javax.jms.QueueBrowser browser = session.createBrowser(queue);
Enumeration msgs = browser.getEnumeration();
int Count=0;
while(msgs.hasMoreElements())
{
message = (javax.jms.Message)msgs.nextElement();
System.out.println("Message"+message);
Count++;
}
However when I am publishing the messages on queue it is not displaying the result. I have verified that messages are reaching the queue and same are being consumed by the receiver. So as this approach was not working I thought to use a different approach which is by counting the numberOfMessages recevied by queue before and after. So I used the below piece of code
QueueInfo q= new QueueInfo(queueName);
long l=q.getInTransitMessageCount();
System.out.println("In transit Mesasge Count="+l+"\n");
But this is also not working. Any suggestion or explanation to resolve this problem would be highly appreciated. Please note there is no compilation error in the code and all the necessary classes are imported.
Upvotes: 3
Views: 9827
Reputation: 153
The code you are using will count the number of messages in the queue at a single moment in time. Messages posted and then consumed outside this timeslice will not show up and messages are consumed very quickly in most JMS implementations.
When we monitor an active message queue we are only concerned if there are messages in the queue as this means the consumer has stopped or is not consuming fast enough.
If you are trying to count the messages going past then you will need to intercept the messages (ie consume them yourself and post them to another queue).
You might want to give more information about what you really want.
Upvotes: 1
Reputation: 11733
QueueBrowser does not need to give you a snapshot. IF you have other consumers on the queue, then they will be draining it, so it's possible that they are being pulled off. Please try turning off your consumer first to see what happens. See [1]
[1] http://docs.oracle.com/javaee/1.3/api/javax/jms/QueueBrowser.html
Upvotes: 0
Reputation: 9702
your browser code seems to be right..check whether you are passing correct queue argument..or just debug and see why that loop is not executed?
Upvotes: 0