【问题标题】:How to schedule delay message in activemq using python stomp.py client如何使用 python stomp.py 客户端在 activemq 中安排延迟消息
【发布时间】:2017-04-26 03:06:27
【问题描述】:

我正在使用 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"

【问题讨论】:

  • 只是为了其他读者,是的,你做的是正确的事情。正如下面的讨论,我相信消息代理没有启用调度程序。

标签: python python activemq stomp


【解决方案1】:

通过 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 * * * *"

【讨论】:

  • 嗨蒂姆,但是如果我们在apache 中更改它,那么它将适用于所有消息吗?我只需要使用delay 发送几条消息,所有其他消息都必须正常执行
  • 我不太明白你的问题,当你想应用延迟时,你把标题放在你的 STOMP 消息上,不确定你问的是什么 apache 是关于
  • 如果我使用conn.send(...,..., headers={'AMQ_SCHEDULED_DELAY': 10}) 那么这将安排我的消息在10 秒后发送?
  • 延迟设置为毫秒,所以不,它会延迟 10 毫秒。
  • 确保在代理上启用了调度程序支持
猜你喜欢
  • 1970-01-01
  • 2015-09-29
  • 2015-07-15
  • 2021-06-07
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
相关资源
最近更新 更多