gpt4 book ai didi

c# - Rabbit-Mq在被拒绝后没有路由到死信队列

转载 作者:行者123 更新时间:2023-11-30 13:35:41 25 4
gpt4 key购买 nike

我目前正在研究 Rabbit-Mq,并试图实现一个“死信”队列,一个用于失败消息的队列。我一直在阅读兔子文档:https://www.rabbitmq.com/dlx.html .

并想出了这个例子:

internal class Program
{
private const string WorkerExchange = "work.exchange";
private const string RetryExchange = "retry.exchange";
public const string WorkerQueue = "work.queue";
private const string RetryQueue = "retry.queue";

static void Main(string[] args)
{
var factory = new ConnectionFactory { HostName = "localhost" };

using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(WorkerExchange, "direct");
channel.QueueDeclare
(
WorkerQueue, true, false, false,
new Dictionary<string, object>
{
{"x-dead-letter-exchange", RetryExchange},

// I have tried with and without this next key
{"x-dead-letter-routing-key", RetryQueue}
}
);
channel.QueueBind(WorkerQueue, WorkerExchange, string.Empty, null);

channel.ExchangeDeclare(RetryExchange, "direct");
channel.QueueDeclare
(
RetryQueue, true, false, false,
new Dictionary<string, object> {
{ "x-dead-letter-exchange", WorkerExchange },
{ "x-message-ttl", 30000 },
}
);
channel.QueueBind(RetryQueue, RetryExchange, string.Empty, null);

var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);

Thread.Sleep(1000);
Console.WriteLine("Rejected message");

// also tried channel.BasicNack(ea.DeliveryTag, false, false);
channel.BasicReject(ea.DeliveryTag, false);
};

channel.BasicConsume(WorkerQueue, false, consumer);

Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
}

发布到工作队列时的队列图像: rabbit-mq worker queue stats

重试队列的图像: rabbit-mq retry stats

我觉得好像遗漏了一些小细节,但似乎无法找到它们是什么。

提前致谢

最佳答案

您应该将死信交换定义为fanout

我们开始了:channel.ExchangeDeclare(RetryExchange, "fanout");

If your dead letter exchange is setup as DIRECT you must specify a dead letter routing key. If you just want all your NACKed message to go into a dead letter bucket for later investigation (as I do) then your dead letter exchange should be setup as a FANOUT.

Look at this for more info

关于c# - Rabbit-Mq在被拒绝后没有路由到死信队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48583893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com