python+stomp+activemq


python也可以連接MQ,以ActiveMQ為例,安裝stomp.py:

https://github.com/jasonrbriggs/stomp.py

下載后安裝:

python setup.py install

很簡單,然后一個簡單的示例:

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' % message)

#官方示例的連接代碼
#
conn = stomp.Connection([('ip...',61613)])
conn.set_listener('', MyListener())
conn.start()
conn.connect()

conn.subscribe(destination='/queue/test', id=1, ack='auto')
#注意,官方示例這樣發送消息的  $ python simple.py hello world
#conn.send(body='hello,garfield! this is '.join(sys.argv[1:]), destination='/queue/test')
conn.send(body='hello,garfield!', destination='/queue/test')

time.sleep(2)
conn.disconnect()

好了,可以發送、接收消息了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM