Azure ServiceBusProcessor 需要很长时间才能停止

Azure ServiceBusProcessor takes a long time to stop

我正在尝试通过 Azure 服务总线库发送消息 Azure.Messaging.ServiceBus。我正在关注 this tutorial 和 sending/processing 一条消息。

调用 processor.StopProcessingAsync() 时,操作大约需要一分钟(每次)。在 Azure 门户中查看时,所有消息均已处理。我不知道为什么即使队列中没有消息,处理器也需要这么长时间才能停止。

似乎每次都花费(完全)相同的时间。如果有人能指出为什么需要这么长时间以及如何减少它 (configuration/setup?),我将非常感激。提前致谢!

好的。在查看源代码时,我发现接收器启动后有一个默认的“等待时间”,即 60 秒。这可以通过在 ServiceBusClientOptions.ServiceBusRetryOptions.

上设置 TryTimeout 来降低

参见: ServiceBusRetryOptions AmqpReceiver.ReceiveMessagesAsyncInternal

I tested this, it works as expected:

var clientOptions = new ServiceBusClientOptions();
clientOptions.RetryOptions.TryTimeout = new TimeSpan(0, 0, 5);
await using var client = new ServiceBusClient(connectionString, clientOptions);

sets the timeout to 5 seconds.