I need to create a high-throughput ServiceBus queue client that can process 1000s of messages a second. The only way I've managed to achieve this is through a combination of ReceiveMode.ReceiveAndDelete
and PrefetchCount
.
var client = new QueueClient(connectionString, queueName, ReceiveMode.ReceiveAndDelete);
client.PrefetchCount = 1000;
client.RegisterMessageHandler(ProcessMessagesAsync, new MessageHandlerOptions());
await Task.Delay(10000); // wait for 10s while some messages are processed
await client.CloseAsync();
Reliability is not critical, so I can afford to lose messages if my application crashes. However, I would like to avoid losing messages during graceful shutdowns, such as the CloseAsync
call above. I particularly would want to avoid losing the hundreds of prefetched messages that are residing in the local cache. Is there a way of making the client wait until the prefetched messages have been processed, but stop receiving new ones?