.NetCore利用BlockingCollection實(shí)現(xiàn)簡易消息隊(duì)列
消息隊(duì)列現(xiàn)今的應(yīng)用場景越來越大,常用的有RabbmitMQ和KafKa。
我們用BlockingCollection來實(shí)現(xiàn)簡單的消息隊(duì)列。
BlockingCollection實(shí)現(xiàn)了生產(chǎn)者/消費(fèi)者模式,是對IProducerConsumerCollection<T>接口的實(shí)現(xiàn)。與其他Concurrent集合一樣,每次Add或Take元素,都會(huì)導(dǎo)致對集合的lock。只有當(dāng)確定需要在內(nèi)存中創(chuàng)建一個(gè)生產(chǎn)者,消費(fèi)者模式時(shí),再考慮這個(gè)類。
MSDN中的示例用法:
using (BlockingCollection<int> bc = new BlockingCollection<int>()) { Task.Factory.StartNew(() => { for (int i = 0; i < 1000; i++) { bc.Add(i); Thread.Sleep(50); } // Need to do this to keep foreach below from hanging bc.CompleteAdding(); }); // Now consume the blocking collection with foreach. // Use bc.GetConsumingEnumerable() instead of just bc because the // former will block waiting for completion and the latter will // simply take a snapshot of the current state of the underlying collection. foreach (var item in bc.GetConsumingEnumerable()) { Console.WriteLine(item); } }
實(shí)現(xiàn)消息隊(duì)列
用Vs2017創(chuàng)建一個(gè)控制臺(tái)應(yīng)用程序。創(chuàng)建DemoQueueBlock類,封裝一些常用判斷。
- HasEle,判斷是否有元素
- Add向隊(duì)列中添加元素
- Take從隊(duì)列中取出元素
為了不把BlockingCollection直接暴漏給使用者,我們封裝一個(gè)DemoQueueBlock類
/// <summary> /// BlockingCollection演示消息隊(duì)列 /// </summary> /// <typeparam name="T"></typeparam> public class DemoQueueBlock<T> where T : class { private static BlockingCollection<T> Colls; public DemoQueueBlock() { } public static bool IsComleted() { if (Colls != null && Colls.IsCompleted) { return true; } return false; } public static bool HasEle() { if (Colls != null && Colls.Count>0) { return true; } return false; } public static bool Add(T msg) { if (Colls == null) { Colls = new BlockingCollection<T>(); } Colls.Add(msg); return true; } public static T Take() { if (Colls == null) { Colls = new BlockingCollection<T>(); } return Colls.Take(); } } /// <summary> /// 消息體 /// </summary> public class DemoMessage { public string BusinessType { get; set; } public string BusinessId { get; set; } public string Body { get; set; } }
添加元素進(jìn)隊(duì)列
通過控制臺(tái),添加元素
//添加元素 while (true) { Console.WriteLine("請輸入隊(duì)列"); var read = Console.ReadLine(); if (read == "exit") { return; } DemoQueueBlock<DemoMessage>.Add(new DemoMessage() { BusinessId = read }); }
消費(fèi)隊(duì)列
通過判斷IsComleted,來確定是否獲取隊(duì)列
Task.Factory.StartNew(() => { //從隊(duì)列中取元素。 while (!DemoQueueBlock<DemoMessage>.IsComleted()) { try { var m = DemoQueueBlock<DemoMessage>.Take(); Console.WriteLine("已消費(fèi):" + m.BusinessId); } catch (Exception ex) { Console.WriteLine(ex.Message); } } });
查看運(yùn)行結(jié)果
運(yùn)行結(jié)果
這樣我們就實(shí)現(xiàn)了簡易的消息隊(duì)列。
示例源碼:簡易隊(duì)列
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET Core讀取Request.Body的正確方法
相信大家在使用ASP.NET Core進(jìn)行開發(fā)的時(shí)候,肯定會(huì)涉及到讀取Request.Body的場景,畢竟我們大部分的POST請求都是將數(shù)據(jù)存放到Http的Body當(dāng)中,本文就介紹一下ASP.NET Core讀取Request.Body,感興趣的可以了解一下2021-05-05aspx文件格式使用URLRewriter實(shí)現(xiàn)靜態(tài)化變成html
如何隱藏aspx文件格式,變成html,使用asp.net 開發(fā)的網(wǎng)頁程序,使用URLRewriter.dll 實(shí)現(xiàn)靜態(tài)化,接下來將介紹下具體操作步驟,感興趣的朋友可以參考下2013-04-04詳解ASP.NET Core和ASP.NET Framework共享身份驗(yàn)證
本篇文章主要介紹了詳解ASP.NET Core和ASP.NET Framework共享身份驗(yàn)證 ,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12asp.net button 綁定多個(gè)參數(shù)
asp.net button 綁定多個(gè)參數(shù)的代碼2008-11-11ASP.NET?MVC5網(wǎng)站開發(fā)之網(wǎng)站設(shè)置(九)
這篇文章主要為大家詳細(xì)介紹了ASP.NET?MVC5網(wǎng)站開發(fā)之網(wǎng)站設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Asp.net 后臺(tái)添加CSS、JS、Meta標(biāo)簽的方法
是從Asp.net 后臺(tái)添加CSS、JS、Meta標(biāo)簽的寫法,我們這里寫成函數(shù)方便以后使用,需要的朋友可以參考下2013-12-12在ASP.net中保存/取出圖片入/從SQL數(shù)據(jù)庫
在ASP.net中保存/取出圖片入/從SQL數(shù)據(jù)庫...2006-09-09