在异常时重新排队消息

时间:2015-08-18 15:50:11

标签: c# rabbitmq publish-subscribe easynetq

我正在寻找一种可靠的方法来重新排列无法正常处理的信息 - 此时此时间。

我一直在关注http://dotnetcodr.com/2014/06/16/rabbitmq-in-net-c-basic-error-handling-in-receiver/,似乎支持在RabbitMQ API中重新排队消息。

else //reject the message but push back to queue for later re-try
{
    Console.WriteLine("Rejecting message and putting it back to the queue: {0}", message);
    model.BasicReject(deliveryArguments.DeliveryTag, true);
}

但我使用的是EasyNetQ。 所以想知道我将如何做类似的事情。

bus.Subscribe<MyMessage>("my_subscription_id", msg => {
    try
    {
        // do work... could be long running
    }
    catch ()
    {
        // something went wrong - requeue message
    }
});

这是一个好方法吗?如果ACK超过RabbitMQ服务器等待do work超时,则ACK消息不会导致问题。

2 个答案:

答案 0 :(得分:7)

所以我提出了这个解决方案。这取代了EasyNetQ的默认错误策略。

public class DeadLetterStrategy : DefaultConsumerErrorStrategy
{
    public DeadLetterStrategy(IConnectionFactory connectionFactory, ISerializer serializer, IEasyNetQLogger logger, IConventions conventions, ITypeNameSerializer typeNameSerializer)
    : base(connectionFactory, serializer, logger, conventions, typeNameSerializer)
    {
    }

    public override AckStrategy HandleConsumerError(ConsumerExecutionContext context, Exception exception)
    {
        object deathHeaderObject;
        if (!context.Properties.Headers.TryGetValue("x-death", out deathHeaderObject))
            return AckStrategies.NackWithoutRequeue;

        var deathHeaders = deathHeaderObject as IList;

        if (deathHeaders == null)
            return AckStrategies.NackWithoutRequeue;

        var retries = 0;
        foreach (IDictionary header in deathHeaders)
        {
            var count = int.Parse(header["count"].ToString());
            retries += count;
        }

        if (retries < 3)
            return AckStrategies.NackWithoutRequeue;
        return base.HandleConsumerError(context, exception);
    }
}

你可以这样替换它:

RabbitHutch.CreateBus("host=localhost", serviceRegister => serviceRegister.Register<IConsumerErrorStrategy, DeadLetterStrategy>())

您必须使用AdvancedBus,因此您必须手动设置所有内容。

using (var bus = RabbitHutch.CreateBus("host=localhost", serviceRegister => serviceRegister.Register<IConsumerErrorStrategy, DeadLetterStrategy>()))
{
    var deadExchange = bus.Advanced.ExchangeDeclare("exchange.text.dead", ExchangeType.Direct);
    var textExchange = bus.Advanced.ExchangeDeclare("exchange.text", ExchangeType.Direct);
    var queue = bus.Advanced.QueueDeclare("queue.text", deadLetterExchange: deadExchange.Name);
    bus.Advanced.Bind(deadExchange, queue, "");
    bus.Advanced.Bind(textExchange, queue, "");

    bus.Advanced.Consume<TextMessage>(queue, (message, info) => HandleTextMessage(message, info));
}

这将死信3次失败。之后,它将转到EasyNetQ提供的默认错误队列以进行错误处理。 您可以订阅该队列。

当异常传播出您的使用者方法时,消息将被删除。所以这会引发一封死信。

static void HandleTextMessage(IMessage<TextMessage> textMessage, MessageReceivedInfo info)
{
    throw new Exception("This is a test!");
}

答案 1 :(得分:3)

据我所知,无法通过EasyNetQ手动<script> function validate(){ if(!document.getElementById("AcceptTerms").checked) alert("You should accept terms in order to proceed!"); else alert("Proceed."); } </script> <html> <label for="AcceptTerms">Accept Terms</label> <input class="required" id="AcceptTerms" name="AcceptTerms" type="checkbox" aria-required="true"/> <input type="button" value="Submit" onclick="validate();"/> </html> acknack发送消息。

我看到你有opened an issue ticket with the EasyNetQ team,关于这个...但是还没有答案。

FWIW,这是一件非常合适的事情。我使用的所有库都支持此功能集(在NodeJS中),这很常见。我很惊讶EasyNetQ并不支持这一点。