ActiveMQ: how to programmatically monitor embedded

2019-06-07 01:27发布

I want to monitor an embedded ActiveMQ 5.8 broker from inside the code.

  • How can this be done?
  • Do I need a JMX connection? I want to prevent exposing JMX
  • Is there a way of accessing org.apache.activemq.broker.jmx Beans without JMX?
  • Are there Hooks, Listeners, Events, ... that can be attached to the broker itself?
  • If this is a really bad idea, why?

标签: activemq
1条回答
▲ chillily
2楼-- · 2019-06-07 01:52

You can access all the standard JMX MBeans from within the process that has an embedded broker without creating the JMX connector that would expose them to the outside world. First you need to tell the embedded broker to enable JMX but not create the connector.

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

Then in your code you can access the JMS MBeans as normal for instance to get the 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;
}

Or to get a 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;
}

And similarly a 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;
}
查看更多
登录 后发表回答