【问题标题】:Subscribing and reading from Topic: ActiveMQ & Python订阅和阅读主题:ActiveMQ & Python
【发布时间】:2021-03-02 11:57:58
【问题描述】:

我正在尝试使用stompest 订阅在localhost 中运行的ActiveMQ 中的主题以连接到代理。请参考以下代码:

import os
import json
from stompest.sync import Stomp
from stompest.config import StompConfig

CONFIG = StompConfig(uri=os.environ['MQ_URL'],
                     login=os.environ['MQ_UID'],
                     passcode=os.environ['MQ_DWP'],
                     version="1.2")

topic = '/topic/SAMPLE.TOPIC'

msg = {'refresh': True}

client = Stomp(CONFIG)
client.connect()
client.send(topic, json.dumps(msg).encode())
client.disconnect()

client = Stomp(CONFIG)
client.connect(heartBeats=(0, 10000))
token = client.subscribe(topic, {
    "ack": "client",
    "id": '0'
})

frame = client.receiveFrame()
if frame and frame.body:
    print(f"Frame received from MQ: {frame.info()}")
client.disconnect()

虽然我看到 ActiveMQ Web 控制台的活动连接,但代码中没有收到任何消息。控制流似乎在frame = client.receiveFrame() 处暂停。

我没有找到任何可靠的资源或文档。

我在这里做错了吗?

【问题讨论】:

    标签: python-3.x activemq stomp jms-topic


    【解决方案1】:

    这是预期的行为,因为您使用的是主题(即 pub/sub 语义)。当您向主题发送消息时,它将被传递给所有现有订阅者。如果没有订阅者连接,则该消息将被丢弃。

    您在连接任何订阅者之前发送消息,这意味着代理将丢弃该消息。一旦订阅者连接,就没有消息要接收,因此receiveFrame() 将简单地阻止等待帧,因为stompest documentation notes

    请记住,如果网络上没有帧传入,此方法将永远阻塞。

    尝试向队列发送消息,然后接收它先创建asynchronous client,然后发送消息。

    【讨论】:

      猜你喜欢
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      相关资源
      最近更新 更多