python 發(fā)送和接收ActiveMQ消息的實例
ActiveMQ是java開發(fā)的消息中間件服務。可以支持多種協(xié)議(AMQP,MQTT,OpenWire,Stomp),默認的是OpenWire。而python與ActiveMQ的通信使用的是Stomp協(xié)議。而如果你的服務沒有開啟則需要配置開啟。
首先需要安裝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()
但是上述只是發(fā)送文本類型的消息,除此之外,ActiveMQ還支持MapMessage、ObjectMessage、BytesMessage、和StreamMessage等多個消息類型。
以上這篇python 發(fā)送和接收ActiveMQ消息的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python如何實現(xiàn)大型數(shù)組運算(使用NumPy)
- python3通過udp實現(xiàn)組播數(shù)據(jù)的發(fā)送和接收操作
- Python udp網(wǎng)絡程序實現(xiàn)發(fā)送、接收數(shù)據(jù)功能示例
- Python 網(wǎng)絡編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
- python UDP(udp)協(xié)議發(fā)送和接收的實例
- Python微信企業(yè)號開發(fā)之回調(diào)模式接收微信端客戶端發(fā)送消息及被動返回消息示例
- python通過get,post方式發(fā)送http請求和接收http響應的方法
- Python中使用socket發(fā)送HTTP請求數(shù)據(jù)接收不完整問題解決方法
- python發(fā)送郵件接收郵件示例分享
- Python如何發(fā)送與接收大型數(shù)組
相關文章
在Python中利用Into包整潔地進行數(shù)據(jù)遷移的教程
這篇文章主要介紹了在Python中如何利用Into包整潔地進行數(shù)據(jù)遷移,在數(shù)據(jù)格式的任意兩個格式之間高效地遷移數(shù)據(jù),需要的朋友可以參考下2015-03-03
Python編程語言的35個與眾不同之處(語言特征和使用技巧)
這篇文章主要介紹了Python編程語言的35個與眾不同之處,Python編程語言的語言特征和使用技巧,需要的朋友可以參考下2014-07-07
python連接、操作mongodb數(shù)據(jù)庫的方法實例詳解
這篇文章主要介紹了python連接、操作mongodb數(shù)據(jù)庫的方法,結合實例形式詳細分析了Python針對MongoDB數(shù)據(jù)庫的連接、查詢、排序等相關操作技巧,需要的朋友可以參考下2019-09-09
極簡Python庫CherryPy構建高性能Web應用實例探索
今天為大家介紹的是 CherryPy,它是一個極簡、穩(wěn)定且功能強大的Web框架,可以幫助開發(fā)者快速構建高性能的 Web 應用程序,使用 CherryPy,你可以輕松地創(chuàng)建RESTful API、靜態(tài)網(wǎng)站、異步任務和 WebSocket 等應用2024-01-01

