如何通过jmx从activemq获取消费者列表和消费者“选择器”?

时间:2018-05-17 07:18:42

标签: java activemq jmx

这是我的代码获取信息通过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 个答案:

答案 0 :(得分:0)

您可以通过查询ConnectionViewMBean获取使用者(ID)列表。您需要传递您希望列出所有使用者的connectionName。要获取connectionName,您需要向JCONSOLE寻求帮助。 Connection Object的规范对象名称可能有点像:

"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;
        }
    }