JoBo
JoBo

Reputation: 347

ServiceBusReceiver PeekMessageAsync only returns maximum of 250 dlq-messages?

I am reading from a deadletter queue on servicebus using the ServiceBusReceiver(which I want to continue using).

However, the receiver.PeekMessagesAsync(550) only fetches a maximum of 250 messages from deadletter queue??? How can I change this? I already have it set to fetch 550 messages so not sure how to do it.

        string topic = "myTopic";
        string subscriptionAndDeadletterPath = @"mySubscription/$deadletterqueue";
        var paginationSize = 1000;
        var dlqCount = 0;
        var numberOfMessagesToFetchFromDlq = 550;

        try
        {
            await using var client = new ServiceBusClient(connectionString);
            var receiver = client.CreateReceiver(topic, subscriptionAndDeadletterPath, new ServiceBusReceiverOptions() { ReceiveMode = ServiceBusReceiveMode.PeekLock, PrefetchCount = paginationSize });
           
            var messagesFromDlq = await receiver.PeekMessagesAsync(numberOfMessagesToFetchFromDlq);
            dlqCount = messagesFromDlq.Count();
       

dlqCount will not exceed 250 for some reason? The int maxMessages parameter doesn't seem to work above this number?

Upvotes: 0

Views: 2689

Answers (2)

JoBo
JoBo

Reputation: 347

This is how I solved it for now:

        string topic = "myTopic";
        string subscriptionAndDeadletterPath = @"mySubscription/$deadletterqueue";
        var sequence = 0;
        var continueWithNextIteration = true;
        var paginationSize = 100;
        var numberOfMessagesToFetchFromDlq = 250

            await using var client = new ServiceBusClient(connectionString);
            var receiver = client.CreateReceiver(topic, subscriptionAndDeadletterPath, new ServiceBusReceiverOptions() { ReceiveMode = ServiceBusReceiveMode.PeekLock, PrefetchCount = paginationSize });

            do
            {
                var messagesFromDlq = await receiver.PeekMessagesAsync(numberOfMessagesToFetchFromDlq, sequence);
                if (messagesFromDlq.Count() > 249)
                {
                    var nextSequenceSetting = await receiver.PeekMessagesAsync(2, Convert.ToInt32(messagesFromDlq.Last().SequenceNumber));
                    sequence = Convert.ToInt32(nextSequenceSetting.Last().SequenceNumber);
                    continueWithNextIteration = nextSequenceSetting.Count() > 1 ? true : false;
                }
             
            } while (continueWithNextIteration);

Upvotes: 3

Jesse Squire
Jesse Squire

Reputation: 7860

The number of messages that you're passing to PeekMessagesAsync is the maximum number of messages that will be returned. There is no way to guarantee a minimum size for the batch; the number of messages included depends on several factors of which the most impactful is how quickly the network can stream messages to the client.

My answer to Optmize receiving messages from Azure Service Bus in batch mode discusses the context in more detail and offers some thoughts that may apply to your scenario as well.

It is important to note that there are some behavioral differences with peeked messages when compared to received messages. The most impactful is that peeked messages are not locked and can't be settled, so considerations around lock token expiration wouldn't apply here.

Upvotes: 2

Related Questions