Monitoring Queue using Java

Kapil picture Kapil · Jan 5, 2012 · Viewed 9.1k times · Source

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.

Answer

David McPaul picture David McPaul · Jan 9, 2012

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.