ActiveMQ:使用Python訪問ActiveMQ

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

 

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

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

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

 

Python訪問ActiveMQ須要使用stomp.py,見其官網分佈式

下載官網的代碼,解壓,命令行進入其目錄,使用pyhthon setup.py install便可安裝好,而後就能夠使用stomp.py了。測試

 

官方示例代碼:給隊列test發送一個消息,也能夠把第7行的destination的「/queue/」去掉,只剩test。spa

1 import stomp
2 
3 conn = stomp.Connection()
4 conn.set_listener('', MyListener())
5 conn.start()
6 conn.connect('admin', 'password', wait=True)
7 conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')
8 conn.disconnect()

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

 

stomp.Connection()默認是connect.StompConnection11(協議1.1),還能夠能夠選擇1.0、1.2。命令行

 

在官方代碼中,stomp.Connection()的參數爲空,實際上能夠有不少參數,好比,設置Broker的IP地址和端口,以下:其中的host_and_ports就是設置IP和端口的。3d

 

IP和端口設置示例:

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

 

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

 

ActiveMQ官網的四個測試:

發送消息到隊列Queue屬於 點對點模式,不能夠重複消費;

發送消息到主題Topic屬於 發佈/訂閱模式,能夠重複消費;

 1 # Send a Message to an Apache ActiveMQ Queue 
 2 import stomp
 3  
 4 conn = stomp.Connection10()
 5  
 6 conn.start()
 7  
 8 conn.connect()
 9  
10 conn.send('SampleQueue', 'Simples Assim')
11  
12 conn.disconnect()
13 
14 # Receive a Message from an Apache ActiveMQ Queue
15 import stomp
16 import time
17  
18 class SampleListener(object):
19   def on_message(self, headers, msg):
20     print(msg)
21  
22 conn = stomp.Connection10()
23  
24 conn.set_listener('SampleListener', SampleListener())
25  
26 conn.start()
27  
28 conn.connect()
29  
30 conn.subscribe('SampleQueue')
31  
32 time.sleep(1) # secs
33  
34 conn.disconnect()
35 
36 # Send a Message to an Apache ActiveMQ Topic 
37 import stomp
38  
39 conn = stomp.Connection10()
40  
41 conn.start()
42  
43 conn.connect()
44  
45 conn.send('/topic/SampleTopic', 'Simples Assim')
46  
47 conn.disconnect()
48 
49 # Receive a Message from an Apache ActiveMQ Topic (1)
50 import stomp
51 import time
52  
53 class SampleListener(object):
54   def on_message(self, headers, msg):
55     print(msg)
56  
57 conn = stomp.Connection10()
58  
59 conn.set_listener('SampleListener', SampleListener())
60  
61 conn.start()
62  
63 conn.connect()
64  
65 conn.subscribe('/topic/SampleTopic')
66  
67 time.sleep(1) # secs
68  
69 conn.disconnect()
70 
71 # Receive a Message from an Apache ActiveMQ Topic (2)
72 import stomp
73 import time
74  
75 class SampleListener(object):
76   def on_message(self, headers, msg):
77     print(msg)
78  
79 conn = stomp.Connection10()
80  
81 conn.set_listener('SampleListener', SampleListener())
82  
83 conn.start()
84  
85 conn.connect(headers={'client-id':'SampleClient'})
86  
87 conn.subscribe(destination='/topic/SampleTopic', headers={'activemq.subscriptionName':'SampleSubscription'})
88  
89 time.sleep(1) # secs
90  
91 conn.disconnect()

 

參考

什麼是分佈式消息中間件?

ActiveMQ-爲何須要消息中間件?

消息隊列的兩種模式

消息隊列使用的四種場景介紹

相關文章
相關標籤/搜索