How can I get all available messages in the MSMQ queue

What is the best way to get all the messages that are currently being processed in the queue?

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

I cannot find a good resource that will show me how I can create a simple method to return IEnnumerable, for example

thanks

+3
source share
4 answers

Although I agree with Nick that queuing is more for handling FIFO style andArsenMkrt , MessageEnumerator 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);
}
+9

...

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;
            }
        }
    }

.

, peek , .

, GetMessageEnumerator2

+2

? , , .

0

MSMQ :

, : ( ReceiveCompleted)

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;
    }
0

Source: https://habr.com/ru/post/1714325/


All Articles