MessageLockLostException中断了服务总线QueueClient

时间:2018-09-28 07:58:06

标签: c# azure azureservicebus

我有监听服务总线队列的服务。几天后,我会收到MessageLockLostException。异常本身对我来说很明显,这不是问题。但是,在收到异常后,QueueClient似乎已损坏,并停止处理任何其他消息。我了解我可以将try / catch添加到ProcessMessagesAsync中。但是我认为ExceptionReceivedHandler应该处理这种情况,并且QueueClient应该尝试再次处理同一条消息(如果多次处理失败,则将其发送到DLQ)。

public class QueueService
{
    private readonly QueueClient _queueClient;

    public QueueServiceServiceBus(string connectionString, ILogger<QueueServiceServiceBus> logger)
    {
        _logger = logger;
        var serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString);
        _queueClient = new QueueClient(serviceBusConnectionStringBuilder);

        RegisterMessageHandler();
    }

    private Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
    {
        _logger.LogError("Message handler encountered an exception {Exception}",
            exceptionReceivedEventArgs.Exception);

        return Task.CompletedTask;
    }        

    private async Task ProcessMessagesAsync(Message message, CancellationToken token)
    {
        //Handle message

        await _queueClient.CompleteAsync(message.SystemProperties.LockToken);
    }

    private void RegisterMessageHandler()
    {
        var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
        {
            AutoComplete = false
        };

        _queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
    }
}

1 个答案:

答案 0 :(得分:0)

ExceptionReceivedHandler处理在消息的检索和处理期间发生的异常。

QueueClient不会因任何原因而中断,除非它因过载而受到限制。

我尝试了您提供的样本。它对我来说很好用,ExceptionReceivedHandler处理MessageLockLostException而不会中断。