python - 如何使用 python stomp.py 客户端在 activemq 中安排延迟消息

标签 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 示例,其中用户可以传递 header send 也将 headers 作为 stomp.py 中的参数,但我不知道我必须在其中传递哪个键。

stomp.constants没有任何延迟 header 。

尝试了 AMQ_SCHEDULED_DELAY header ,但似乎不起作用。

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"

最佳答案

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

关于python - 如何使用 python stomp.py 客户端在 activemq 中安排延迟消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41193036/

相关文章:

http - 为什么 HTTP 不是消息传递协议(protocol)? (根据 RabbitMQ)

python - 我可以在不重新打开文件的情况下从打开的文件中读取新数据吗?

python - 如何在wxpython中制作 Canvas (矩形)?

java - 当我使用 Spring 和 MQ Queue 发送 OBject 消息时,为什么会收到 JMSBytesMessage

java - JMS消息重新传递

java - ActiveMQ 配置和 Spring 表达式语言 (SpEL)

python - 错误 : out of bounds when searching for identical rows in a numpy array

python - 如何使用 Python 使用 Azure 文件存储的获取文件属性 REST API 获取 "content_type"

spring - 使用 Stomp 向同一主题发送多条消息

java - 如果使用 spring + stomp + 带有rabbitmq的完整代理,如何自动删除队列