ActiveMQ 在不使用 JMX、JMS 的情况下获取队列大小

ActiveMQ get queue size without using JMX, JMS

我需要一种无需 using JMX or Iterate all queue using JMS 即可在 ActiveMQ 中获取队列大小的方法。是否有任何使用 ActiveMQ API 获取队列大小的解决方案。

JMS 中没有 API 用于查询代理的统计信息,这违背了将客户端与彼此以及中间代理解耦的概念。 ActiveMQ 确实提供了一些您可以使用的东西,JMX 是获得您想要的东西的最强大的方式,但是如果您不想直接进入 JMX API,您可以使用基于 REST 的方法,使使用了Jolokia project which ActiveMQ embeds to support access to the JMX Mbeans that the broker exposes using REST calls

除了 REST 选项之外,唯一的其他方法是启用 Statistics Broker Plugin 以允许您将目标消息发送到代理以使用标准 JMS 代码检索 运行 时间统计信息。

Statistics Broker Plugin 添加到您的 activemq.xml 。 以下是获取 ActiveMQ 统计信息的代码片段。

public Long checkMessageCountOnAllBroker() throws JMSException {
    MapMessage mapMessage = (MapMessage) jmsTemplate.sendAndReceive("ActiveMQ.Statistics.Broker", Session::createMessage);
    return mapMessage.getLong("size");
}

这将从ActiveMq.For获取特定主题的静态信息中获取所有队列的大小。

public Long checkMessageCountOnDestination() throws JMSException {
    MapMessage mapMessage = jmsTemplate.sendAndReceive("ActiveMQ.Statistics.Destination.some-topic", Session::createMessage);
    return mapMessage.getLong("size");
}

它将从目标主题中获取统计信息