1
votes

I wrote consumer using MassTransit and using Azure Service Bus as transport.

public async Task Consume(ConsumeContext<ISimpleRequest> context)
{
    try
    {
        _log.InfoFormat("Strated working on {0}", context.Message.CustomerId);
        Thread.Sleep(500);
        _log.InfoFormat("Returning name for {0}", context.Message.CustomerId);
    }
    catch(Exception ex) 
    {
        await context.Redeliver(TimeSpan.FromSeconds(1));
    }
}

I redeliver message to another consumer if exception happens.
But what happens if bus sends a message, the consumer starts working on it and process is shut down? How can I not lose the message?

1
Consuming Faults part you mean? - Oleksandr Hrebeniuk
Failed messages are put on an error queue - "With a default bus configuration, the exception is caught … and the message is moved to an _error queue (prefixed by the receive endpoint queue name). The exception details are stored as headers with the message, for analysis and to assist in troubleshooting the exception". You can also consume the fault message to get notification of the failure, rather than by checking the error queues. - stuartd
With your current code, though, I'm not sure this will happen, as you're calling Redeliver in your exception handling. You would be better off using a retry policy, and then if it still fails allow the exception to fault the consumer. - stuartd
I'm talking not about exception, I talk about stop application when consumer got message. It is the same? I'll get message in error queue? - Oleksandr Hrebeniuk

1 Answers

0
votes

Answer from google group:

Messages are confirmed (ACKed) to the transport only after the consumer has completed its work. So for any unfinished consumer, would it be application stop or exception in the consumer, the message will remain in the queue. This is different though if speaking about proper terminations, since once you require busHandle to run Stop() before terminating the application, it will wait for all messages that are currently being processed, to run through the pipeline before allowing the bus to be stopped.