简体   繁体   English

如何在MSMQ队列上获取所有可用消息

[英]how can i get all the available messages on a MSMQ Queue

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 我似乎无法找到任何好的资源,它将告诉我如何创建一个简单的方法来返回IEnnumerable例如

Thanks 谢谢

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. 虽然我同意Nick的看法,队列的目的更多是用于FIFO样式处理,而ArsenMkrt的解决方案也可以使用,另一个选择是使用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);
}

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. 此外,可以使用peek而不是将消息从队列中取出。

Also, could use GetMessageEnumerator2 此外,可以使用GetMessageEnumerator2

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. 队列应该保持消息的顺序,所以你必须循环并继续拉出第一个消息。

Latest versions of MSMQ also has the following feature: 最新版本的MSMQ还具有以下功能:

You can get all messages in a single object such as follows: (Write your own "ReceiveCompleted event" handler) 您可以在单个对象中获取所有消息,如下所示:(编写您自己的“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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM