使用python怎么實現發送和接收ActiveMQ消息,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
首先需要安裝python的stomp庫。
命令如下:
pip install stomp.py
接著,就是上代碼了具體如下:
# -*-coding:utf-8-*-
import stomp
import time
queue_name = '/queue/SampleQueue'
topic_name = '/topic/SampleTopic'
listener_name = 'SampleListener'
class SampleListener(object):
def on_message(self, headers, message):
print 'headers: %s' % headers
print 'message: %s' % message
# 推送到隊列queue
def send_to_queue(msg):
conn = stomp.Connection10([('127.0.0.1',61613)])
conn.start()
conn.connect()
conn.send(queue_name, msg)
conn.disconnect()
#推送到主題
def send_to_topic(msg):
conn = stomp.Connection10([('127.0.0.1',61613)])
conn.start()
conn.connect()
conn.send(topic_name, msg)
conn.disconnect()
##從隊列接收消息
def receive_from_queue():
conn = stomp.Connection10([('127.0.0.1',61613)])
conn.set_listener(listener_name, SampleListener())
conn.start()
conn.connect()
conn.subscribe(queue_name)
time.sleep(1) # secs
conn.disconnect()
##從主題接收消息
def receive_from_topic():
conn = stomp.Connection10([('127.0.0.1',61613)])
conn.set_listener(listener_name, SampleListener())
conn.start()
conn.connect()
conn.subscribe(topic_name)
while 1:
send_to_topic('topic')
time.sleep(3) # secs
conn.disconnect()
if __name__=='__main__':
# send_to_queue('len 123')
# receive_from_queue()
receive_from_topic()看完上述內容,你們掌握使用python怎么實現發送和接收ActiveMQ消息的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。