1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// 延迟队列:消息过期后进入死信
var delayArgs = new Dictionary<string, object>
{
{ "x-dead-letter-exchange", "order.dlx" },
{ "x-dead-letter-routing-key", "order.timeout" },
{ "x-message-ttl", 1800000 } // 30 分钟(毫秒)
};
channel.QueueDeclare("order-wait-queue", durable: true, exclusive: false, autoDelete: false, delayArgs);
// 死信交换机和消费队列
channel.ExchangeDeclare("order.dlx", ExchangeType.Direct);
channel.QueueDeclare("order-timeout-queue", durable: true, exclusive: false, autoDelete: false, null);
channel.QueueBind("order-timeout-queue", "order.dlx", "order.timeout");
// 发送延迟消息
channel.BasicPublish("", "order-wait-queue", null, orderBody);
// 30 分钟后,消息过期进入 order-timeout-queue 被消费
|