how can i get all the available messages on a MSMQ Queue
Asked Answered
G

4

5

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

Gammadion answered 4/8, 2009 at 16:55 Comment(0)
C
9

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);
}
Collaboration answered 4/8, 2009 at 17:50 Comment(0)
E
2

For simple stuff...

public void DoIt()
    {

        bool continueToSeekForMessages = true;

        while (continueToSeekForMessages)
        {
            try
            {
                var messageQueue = new System.Messaging.MessageQueue(@"FormatName:Direct=OS:MyComputerNameHere\Private$\MyPrivateQueueNameHere");
                var message = messageQueue.Receive(new TimeSpan(0, 0, 3));
                message.Formatter = new System.Messaging.XmlMessageFormatter(new String[] { "System.String,mscorlib" });
                var messageBody = message.Body;

            }
            catch (Exception ex)
            {
                continueToSeekForMessages = false;
            }
        }
    }

.

Also, could use peek instead of taking the message off the queue.

Also, could use GetMessageEnumerator2

Easel answered 6/2, 2014 at 21:13 Comment(0)
P
0

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.

Primogeniture answered 4/8, 2009 at 17:10 Comment(1)
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 haveGammadion
R
0

Latest versions of MSMQ also has the following feature:

You can get all messages in a single object such as follows: (Write your own "ReceiveCompleted event" handler)

private static void MyReceiveCompleted(Object source,
        ReceiveCompletedEventArgs asyncResult)
    {
        MessageQueue mq = (MessageQueue)source;
        try
        {
            Message[] mm = mq.GetAllMessages();
            foreach (Message m in mm)
            {
            // do whatever you want
            }
        }
        catch (MessageQueueException me)
        {
            Console.WriteLine(me.Message);
        }
        finally
        {

        }


        return;
    }
Robbynrobe answered 2/6, 2014 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.