欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.NetCore利用BlockingCollection實(shí)現(xiàn)簡易消息隊(duì)列

 更新時(shí)間:2018年09月04日 14:24:47   作者:范存威  
這篇文章主要介紹了.NetCore利用BlockingCollection實(shí)現(xiàn)簡易消息隊(duì)列,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

消息隊(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)文章

最新評論