我想在 rabbit MQ 中实现 DLX,它设置失败消息的重试次数。如果重试次数大于 5,则消息将被自动丢弃。
下面是 DLX 的片段。
string WORK_EXCHANGE = "DeadExchange"; // dead letter exchange
string RETRY_QUEUE = "RetryQueue";
int RETRY_DELAY = 10000;
var queueArgs = new Dictionary<string, object> {
{ "x-dead-letter-exchange", WORK_EXCHANGE },
{ "x-message-ttl", RETRY_DELAY }
};
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
channel.ExchangeDeclare(exchange: WORK_EXCHANGE,
type: ExchangeType.Direct);
channel.QueueBind(queue: queueName, exchange: WORK_EXCHANGE, routingKey: routingKey, arguments: null);
channel.ExchangeDeclare(exchange: RETRY_EXCHANGE,
type: ExchangeType.Direct);
channel.QueueDeclare(queue: RETRY_QUEUE, durable: true, exclusive: false, autoDelete: false, arguments: queueArgs);
channel.QueueBind(queue: RETRY_QUEUE, exchange: RETRY_EXCHANGE, routingKey: routingKey, arguments: null);
channel.BasicPublish(exchange: RETRY_EXCHANGE, routingKey: routingKey, mandatory: true, basicProperties: properties, body: message);
我想读取 x-Death 属性,如下面的队列消息所示。
到目前为止我已经达到了
(BasicDeliveryEventArgs)ea.BasicProperties.Headers["x-death"]
最佳答案
您可以通过这种方式检索计数:
private long? GetRetryCount(IBasicProperties properties)
{
if (properties.Headers.ContainsKey("x-death"))
{
var deathProperties = (List<object>)properties.Headers["x-death"];
var lastRetry = (Dictionary<string, object>)deathProperties[0];
var count = lastRetry["count"];
return (long)count;
}
else
{
return null;
}
}
希望对大家有用
关于c# - 在C#中的rabbit Mq中获取xDeath中队列消息的最大重试次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58993088/