.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è)控制臺應(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ì)列
通過控制臺,添加元素
//添加元素
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 MVC中為DropDownListFor設(shè)置選中項(xiàng)的方法
這篇文章主要介紹了ASP.NET MVC中為DropDownListFor設(shè)置選中項(xiàng)的方法,需要的朋友可以參考下2014-10-10
asp.net實(shí)現(xiàn)生成縮略圖及給原始圖加水印的方法示例
這篇文章主要介紹了asp.net實(shí)現(xiàn)生成縮略圖及給原始圖加水印的方法,結(jié)合具體實(shí)例形式分析了asp.net圖片的縮略圖與水印操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
asp.net中JavaScript數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)代碼
我對JavaScript一直不了解。常常為了一點(diǎn)點(diǎn)的數(shù)據(jù)驗(yàn)證和無刷新就去動(dòng)用AJAX,實(shí)在不爽——有點(diǎn)殺雞用牛刀的感覺。2010-05-05
c# Random快速連續(xù)產(chǎn)生相同隨機(jī)數(shù)的解決方案
在寫數(shù)獨(dú)基類的時(shí)候?yàn)榱水a(chǎn)生隨機(jī)數(shù)的時(shí)候遇到奇怪的問題2009-03-03
FileUpload1 上傳文件類型驗(yàn)證正則表達(dá)式
FileUpload1 上傳文件類型驗(yàn)證正則表達(dá)式...2006-10-10
為ASP.NET Core強(qiáng)類型配置對象添加驗(yàn)證的方法
這篇文章主要給大家介紹了關(guān)于如何為ASP.NET Core強(qiáng)類型配置對象添加驗(yàn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12

