tags:

views:

291

answers:

3

Whats the best way to get all the messages currently on a queue to process?

We have a queue with a large number of very small messages, what I would like to do is read all the current messages off and then send them through a thread pool for processing.

I can't seem to find any good resource which will show me how i can create a simple method to return an IEnnumerable for example

Thanks

+1  A: 

take a look here

ArsenMkrt
A: 

Doesn't that defeat the purpose of the queue? The queue is supposed to keep the order of the messages, so you have to loop through and keep pulling the first message off.

Nick
You're right it does, unfortunatly I wasnt involved in the initial design of the system so we are looking for options to make it work a little better with the services we have
Kev Hunter
+1  A: 

Although I agree with Nick that the queue's purpose is more for FIFO style processing, and ArsenMkrt's solution will work, another option involves using a MessageEnumerator and piling the messages into your IEnumerable.

var msgEnumerator = queue.GetMessageEnumerator2();
var messages = new List<System.Messaging.Message>();
while(msgEnumerator.MoveNext(new TimeSpan(0, 0, 1))
{
    var msg = queue.ReceiveById(msgEnumerator.Current.Id, new TimeSpan(0, 0, 1));
    messages.Add(msg);
}
AJ