ActiveMQ:如何以编程方式监控嵌入式代理

ActiveMQ: how to programmatically monitor embedded broker

我想从代码内部监控嵌入式 ActiveMQ 5.8 代理。

您可以从具有嵌入式代理的进程中访问所有标准 JMX MBean,而无需创建将它们暴露给外界的 JMX 连接器。首先,您需要告诉嵌入式代理启用 JMX 但不创建连接器。

    brokerService = new BrokerService();
    brokerService.setPersistent(false);
    brokerService.setAdvisorySupport(false);
    brokerService.setSchedulerSupport(true);
    brokerService.setPopulateJMSXUserID(true);
    brokerService.setSchedulerSupport(true);
    brokerService.getManagementContext().setCreateConnector(false);

然后在您的代码中,您可以正常访问 JMS MBean,例如获取 BrokerViewMBean:

protected BrokerViewMBean getProxyToBroker() throws MalformedObjectNameException, JMSException {
    ObjectName brokerViewMBean = new ObjectName(
        "org.apache.activemq:type=Broker,brokerName=localhost");
    BrokerViewMBean proxy = (BrokerViewMBean) brokerService.getManagementContext()
            .newProxyInstance(brokerViewMBean, BrokerViewMBean.class, true);
    return proxy;
}

或获取 QueueViewMBean:

protected QueueViewMBean getProxyToQueue(String name) throws MalformedObjectNameException, JMSException {
    ObjectName queueViewMBeanName = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName="+name);
    QueueViewMBean proxy = (QueueViewMBean) brokerService.getManagementContext()
            .newProxyInstance(queueViewMBeanName, QueueViewMBean.class, true);
    return proxy;
}

和 TopicViewMBean 类似。

protected TopicViewMBean getProxyToTopic(String name) throws MalformedObjectNameException, JMSException {
    ObjectName topicViewMBeanName = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Topic,destinationName="+name);
    TopicViewMBean proxy = (TopicViewMBean) brokerService.getManagementContext()
            .newProxyInstance(topicViewMBeanName, TopicViewMBean.class, true);
    return proxy;
}