简体   繁体   English

接收MSMQ消息作为列表

[英]Receiving MSMQ messages as a list

Just a small question that whether a list would be able to store large amount of messages. 只是一个小问题,即列表是否能够存储大量消息。

My code is like this, there are 50,000 queue messages which I am receiving and assigning to messages list. 我的代码是这样的,我正在接收并分配给消息列表的队列消息有50,000条。

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

foreach(var k in messages) {
    MailMessage mailM = (k.Body as SerializeableMailMessage).GetMailMessage();

    try {
        SmtpClient sp = new SmtpClient(smtpip, 25);
        sp.EnableSsl = false;
        sp.Send(mailM);
    }   
    catch (Exception ex) {
        logger.ErrorException("General error sending email.", ex);
    }
}

Is this the correct way, or is there an alternative for this? 这是正确的方法,还是有其他替代方法? Let me know your suggestions and help! 让我知道您的建议和帮助!

Seems all fine... It gives often multiple ways to programm the exact same thing, but yours is already pretty good ;) 似乎一切都很好...它常常提供多种方法来编程完全相同的东西,但是您的已经相当不错了;)

I personally would rather prefer to use a for loop, but my know league isnt so high msmq, soooo... 我个人更喜欢使用for循环,但是我知道联盟的msmq并不是很高,所以...

Next level of this is programming a Keylogger-Bypass-injection or how it is called... (I hope i didnt missunderstood your intention with this code). 下一步是对Keylogger-Bypass-injection或它的调用方式进行编程...(我希望我不会因为这段代码而误解了您的意图)。

First of all, when you are doing RecieveById, you remove the message from the queue. 首先,在执行RecieveById时,您将从队列中删除该消息。 And if your software crashes after reading all the messages from the queue, they are gone forever. 而且,如果您的软件在从队列中读取所有消息后崩溃,那么它们将永远消失。

You should be using BeginPeek and create event handler OnPeekCompleted to handle new messages from the queue. 您应该使用BeginPeek并创建事件处理程序OnPeekCompleted来处理来自队列的新消息。 This way you only handle 1 message at a time and are not that susceptible to such errors when just reading all and then handling them. 这样,您一次只处理一条消息,而在阅读所有内容然后进行处理时就不会受到此类错误的影响。

private static void Main()
{
    var queue = new MessageQueue("myQueue");
    queue.PeekCompleted += new PeekCompletedEventHandler(OnPeekCompleted);

    // Start listening to queue
    queue.BeginPeek();
}

private void OnPeekCompleted(object sender, PeekCompletedEventArgs e)
{
    var cmq = (MessageQueue)sender;
    try
    {
        var msmqMessage = cmq.EndPeek(e.AsyncResult);

        msmqMessage.Formatter = new XmlMessageFormatter(typeof(messagetypehere));

        var message = msmqMessage.Body;

        // Do logic for the message
        {
            // Send mail or what ever

        }

        cmq.Receive(); // Remove the message from the queue
    }
    catch (Exception ex) 
    {
        // Log or other
    }

    // Refresh queue
    cmq.Refresh();

    // Start listening to queue
    cmq.BeginPeek();
}

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

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