15

How to obtain the queue length (number of unconsumed messages sent to queue) in ActiveMQ, using Java?

9ilsdx 9rvj 0lo
  • 7,955
  • 10
  • 38
  • 77
user705414
  • 20,472
  • 39
  • 112
  • 155
  • 1
    You have to use JMX, since the Queue interface does not provide such information. Reference: [ActiveMQ JMX](http://activemq.apache.org/jmx.html), [Required MBeans](http://activemq.apache.org/jmx-support.html) Example: [managing ActiveMQ with JMX APIs](http://www.consulting-notes.com/2010/08/monitoring-and-managing-activemq-with.html) – Dag Oct 10 '11 at 15:22

2 Answers2

16

You have to use JMX, since the Queue interface does not provide such information.

Example of retrieving the size of a specific queue:

// connection
String url = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(url));
MBeanServerConnection connection = connector.getMBeanServerConnection();
// get queue size
ObjectName nameConsumers = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=myqueue");
DestinationViewMBean mbView = MBeanServerInvocationHandler.newProxyInstance(connection, nameConsumers, DestinationViewMBean.class, true);
long queueSize = mbView.getQueueSize();

Reference: ActiveMQ JMX, Required MBeans

Example: managing ActiveMQ with JMX APIs

Dag
  • 10,079
  • 8
  • 51
  • 74
  • 1
    Thanks for hint, but I used next ObjectName: "org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=queueName" and QueueViewMBean class to get queueViewMBean. Generally - approach was the same –  Sep 10 '17 at 12:16
3

Like this;

QueueBrowser browser = session.createBrowser(queue);
Enumeration enu = browser.getEnumeration();
List list = new ArrayList();        
  while (enu.hasMoreElements()) {
    TextMessage message = (TextMessage) enu.nextElement();          
    list.add(message.getText());
   }
System.out.println("Size " + list.size());
Ratha
  • 9,434
  • 17
  • 85
  • 163
  • 3
    NB that unless you increase maxPageSize and memoryLimit it will only browse you at most 400 messages: http://betterlogic.com/roger/2012/06/activemq-browse-all-messages – rogerdpack Jun 11 '12 at 22:31
  • 8
    Will you read all broker messages at the consumer to simply count it, really? – deFreitas Jun 15 '17 at 01:49