Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving multiple messages from Queue without a loop

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.

like image 939
GoodSpeed Avatar asked Jun 07 '12 20:06

GoodSpeed


3 Answers

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.

like image 166
David Makogon Avatar answered Sep 21 '22 00:09

David Makogon


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)
like image 25
Minas Avatar answered Sep 20 '22 00:09

Minas


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/

like image 32
AvkashChauhan Avatar answered Sep 22 '22 00:09

AvkashChauhan