You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用ActiveMQ Classic统计插件无法获取数据问题求助

ActiveMQ Classic统计数据获取失败问题解决

我参考ActiveMQ Classic官方文档获取Broker/队列统计数据,但按文档编写的代码无法拿到数据:ActiveMQ.Statistics.Broker队列有消息,但test队列无任何消息,代码片段如下:

self.conn = stomp.Connection([('localhost', 61616)] , keepalive=True, try_loopback_connect=True, reconnect_attempts_max=3,)
self.conn.set_listener("testlistener", TestListener(print_to_log=True))
# 其他用于现有功能的监听器

def retrieve_stats(self) -> None:
  print("*************START*****************\n")

  self.conn.subscribe(id=time.time(), destination="/queue/test") # 创建接收统计数据的队列

  self.conn.send(
      body="",
      destination="/queue/ActiveMQ.Statistics.Broker",
      content_type="text/blah",
      headers={
          "amq-msg-type": "text",
          "persistent": "true",
          "reply-to": "/queue/test",
        },
  )
  broker_stat_listener = self.conn.get_listener("testlistener")
  if len(broker_stat_listener.message_list) > 0:
       print(broker_stat_listener.message_list.__str__())

  print("*************END*****************\n")

排查修复点:

  1. 缺少等待时间:发送请求后立即检查消息列表,Broker还未完成统计消息的推送,需添加等待逻辑,比如:
    import time
    # 发送请求后添加等待
    time.sleep(2)
    
  2. 订阅ID不规范:time.time()生成的浮点数作为订阅ID可能不符合STOMP规范,改用固定字符串ID:
    self.conn.subscribe(id="stats-sub", destination="/queue/test", ack="auto")
    
  3. 无效头部参数:移除不必要的amq-msg-type,修正content_type为标准格式:
    self.conn.send(
        body="",
        destination="/queue/ActiveMQ.Statistics.Broker",
        content_type="text/plain",
        headers={
            "persistent": "true",
            "reply-to": "/queue/test",
        },
    )
    
  4. 监听器实现验证:确保TestListener正确处理消息并添加到message_list,示例实现:
    class TestListener(stomp.ConnectionListener):
        def __init__(self, print_to_log=True):
            self.print_to_log = print_to_log
            self.message_list = []
    
        def on_message(self, frame):
            self.message_list.append(frame.body)
            if self.print_to_log:
                print(f"收到消息: {frame.body}")
    
  5. 连接状态确认:调用retrieve_stats前必须确保self.conn已执行connect()完成连接,否则所有操作无效。

内容的提问来源于stack exchange,提问作者Khushboo Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 12:25:24