Strange behavior when counting messages in a JMS queue

I am using Active MQ and Java JMS.

I want to count the number of messages in the queue.

One approach is counting messages with a browser:

Queue queue = (Queue) session.createQueue(subject); QueueBrowser queueBrowser = session.createBrowser(queue); Enumeration<?> e = queueBrowser.getEnumeration(); int numMsgs = 0; // count number of messages while (e.hasMoreElements()) { // Message m = (Message) e.nextElement(); e.nextElement(); numMsgs++; } 

But for a queue with 5000 pending requests, this returns only 500.

Another approach is this (repeat all the clutter in the queue):

 Message message= consumer.receive(500); while(message!= null) { if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; // BytesMessage Byte System.out.println("Received message '"+ textMessage.getText() + "'"); } if(message!=null) Messages_list.add(message); message = consumer.receive(1); } 

But it also does not give the correct number of pending messages.

How can I confidently loop through akk messages waiting in line?

+4
source share
2 answers

There is an error in ActiveMQ that prevents the actual number of messages from being returned. In this case, the view returns only one page of messages, which is set by the maxPageSize property and is documented here: http://activemq.apache.org/per-destination-policies.html

ActiveMQ currently has a bug report on this issue, and it is tracked here: https://issues.apache.org/jira/browse/AMQ-4181 . This issue has been resolved and is currently planned to be installed in ActiveMQ 5.8.0.

+7
source

Since you are using ActiveMQ, you can use ActiveMQ StatisticsPlugin: http://activemq.apache.org/statisticsplugin.html

Similarly, to request statistics for a destination, simply send a message to the destination name, which is added to ActiveMQ.Statistics.Destination. For example, to get statistics for a queue whose name is TEST.FOO, send an empty message to the queue named ActiveMQ.Statistics.Destination.TEST.FOO

In particular, you might be interested in enqueueCount . I omit the sample code here, since the sample code on the plugin web page is concise and good.

+1
source

All Articles