如何使用python stomp.py client

时间:2016-12-16 22:01:06

标签: python activemq stomp

我正在使用 sompt.py来访问我的ActiveMQ服务器。

我创建了客户端并发送数据。但我必须Schedule Delay Message

import time
import sys

import stomp

class MyListener(stomp.ConnectionListener):
    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()
conn.set_listener('', MyListener())
conn.start()
conn.connect('admin', 'password', wait=True)

conn.subscribe(destination='/queue/test', id=1, ack='auto')

conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')

time.sleep(2)
conn.disconnect()

我查看了JAVA示例,用户可以通过headerssend也将headers作为stomp.py中的参数,但我不知道我必须传递哪个密钥。

stomp.constants没有任何延迟标题。

尝试了AMQ_SCHEDULED_DELAY标题,但似乎无效。

import time
import sys

import stomp

class MyListener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error "%s"' % message)
    def on_message(self, headers, message):
        print "Time for message receive: %s", time.strftime('%H:%M:%S')
        print('received a message "%s"' % message)

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.start()
conn.connect(wait=True)

conn.subscribe(destination='/queue/test', id=1, ack='auto')

print "Time for send message: %s", time.strftime('%H:%M:%S')
conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test', headers={'AMQ_SCHEDULED_DELAY': 100000})

time.sleep(2)
conn.disconnect()

输出:

test@localhost$ python /tmp/test.py this is test
Time for send message: %s 14:03:34
Time for message receive: %s 14:03:34
received a message "this is test"

1 个答案:

答案 0 :(得分:2)

通过STOMP发送预定消息的值与代理的Java代码中的值的定义相匹配,但作为快速参考,您可以快速查看Apache NMS STOMP站点example

值如下:

"AMQ_SCHEDULED_DELAY" = delay in milliseconds
"AMQ_SCHEDULED_PERIOD" = repeat period in milliseconds
"AMQ_SCHEDULED_REPEAT" = repeat count
"AMQ_SCHEDULED_CRON" = cron entry such as "0 * * * *"