Count number of messages in a JMS queue
Asked Answered
M

3

18

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.

Man answered 28/11, 2012 at 11:25 Comment(2)
You can use jmx in some cases (depends of JMS implementation)Titanothere
i see, activemq tag. Example for ActiveMQ java.dzone.com/articles/managing-activemq-jmx-apisTitanothere
P
16

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();
        }
    });
}
Pentad answered 28/2, 2017 at 18:47 Comment(3)
For the sake of completeness: this is for users of the Spring framework.Rickrickard
I have found an issue with this where the queue size sometimes returns 0, which for our use case, fails the job, no idea why, but has anybody else had reliability issues when using this?Sixteenth
can i get the dequeued msg count from jmstemplate?Lymphatic
A
8

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();
    }
Abnaki answered 28/11, 2012 at 11:33 Comment(9)
I actually ran this example and for some reason the messeage count shows 400 when i have 5000 messeages on queueMan
how do you say that you have 5000 messages in a queue.Abnaki
I see it physiclly on my ActiveMQ consoleMan
you send it in a single execution or multiple times , If tried by multiple times ActiveMQ was running or not while you do check.Abnaki
my receiver receives amoun of messseages as the count of messeages getMan
problem may be your receiver receives messages before you get count. So go and stop the receiver and send using sender and count no of messages.you will get it.Abnaki
is this applicable for JBoss 6.4.1 JMS queue?Ratib
I have around 5000 messages in ActiveMQ, So below code taking time to count. Is there any other way to count ?? while (e.hasMoreElements()) { Message message = (Message) e.nextElement(); numMsgs++; }Milton
can i get the dequeued msg count from jmstemplate?Lymphatic
S
7

Java 8 & Spring Boot

   public int countPendingMessages(String destination) {
    // to an Integer because the response of .browse may be null
    Integer totalPendingMessages = this.jmsTemplate.browse(destination, (session, browser) -> Collections.list(browser.getEnumeration()).size());

    return totalPendingMessages == null ? 0 : totalPendingMessages;
   }
Siffre answered 8/1, 2020 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.