ActiveMQ get number of consumers listening to a topic from java

﹥>﹥吖頭↗ 提交于 2020-01-14 07:44:10

问题


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?


回答1:


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




回答2:


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




回答3:


In case of embedded ActiveMQ you may use BrokerService, to get consumers count on topic. Code is in Scala, but there shouldn't be much difference in Java.

            import org.apache.activemq.broker.{BrokerService, TransportConnector}

            val brokerService = new BrokerService()
            brokerService.setBrokerName("localhost")
            brokerService.setUseJmx(false)
            val transportConnector: TransportConnector = brokerServiceSetup.addConnector(s"tcp://localhost:61616")
            brokerService.start()
            brokerService.getDestination(new ActiveMQTopic(topicName))
            topic.getConsumers


来源:https://stackoverflow.com/questions/10291452/activemq-get-number-of-consumers-listening-to-a-topic-from-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!