Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of messages in a JMS queue

What is the best way to go over a JMS queue and get all the messages in it?

How can count the number of messages in a queue?

Thanks.

like image 854
Michael A Avatar asked Nov 28 '12 11:11

Michael A


People also ask

What is consumer count in JMS queue?

consumer-count The number of consumers consuming messages from this queue.

How do I check my JMS messages?

JMS Message Management Using the Administration Console. The JMS Message Management page of the Administration Console summarizes the messages that are available on the standalone queue, distributed queue, or durable topic subscriber you that you are monitoring.

How do I monitor JMS queues?

In the Administration Console, expand Services > Messaging > JMS Modules. In the JMS Modules table, click the JMS module that contains the configured queue that you want to access. In the selected JMS module's Summary of Resources table, click the queue that you want to monitor. Click the Monitoring > Statistics tab.


2 Answers

Using JmsTemplate

public int getMessageCount(String messageSelector)
{
    return jmsTemplate.browseSelected(messageSelector, new BrowserCallback<Integer>() {
        @Override
        public Integer doInJms(Session s, QueueBrowser qb) throws JMSException
        {
            return Collections.list(qb.getEnumeration()).size();
        }
    });
}
like image 175
Aaron G. Avatar answered Nov 02 '22 21:11

Aaron G.


This is how you can count No of Messages in a Queue

public static void main(String[] args) throws Exception
    {
        // get the initial context
        InitialContext ctx = new InitialContext();

        // lookup the queue object
        Queue queue = (Queue) ctx.lookup("queue/queue0");

        // lookup the queue connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
            lookup("queue/connectionFactory");

        // create a queue connection
        QueueConnection queueConn = connFactory.createQueueConnection();

        // create a queue session
        QueueSession queueSession = queueConn.createQueueSession(false,
            Session.AUTO_ACKNOWLEDGE);

        // create a queue browser
        QueueBrowser queueBrowser = queueSession.createBrowser(queue);

        // start the connection
        queueConn.start();

        // browse the messages
        Enumeration e = queueBrowser.getEnumeration();
        int numMsgs = 0;

        // count number of messages
        while (e.hasMoreElements()) {
            Message message = (Message) e.nextElement();
            numMsgs++;
        }

        System.out.println(queue + " has " + numMsgs + " messages");

        // close the queue connection
        queueConn.close();
    }
like image 40
sunleo Avatar answered Nov 02 '22 20:11

sunleo