ActiveMQ Classic StatisticsPlugin使用通配符目标失效求助
ActiveMQ Classic StatisticsPlugin 通配符统计失效解决方案
问题原因
ActiveMQ.Statistics.Destination.* 这种通配符格式的查询主题不被StatisticsPlugin支持,该插件仅响应单个具体目的地名称的查询请求,通配符无法触发批量返回所有主题的统计数据。
正确解决方法
要批量获取所有主题的统计信息,需查询Broker级别的统计主题,再从返回结果中筛选主题数据:
1. 修改查询主题
将原代码中的查询主题替换为 ActiveMQ.Statistics.Broker,该主题会返回整个Broker的完整统计数据,包含所有队列和主题的信息。
2. 调整结果解析逻辑
从返回的MapMessage中提取destinations嵌套Map,遍历并筛选出类型为Topic的条目。
修改后的完整代码
Connection connection = activeMQConnectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue replyTo = session.createTemporaryQueue(); MessageConsumer consumer = session.createConsumer(replyTo); MessageProducer producer = session.createProducer(null); // 使用Broker级统计主题 String topicName = "ActiveMQ.Statistics.Broker"; Topic query = session.createTopic(topicName); Message msg = session.createMessage(); msg.setJMSReplyTo(replyTo); producer.send(query, msg); System.out.println("统计查询已发送"); // 添加超时时间,避免无限阻塞 MapMessage reply = (MapMessage) consumer.receive(5000); if (reply != null) { System.out.println("收到统计回复"); // 获取所有目的地的统计数据 Map<String, Object> destinations = (Map<String, Object>) reply.getObject("destinations"); for (Map.Entry<String, Object> destEntry : destinations.entrySet()) { String destName = destEntry.getKey(); Map<String, Object> destStats = (Map<String, Object>) destEntry.getValue(); // 筛选主题类型的统计 if ("Topic".equals(destStats.get("type"))) { System.out.println("\n=== 主题 " + destName + " 统计信息 ==="); for (Map.Entry<String, Object> statEntry : destStats.entrySet()) { System.out.println(statEntry.getKey() + "=" + statEntry.getValue()); } } } } else { System.out.println("未收到统计回复,请检查:"); System.out.println("- StatisticsPlugin是否已在activemq.xml中启用"); System.out.println("- ActiveMQ服务是否正常运行"); } connection.close();
额外检查项
确保ActiveMQ配置文件activemq.xml中已启用StatisticsPlugin:
<broker ...> <!-- 其他配置 --> <plugins> <statisticsPlugin/> </plugins> </broker>
内容的提问来源于stack exchange,提问作者Developer
相关产品推荐
相关产品推荐

