我已经安装了从这里发送延迟消息的插件 rabbitmq-delayed-message-exchange .
我找不到在 python 中使用它的任何帮助。我刚开始使用 rabbitmq 。
这是我一直在尝试的:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare("test-x", type="x-delayed-message", arguments={"x-delayed-type":"direct"})
channel.queue_declare(queue='task_queue',durable=True)
channel.queue_bind(queue="task_queue", exchange="test-x", routing_key="task_queue")
channel.basic_publish(exchange='test-x',routing_key='task_queue',body='Hello World! Delayed',arguments={"x-delay":100})
print(" [x] Sent 'Hello World! Delayed'")
connection.close()
以下是列出的交易所:
sudo rabbitmqctl list_exchanges
Listing exchanges ...
amq.direct direct
test-x x-delayed-message
amq.fanout fanout
amq.match headers
amq.headers headers
direct
amq.rabbitmq.trace topic
amq.topic topic
amq.rabbitmq.log topic
我不知道如何将延迟参数传递给 basic_publish 函数
感谢任何帮助
最佳答案
您需要将 x-delay
header 添加到您的消息属性中,并以毫秒为单位指定延迟值。试试这个:
channel.basic_publish(
exchange='test-x',
routing_key='task_queue',
body='Hello World! Delayed',
properties=pika.BasicProperties(headers={"x-delay": 1000})
)
关于python - 我如何使用 rabbitmq-delayed-message-exchange 插件在 rabbitmq 中发送延迟消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35449234/