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.Connection10([(ip...,61613)])  
conn.set_listener(‘‘, MyListener())
conn.start()
conn.connect()

conn.subscribe(destination=/queue/test, id=1, ack=auto)
#注意,官方示例这样发送消息是有问题的
#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()

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

文章来自:http://www.cnblogs.com/GarfieldTom/p/4153957.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3