使用python stomp api接收消息

时间:2012-05-30 01:03:35

标签: python api stomp subscribe

我可以找到python stomp使用者订阅和接收消息的工作示例(来自activemq broker)。我检查了python stomp API,但它似乎不支持像conn.receive这样的东西。无论如何,我可以达到同样的效果。

#!/usr/bin/env python
import stomp
import time
import logging
import sys
import random

logging.basicConfig()

class MyListener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error %s' % message)
    def on_message(self, headers, message):
        for k,v in headers.iteritems():
            print('header: key %s , value %s' %(k,v))
        print('received message\n %s'% message)


dest='/topic/manager'
conn=stomp.Connection([('localhost',9998)])
print('set up Connection')
conn.set_listener('somename',MyListener())
print('Set up listener')

conn.start()
print('started connection')

conn.connect(wait=True)
print('connected')
conn.subscribe(destination=dest, ack='auto')
print('subscribed')

message='hello cruel world'
conn.send(message=message, destination=dest,headers={'seltype':'mandi-age-to-man','type':'textMessage','MessageNumber':random.randint(0,65535)},ack='auto')
print('sent message')
time.sleep(2)
print('slept')
conn.disconnect()
print('disconnected')

0 个答案:

没有答案