如何从队列中删除消息? [英] How can I remove messages from a queue?

查看:241
本文介绍了如何从队列中删除消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的邮件被卡在队列中,我正在寻找一种以编程方式删除它们的方法.

I have messages that get stuck in queue and I am looking for a way to programmatically remove them.

是否有一种方法可以将队列中已放置超过x天的邮件从队列中删除? 我可以像这样连接和删除队列,但是不确定如何删除单个消息.

Is there a way to remove messages from a queue if it has been sitting for more than x days? I can connect and delete a queue like this, but not sure how to remove individual messages.

MessageQueue queue = new MessageQueue(@".\private$\SomeTestName");
//queue.Purge(); //deletes the entire queue
try
{
    // Peek and format the message. 
    Message m = myQueue.Peek();

   // Display message information.
   Console.WriteLine("Sent time {0}", m.SentTime);
   Console.WriteLine("Arrived time {0}", m.ArrivedTime);
}

推荐答案

没有可用的API来执行此操作.但是您可以使用

There is no API available to do this. But you can use

使用枚举的好处是,如果队列中有许多消息,则读取所有消息可能会导致OutOfMemoryException.使用枚举器一次只能读取1条消息,分配给它的内存可以重复使用.

A benefit of using enumeration is that if a queue has many messages, reading all of them may result in OutOfMemoryException. With enumerator you only read 1 message at a time, and memory allocated for it can be reused.

提高性能的另一个技巧是指定要读取的属性,这样,如果邮件正文很大而您对内容不感兴趣,则可以禁用对其进行读取.

Another trick to increase performance is to specify which properties to read, so that if the message body is large and you aren't interested in the content, you can disable reading it.

var enumerator = _queue.GetMessageEnumerator2();  // get enumerator
var staleDate = DateTime.UtcNow.AddDays(-3);      // take 3 days from UTC now    
var filter = new MessagePropertyFilter();         // configure props to read
filter.ClearAll();                                // don't read any property
filter.ArrivedTime = true;                        // enable arrived time
_queue.MessageReadPropertyFilter = filter;        // apply filter

// untested code here, edits are welcome
while (enumerator.Current != null)    
     if(enumerator.Current.ArrivedTime.Date >= staleDate)
         enumerator.RemoveCurrent();
     else
         enumerator.MoveNext();

这篇关于如何从队列中删除消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆