Receiving multiple messages from Queue without a loop
Asked Answered
R

3

5

I'm using Azure Service Bus to manage messages with a webrole and workerrole.

I need to know how can i get multiple messages from queue at once without using a loop.

Redblooded answered 7/6, 2012 at 20:39 Comment(3)
Why do you need to do it without looping?Directly
for example i want to receive 10 messages every time i call a functionRedblooded
yes, but why? Can't you just loop 10 times and read one message each time through?Directly
D
5

Service Bus queues don't have a way of retrieving more than one message at a time, but you can set up a prefetch, where messages will be cached (resulting in faster performance). More info on prefetch here.

If you really need to bulk-read messages, consider what @AvkashChauhan pointed out: Windows Azure Storage queues support up to 32 messages to be read at once, in a single transaction. You'll need to individually delete each queue message; there's no batch delete.

Diehl answered 7/6, 2012 at 23:37 Comment(0)
A
4

You can use the ReceiveBatch method of the Microsoft.ServiceBus.Messaging :

private MessageReceiver messageReceiver;
var brokeredMessagesList = messageReceiver.ReceiveBatch(100);

You can put a lock on the queue until processing of the received batch is completed and after you are done with your processing, you can call the CompleteBatch to release the lock on the queue:

                List<Guid> messageLockTokenList = new List<System.Guid>();
                foreach(BrokeredMessage message in brokeredMessagesList)
                {
                    messageLockTokenList.Add(message.LockToken);
                }
                messageReceiver.CompleteBatch(messageLockTokenList)
Albumin answered 30/6, 2014 at 2:41 Comment(0)
A
3

When retrieving messages from a queue, batch multiple messages together in a single storage transaction. The GetMessages method in the Queue Service API enables de-queuing the specified number of messages in a single transaction

When retrieving messages via the GetMessages method, the maximum batch size supported by Queue Service API in a single dequeue operation is limited to 32. Exceeding this limit will cause a runtime exception.

Visit here for more details: http://windowsazurecat.com/2010/12/best-practices-for-maximizing-scalability-and-cost-effectiveness-of-queue-based-messaging-solutions-on-windows-azure/

Ansela answered 7/6, 2012 at 23:10 Comment(2)
I think the OP was asking about Service Bus queues, not Windows Azure Storage queues.Diehl
But +1 because it's really the only way to bulk-read messages from a queue.Diehl

© 2022 - 2024 — McMap. All rights reserved.