Count number of messages in a JMS queue

Michael A picture Michael A · Nov 28, 2012 · Viewed 27.1k times · Source

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.

Answer

Aaron G. picture Aaron G. · Feb 28, 2017

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();
        }
    });
}