python 通过stomp操作ActiveMQ

windows 下,在cmd 命令下执行 pip install stomp.py 安装stomp
# -*-coding:utf-8-*-
import stomp
import time

topic_name = 'test_topic'
listener_name = 'SampleListene'
mq_ip = "*.0.0.*"
mq_port = 61613


class SampleListener(stomp.ConnectionListener):
    def on_message(self, headers, message): 
        #header 消息头信息,可以通过header 判断那个topic的数据 
        #{'message-id': 'ID:jtcsOTMS-db-47634-1552380859117-1:1:1:1:7249045', 'destination': '/queue/test_topic', 'timestamp': '15529}
        print(message)


def receive_from_topic():
    conn = stomp.Connection10([(mq_ip, mq_port)]) #STOMP 1.0 连接方式
    conn.set_listener(listener_name, SampleListener()) #创建监听者
    #conn.start()
    conn.connect() # 在connect()中已经添加了start,可以将上边的start去掉
    conn.subscribe(topic_name, id="test")   #开始监听 接收消息
    while 1:
        try:
            time.sleep(10)
        except Exception as e:
            print(e)
            break
    if conn.is_connected():
        conn.disconnect()


if __name__ == '__main__':
    receive_from_topic()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值