转载一篇老外的消息队列服务文章
2014-10-25 12:55
246 查看
接着上一篇:/article/1740840.html
原文:http://www.codeproject.com/Articles/821220/Throwing-a-Great-Block
原文:http://www.codeproject.com/Articles/821220/Throwing-a-Great-Block
using System; namespace MessagingQueueing { using System.Collections.Concurrent; using System.Threading; class Program { static void Main(string[] args) { using (MyQueue queue = new MyQueue()) { for (int msgIdx = 1; msgIdx < 21; msgIdx++) { queue.AddLog(new MyMessage { MessageId = msgIdx, Message = string.Format("Message text # {0:#,##0}", msgIdx) }); } } Console.ReadKey(); } } class MyQueue : IDisposable { private BlockingCollection<MyMessage> messageQueue; private Thread dequeueThread; bool stopped = true; bool isStopping = false; public MyQueue() { messageQueue = new BlockingCollection<MyMessage>(new ConcurrentQueue<MyMessage>()); dequeueThread = new Thread(new ThreadStart(DequeueMessageThread)); dequeueThread.Name = "TransactionPostThread"; dequeueThread.Start(); stopped = false; } private void DequeueMessageThread() { try { while (true) { MyMessage message = messageQueue.Take(); Console.WriteLine("Dequeueing: " + message.ToString()); if (messageQueue.IsCompleted) { break; } } } catch (InvalidOperationException) { // if invalid op it's because queue was completed } catch (ThreadAbortException) { // Thread aborted due to queue issue, ignore } catch (Exception) { throw; } } public void AddLog(MyMessage message) { Console.WriteLine("Enqueueing: " + message.ToString()); messageQueue.Add(message); } /// <summary> /// 实现IDisposable接口方法 /// </summary> public void Dispose() { Dispose(false); } private void Dispose(bool fromDestructor) { isStopping = true; int logShutdownTimeout = 30000; Console.WriteLine("Shutting down queue. Waiting for dequeue thread completion."); // Signal queue that we're shutting down messageQueue.CompleteAdding(); // Wait for thread to complete before exiting do { if (!dequeueThread.Join(logShutdownTimeout)) { // Queue thread may be stuck. Check for items in queue and kill thread if empty if (messageQueue.Count == 0) { System.Diagnostics.Debug.Print("Aborting thread"); dequeueThread.Abort(); break; } } } while (dequeueThread.IsAlive); Console.WriteLine("Dequeue thread complete."); if (!fromDestructor) { GC.SuppressFinalize(this); } stopped = true; isStopping = false; } ~MyQueue() { Dispose(true); } } class MyMessage { public int MessageId { get; set; } public string Message { get; set; } public override string ToString() { return string.Format("Message with ID {0:#,##0} and value {1}.", MessageId, Message); } } }
相关文章推荐
- 一篇线程消息队列的文章
- 一篇很好的Windows消息队列的文章
- 今天看到一篇介绍环形消息队列的文章,学习了
- 又一篇关于windows消息队列比较好的文章
- 转载一篇关于庄子的文章
- 转载一篇关于 web报表工具设计 的文章
- 消息队列study------转载
- 转载一篇关于ALE的应用极好的文章
- 如何应用.NET中的消息队列服务
- Proxy源代码分析--谈谈如何学习linux网络编程(转载,又一篇很好的文章)
- 使用NBear.MQ分布式服务消息队列模块开发分布式系统
- 一篇非常好的服务器控件原理讲解文章(转载)
- [C#]Windows服务程序[转载西门吹雪的好文章]
- 在北大校园BBS引起轰动的一篇文章 (转载)
- 转载一篇文章
- 转载一篇不错的关于.NET中内存使用的文章
- 消息队列服务
- 转载一篇有关黎鸣先生的文章(比较长,读完者必定受益无穷)
- 转载一篇关于ALE的应用极好的文章
- 转载:一篇java与C#的对比文章(英文)