C# 获取C中兔子Mq中xDeath中队列消息的最大重试次数#
我想在rabbit MQ中实现DLX,它设置失败消息的重试次数。如果重试次数大于5,则消息将自动丢弃 下面是DLX的代码片段C# 获取C中兔子Mq中xDeath中队列消息的最大重试次数#,c#,rabbitmq,dead-letter,C#,Rabbitmq,Dead Letter,我想在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 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);
您可以通过以下方式检索计数:
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;
}
}
private long?GetRetryCount(IBasicProperties)
{
if(properties.Headers.ContainsKey(“x-death”))
{
var deathProperties=(List)properties.Headers[“x-death”];
var lastRetry=(字典)死亡属性[0];
var count=lastRetry[“count”];
返回(长)计数;
}
其他的
{
返回null;
}
}
我希望它对某人有用
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;
}
}