Any simple way to get the queue length of an ActiveMQ?

18,793

Solution 1

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

Solution 2

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());
Share:
18,793

Related videos on Youtube

user705414
Author by

user705414

Updated on June 04, 2022

Comments

  • user705414
    user705414 almost 2 years

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

  • rogerdpack
    rogerdpack almost 12 years
    NB that unless you increase maxPageSize and memoryLimit it will only browse you at most 400 messages: betterlogic.com/roger/2012/06/activemq-browse-all-messages
  • deFreitas
    deFreitas almost 7 years
    Will you read all broker messages at the consumer to simply count it, really?
  • Admin
    Admin over 6 years
    Thanks for hint, but I used next ObjectName: "org.apache.activemq:BrokerName=localhost,Type=Queue,Destina‌​tion=queueName" and QueueViewMBean class to get queueViewMBean. Generally - approach was the same