【问题标题】:ActiveMQ: how to programmatically monitor embedded brokerActiveMQ:如何以编程方式监控嵌入式代理
【发布时间】:2015-05-12 18:10:15
【问题描述】:

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

  • 如何做到这一点?
  • 我需要 JMX 连接吗?我想防止暴露 JMX
  • 有没有办法在没有 JMX 的情况下访问 org.apache.activemq.broker.jmx Bean?
  • 是否有可以附加到代理本身的 Hooks、Listeners、Events 等?
  • 如果这是一个非常糟糕的主意,为什么?

【问题讨论】:

  • 你用 Tim Bish 回答了吗?我正在尝试进行相同的监控,但我没有找到如何以编程方式知道 ActiveMQ 是否工作正常
  • @JoãoRebelo 据我所知,我使用 Tim 的答案来实现我的解决方案。

标签: activemq


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-05-28
    • 2012-05-13
    • 2013-01-06
    • 2013-12-11
    • 2023-03-16
    • 2018-02-16
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    相关资源
    最近更新 更多