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

Apache ActiveMQ Classic虚拟主题与python-stomp配合失效问题排查

问题排查与解决方案

1. 修正订阅目的地格式

你的虚拟主题配置中,prefix="VirtualTopicConsumers.*."定义了订阅队列的格式为VirtualTopicConsumers.<消费者组>.<虚拟主题名>,无需重复VirtualTopic.前缀。你发布的主题是VirtualTopic.test,对应的虚拟主题核心名称是test,所以正确的订阅目的地需要明确指定队列类型,避免Stomp客户端误判:

conn.subscribe(destination='queue://VirtualTopicConsumers.A.test', id="1", ack='auto')

2. 修复订阅者连接逻辑

当前订阅者代码会每隔3秒强制断开重连,大概率导致消息发布时订阅者处于离线状态。需调整为保持持续在线:

import time
import stomp

class MyListener(stomp.ConnectionListener):
    def on_error(self, message):
        print('received an error "%s"' % message.body)
    def on_message(self, frame):
        print('received a message body: "%s"' % frame.body)

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.connect('admin', 'admin', wait=True)
# 订阅正确的队列目的地
conn.subscribe(destination='queue://VirtualTopicConsumers.A.test', id="1", ack='auto')

# 保持连接在线,直到手动中断
try:
    while True:
        time.sleep(60)
except KeyboardInterrupt:
    conn.disconnect()

3. 验证配置与测试

  • 确认activemq.xml中的虚拟主题配置无误:<virtualTopic name=">" prefix="VirtualTopicConsumers.*." selectorAware="false"/>会匹配所有VirtualTopic.开头的主题,符合你的发布需求。
  • 测试流程:先启动修改后的订阅者(保持在线),再运行发布者代码,查看订阅者控制台是否收到消息。
  • 若仍有问题,可登录ActiveMQ管理控制台(默认http://localhost:8161),检查Topics下的VirtualTopic.test是否有消息记录,以及Queues下是否生成了VirtualTopicConsumers.A.test队列,确认消息是否进入队列。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 01:53:23