我最近发现了允许您延迟消息的 RabbitMQ 功能,并且效果很好,尽管我找不到与我需要的类似的示例:
假设有 3 种类型的消息:A、B 和 C。我们有 2 个具有 1 小时和 2 小时'x-message-ttl
值的延迟队列。还有 3 种类型的destination_queues - 每种用于特定的消息类型。
我想要实现的是,在 delay_queues 之一中的消息达到其 TTL 后,它将根据其类型被路由到 destination_queues 之一。像这样的东西:
这甚至可以使用 RabbitMQ 消息属性吗?有任何想法吗?我的代码将消息发送到延迟队列(到期后它们被发送到 hello 队列):
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
channel.confirm_delivery()
channel.queue_declare(queue='hello', durable=True)
channel.queue_bind(exchange='amq.direct',
queue='hello')
delay_channel = connection.channel()
delay_channel.confirm_delivery()
delay_channel.queue_declare(queue='hello_delay', durable=True, arguments={
'x-message-ttl' : 3600000,
'x-dead-letter-exchange' : 'amq.direct',
'x-dead-letter-routing-key' : 'hello'
})
while 1 :
delay_channel.basic_publish(exchange='',
routing_key='hello_delay',
body="test",
properties=pika.BasicProperties(delivery_mode=2))
print "Sent to delay queue"