python stomp 收發指定的消息

python stomp收發指定的消息

# -*- coding: utf-8 -*-
import sys
import time
import sys
import stomp

class MyListener(object):
    def on_error(self, headers, message):
        print('received an error %s' % message)
    def on_message(self, headers, message):
        print('received a message %s' % headers)


conn = stomp.Connection10([('localhost',61613)])  
conn.set_listener('logicServerQueue', MyListener())
conn.start()
conn.connect(wait=True)


# 發送消息到testQueue隊列,指定consumerId='88.3@6006'
conn.send(body=b'hahah', destination='testQueue', headers={'consumerId': '88.3@6006'})
# 從testQueue隊列中接收消息,用selector過濾,只接收consumerId = '88.3@6006'的消息
conn.subscribe(destination='testQueue', headers={'selector' : "consumerId = '88.3@6006'"})

while True:
    try:
        time.sleep(1)
    except:
        break

conn.disconnect()

time.sleep(2)
conn.disconnect()

這裏寫圖片描述

從控制檯可以看出consumerId = ‘88.3@6006’被設置到了消息的headers中

發佈了84 篇原創文章 · 獲贊 135 · 訪問量 41萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章