ActiveMQ get number of consumers listening to a topic from java

I think that the consumer count in the statistics plugin should give you what you want. And I'm fairly sure that the statistics plugin can be enabled in an embedded broker.

http://activemq.apache.org/statisticsplugin.html


I do it simply by fireing this GET: http://localhost:8161/admin/xml/queues.jsp

It returns a list of all queues with registered consumers in XML:

<?xml version="1.0" encoding="UTF-8"?>
<queues>
    <queue name="sauer_test2">
        <stats size="0" consumerCount="0" enqueueCount="0" dequeueCount="0"/>
        <feed>
            <atom>queueBrowse/sauer_test2?view=rss&amp;amp;feedType=atom_1.0</atom>
            <rss>queueBrowse/sauer_test2?view=rss&amp;amp;feedType=rss_2.0</rss>
        </feed>
    </queue>
    <queue name="sauer_test1">
        <stats size="0" consumerCount="1" enqueueCount="1" dequeueCount="1"/>
        <feed>
            <atom>queueBrowse/sauer_test1?view=rss&amp;amp;feedType=atom_1.0</atom>
            <rss>queueBrowse/sauer_test1?view=rss&amp;amp;feedType=rss_2.0</rss>
        </feed>
    </queue>
</queues>

You can see the "consumerCount" attribute and react, accordingly. There is also a get method for getting all consumer-details of a given queue: http://localhost:8161/admin/queueConsumers.jsp?JMSDestination=sauer_test1.


you can use Advisory Messages to get the number of consumers of queues/topics (amongst other things) without using JMX (see ActiveMQ.Advisory.Consumer.Topic, etc)...

Tags:

Java

Activemq

Jmx