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

时间:2015-05-12 07:00:16

标签: activemq

我想从代码中监视嵌入式ActiveMQ 5.8代理。

  • 怎么办呢?
  • 我是否需要JMX连接?我想防止暴露JMX
  • 有没有办法在没有JMX的情况下访问org.apache.activemq.broker.jmx Beans?
  • 是否有可以附加到经纪人本身的钩子,听众,事件......?
  • 如果这是一个非常糟糕的主意,为什么?

1 个答案:

答案 0 :(得分:2)

您可以从具有嵌入式代理的进程中访问所有标准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;
}