如何通过 jmx 从 activemq 获取消费者列表和消费者“选择器”?
Posted
技术标签:
【中文标题】如何通过 jmx 从 activemq 获取消费者列表和消费者“选择器”?【英文标题】:How can I get consumer list and consumer "selector" from activemq via jmx? 【发布时间】:2018-10-27 08:59:39 【问题描述】:这是我通过 jmx 获取信息的代码,从这段代码中,我可以获取有关 ActiveMQ 的一些信息,例如队列名称、消费者数量,但我不知道如何获取一个队列的消费者列表并获取消费者“选择器”在图片中?
"selector" image
package controller;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;
public class Monitor
public static void main(String[] args) throws Exception
String url = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
JMXServiceURL urls = new JMXServiceURL(url);
JMXConnector connector = JMXConnectorFactory.connect(urls);
connector.connect();
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ObjectName objectName = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker");
BrokerViewMBean mBean = MBeanServerInvocationHandler.newProxyInstance(mbsc, objectName, BrokerViewMBean.class, true);
for (ObjectName queueName : mBean.getQueues())
QueueViewMBean queueMBean = MBeanServerInvocationHandler.newProxyInstance(mbsc, queueName, QueueViewMBean.class, true);
// queue name
System.out.println("Queue Name:" + queueMBean.getName());
// consumer count
System.out.println("Number of Consumers:" + queueMBean.getConsumerCount());
【问题讨论】:
【参考方案1】:您可以通过查询 ConnectionViewMBean 来获取消费者 (ID) 的列表。您需要传递您希望为其列出所有消费者的 connectionName。要获得 connectionName,您需要向 JCONSOLE 寻求帮助。连接对象的规范对象名称可能有点像:
"org.apache.activemq:type=Broker,brokerName=<>,connector=<Connector>,connectorName=<ConnectorName>"
ConnectionViewMBean connectionView = MBeanServerInvocationHandler.newProxyInstance(mbeanServer, <connectionName>, ConnectionViewMBean.class, true);
for (ObjectName name : connectionView.getConsumers())
if (name.equals(subscriptionName))
found = true;
【讨论】:
以上是关于如何通过 jmx 从 activemq 获取消费者列表和消费者“选择器”?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 JMX 导出器将 JMX 指标从 Kafka 消费者推送到普罗米修斯