python stomp 转发activemq topic消息
import pysimplestomp
- 连接到ActiveMQ的Topic:
# 连接ActiveMQ服务器
server = "tcp://localhost:61613"
topic = "/topic/your_topic"# 连接到ActiveMQ的Topic
destination = f"destination://{topic}"
connection = pysimplestomp.connect(server)
- 订阅Topic并转发消息:
def on_message(message):
# 在这里处理收到的消息,例如将其转发到另一个Topic或执行其他操作
print("Received message:", message)
# 示例:将消息转发到另一个Topic
connection.send(message['body'], destination=f"destination://{topic_to_forward}")
- 启动监听器并等待消息到达:
connection.subscribe(destination, on_message)
connection.listen()
请注意,上述示例中的your_topic
和topic_to_forward
应替换为您实际使用的Topic名称。此外,您需要确保ActiveMQ服务器正在运行,并且正确配置了所需的Topic。