Reputation: 33964
I am working on Azure queue. I need to get, process and delete all queue messages. What I am doing right now is calling the GetMessage, process the message and calling DeleteMessage one by one.
var message = _queue.GetMessage();
if (message == null)
{
return;
}
// processs
_queue.DeleteMessage(message);
Is there is a way to get all messages first then process it and delete all these processed messages?
Upvotes: 1
Views: 714
Reputation: 136146
You can't get all messages from a queue in a single call. Maximum number of messages you can Get from a queue in a single call is 32
. So what you would need to do is something like:
var messages = _queue.GetMessages(32);
and then process these messages instead of getting one message at a time.
UPDATE
So a few things based on your comments:
ApproximateMessages
which will tell you approximately how many messages are there in the queue. This should give you an idea about the total number of messages.Based on these, do take a look at pseudo code below:
do
{
var messages = _queue.GetMessages(32);
foreach (var msg in messages)
{
ProcessMessage(msg);
DeleteMessage(msg);
}
var approximateMessagesCount = _queue.FetchAttributes().ApproximateMessageCount.Value;
if (approximateMessagesCount == 0)
{
break;
}
} while (true);
Basically you have to keep on fetching messages from the queue (32 at a time), process individual message and once the message is processed then delete it. Once these 32 messages have been processed and deleted, you have to check if there are any more messages in the queue. If there are messages, you would repeat this process. If there are no messages, then you would exit out of the loop.
Upvotes: 1