溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ActiveMQ:使用Python訪問ActiveMQ的方法

發布時間:2020-10-12 22:53:21 來源:腳本之家 閱讀:352 作者:邏輯Ren 欄目:開發技術

Windows 10家庭中文版,Python 3.6.4,stomp.py 4.1.21

ActiveMQ支持Python訪問,提供了基于STOMP協議(端口為61613)的庫。

ActiveMQ的官文Cross Language Clients中給出了更詳細的介紹,并附有示例代碼,如下圖:

第一行為常規Python訪問,第二行為使用Jython訪問的方式,四個操作。

ActiveMQ:使用Python訪問ActiveMQ的方法

Python訪問ActiveMQ需要使用stomp.py,見其官網。

下載官網的代碼,解壓,命令行進入其目錄,使用pyhthon setup.py install即可安裝好,然后就可以使用stomp.py了。

官方示例代碼:給隊列test發送一個消息,也可以把第7行的destination的“/queue/”去掉,只剩test。

import stomp

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.start()
conn.connect('admin', 'password', wait=True)
conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')
conn.disconnect()

測試結果:test隊列接收到消息數量增加了

ActiveMQ:使用Python訪問ActiveMQ的方法

stomp.Connection()默認是connect.StompConnection11(協議1.1),還可以可以選擇1.0、1.2。

ActiveMQ:使用Python訪問ActiveMQ的方法

在官方代碼中,stomp.Connection()的參數為空,實際上可以有很多參數,比如,設置Broker的IP地址和端口,如下:其中的host_and_ports就是設置IP和端口的。

ActiveMQ:使用Python訪問ActiveMQ的方法

IP和端口設置示例:

c = stomp.Connection([('127.0.0.1', 62613)])

這里我犯錯了,端口我協程了8161(ActiveMQ的Web訪問的端口),經查詢(百度搜索到stackoverflow.com)才知,STOMP協議用的是61613(ActiveMQ的配置文件中):

ActiveMQ:使用Python訪問ActiveMQ的方法

ActiveMQ官網的四個測試:

發送消息到隊列Queue屬于 點對點模式,不可以重復消費;

發送消息到主題Topic屬于 發布/訂閱模式,可以重復消費;

# Send a Message to an Apache ActiveMQ Queue 
import stomp
 
conn = stomp.Connection10()
 
conn.start()
 
conn.connect()
 
conn.send('SampleQueue', 'Simples Assim')
 
conn.disconnect()

# Receive a Message from an Apache ActiveMQ Queue
import stomp
import time
 
class SampleListener(object):
 def on_message(self, headers, msg):
 print(msg)
 
conn = stomp.Connection10()
 
conn.set_listener('SampleListener', SampleListener())
 
conn.start()
 
conn.connect()
 
conn.subscribe('SampleQueue')
 
time.sleep(1) # secs
 
conn.disconnect()

# Send a Message to an Apache ActiveMQ Topic 
import stomp
 
conn = stomp.Connection10()
 
conn.start()
 
conn.connect()
 
conn.send('/topic/SampleTopic', 'Simples Assim')
 
conn.disconnect()

# Receive a Message from an Apache ActiveMQ Topic (1)
import stomp
import time
 
class SampleListener(object):
 def on_message(self, headers, msg):
 print(msg)
 
conn = stomp.Connection10()
 
conn.set_listener('SampleListener', SampleListener())
 
conn.start()
 
conn.connect()
 
conn.subscribe('/topic/SampleTopic')
 
time.sleep(1) # secs
 
conn.disconnect()

# Receive a Message from an Apache ActiveMQ Topic (2)
import stomp
import time
 
class SampleListener(object):
 def on_message(self, headers, msg):
 print(msg)
 
conn = stomp.Connection10()
 
conn.set_listener('SampleListener', SampleListener())
 
conn.start()
 
conn.connect(headers={'client-id':'SampleClient'})
 
conn.subscribe(destination='/topic/SampleTopic', headers={'activemq.subscriptionName':'SampleSubscription'})
 
time.sleep(1) # secs
 
conn.disconnect()

以上這篇ActiveMQ:使用Python訪問ActiveMQ的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女