EventConsumer.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. namespace Admin.NET.Core;
  10. /// <summary>
  11. /// Redis 消息扩展
  12. /// </summary>
  13. /// <typeparam name="T"></typeparam>
  14. public class EventConsumer<T> : IDisposable
  15. {
  16. private Task _consumerTask;
  17. private CancellationTokenSource _consumerCts;
  18. /// <summary>
  19. /// 消费者
  20. /// </summary>
  21. public IProducerConsumer<T> Consumer { get; }
  22. /// <summary>
  23. /// ConsumerBuilder
  24. /// </summary>
  25. public FullRedis Builder { get; set; }
  26. /// <summary>
  27. /// 消息回调
  28. /// </summary>
  29. public event EventHandler<T> Received;
  30. /// <summary>
  31. /// 构造函数
  32. /// </summary>
  33. public EventConsumer(FullRedis redis, string routeKey)
  34. {
  35. Builder = redis;
  36. Consumer = Builder.GetQueue<T>(routeKey);
  37. }
  38. /// <summary>
  39. /// 启动
  40. /// </summary>
  41. /// <exception cref="InvalidOperationException"></exception>
  42. public void Start()
  43. {
  44. if (Consumer is null)
  45. {
  46. throw new InvalidOperationException("Subscribe first using the Consumer.Subscribe() function");
  47. }
  48. if (_consumerTask != null)
  49. {
  50. return;
  51. }
  52. _consumerCts = new CancellationTokenSource();
  53. var ct = _consumerCts.Token;
  54. _consumerTask = Task.Factory.StartNew(() =>
  55. {
  56. while (!ct.IsCancellationRequested)
  57. {
  58. var cr = Consumer.TakeOne(10);
  59. if (cr == null) continue;
  60. Received?.Invoke(this, cr);
  61. }
  62. }, ct, TaskCreationOptions.LongRunning, TaskScheduler.Default);
  63. }
  64. /// <summary>
  65. /// 停止
  66. /// </summary>
  67. /// <returns></returns>
  68. public async Task Stop()
  69. {
  70. if (_consumerCts == null || _consumerTask == null) return;
  71. _consumerCts.Cancel();
  72. try
  73. {
  74. await _consumerTask;
  75. }
  76. finally
  77. {
  78. _consumerTask = null;
  79. _consumerCts = null;
  80. }
  81. }
  82. /// <summary>
  83. /// 释放
  84. /// </summary>
  85. public void Dispose()
  86. {
  87. Dispose(true);
  88. GC.SuppressFinalize(this);
  89. }
  90. /// <summary>
  91. /// 释放
  92. /// </summary>
  93. /// <param name="disposing"></param>
  94. protected virtual void Dispose(bool disposing)
  95. {
  96. if (disposing)
  97. {
  98. if (_consumerTask != null)
  99. {
  100. Stop().Wait();
  101. }
  102. Builder.Dispose();
  103. }
  104. }
  105. }