Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveMQ get number of consumers listening to a topic from java

Tags:

java

activemq

jmx

I would like to be able to get the number of consumers listening to a topic from java for an embedded ActiveMQ (5.4.2) broker in the same JVM. Is JMX really the only option here? JMX seems like a bad option since it may be optionally disabled. This post shows how to use JMX to get a list of connections: ActiveMQ: Get list of connections through JMX?

I would prefer a non-JMX based solution though due to it perhaps being disabled. I guess JMX would be ok if it was still usable from java when disabled. I am just familiar with enabling/disabling it for use with jconsole.

Am I missing something in the API?

like image 453
Ryan R. Avatar asked Apr 24 '12 03:04

Ryan R.


People also ask

What is number of consumers in ActiveMQ?

You can write two consumers—a heads consumer and a tails consumer—that subscribe to that topic but that only receive messages with their selected value of the coin property.

Can JMS queue have multiple consumers?

Support for multiple-consumer queues is a Message Queue feature (the JMS specification defines messaging behavior in the case of only one consumer accessing a queue). When multiple consumers access a queue, the load-balancing among them takes into account each consumer's capacity and message processing rate.

How do you increase the number of consumers in ActiveMQ?

Straight through Session Consumption If you are using Consumers with auto acknowledge, you can increase throughput by passing messages straight through the Session to the Consumer by setting alwaysSessionAsync=false on the ActiveMQ ConnectionFactory .


3 Answers

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)...

like image 81
Ben ODay Avatar answered Oct 21 '22 23:10

Ben ODay


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.

like image 34
Sauer Avatar answered Oct 22 '22 01:10

Sauer


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

like image 8
Jason Axelson Avatar answered Oct 22 '22 00:10

Jason Axelson