使用python stomp api接收消息

2024-04-29 04:22:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以找到python stomp消费者订阅和接收消息(来自activemq代理)的工作示例。我检查了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')

Tags: importselfmessagetimeonloggingdefrandom