订阅和阅读主题:ActiveMQ & Python
Subscribing and reading from Topic: ActiveMQ & Python
我正在尝试订阅 localhost
中 ActiveMQ 运行 中的主题,使用 stompest 连接到代理。请参考以下代码:
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()
.
处暂停
我没有找到与此相关的任何可靠资源或文档。
我是不是做错了什么?
这是预期的行为,因为您正在使用主题(即 pub/sub 语义)。当您向主题发送消息时,它将被传递给所有现有订阅者。如果没有订阅者连接,则消息将被丢弃。
您在任何订阅者连接之前发送您的消息,这意味着代理将丢弃该消息。一旦订阅者连接,就没有消息可接收,因此 receiveFrame()
将简单地阻塞等待帧,因为 stompest documentation notes:
Keep in mind that this method will block forever if there are no frames incoming on the wire.
尝试将消息发送到 queue 然后接收它 或者 先创建一个 asynchronous client 然后发送你的留言。
我正在尝试订阅 localhost
中 ActiveMQ 运行 中的主题,使用 stompest 连接到代理。请参考以下代码:
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()
.
我没有找到与此相关的任何可靠资源或文档。
我是不是做错了什么?
这是预期的行为,因为您正在使用主题(即 pub/sub 语义)。当您向主题发送消息时,它将被传递给所有现有订阅者。如果没有订阅者连接,则消息将被丢弃。
您在任何订阅者连接之前发送您的消息,这意味着代理将丢弃该消息。一旦订阅者连接,就没有消息可接收,因此 receiveFrame()
将简单地阻塞等待帧,因为 stompest documentation notes:
Keep in mind that this method will block forever if there are no frames incoming on the wire.
尝试将消息发送到 queue 然后接收它 或者 先创建一个 asynchronous client 然后发送你的留言。