RabbitMQEventSourceStore.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. using RabbitMQ.Client;
  5. using RabbitMQ.Client.Events;
  6. using System.Threading.Channels;
  7. namespace Admin.NET.Core;
  8. /// <summary>
  9. /// RabbitMQ自定义事件源存储器
  10. /// </summary>
  11. public class RabbitMQEventSourceStore : IEventSourceStorer
  12. {
  13. /// <summary>
  14. /// 内存通道事件源存储器
  15. /// </summary>
  16. private readonly Channel<IEventSource> _channel;
  17. /// <summary>
  18. /// 通道对象
  19. /// </summary>
  20. private readonly IModel _model;
  21. /// <summary>
  22. /// 连接对象
  23. /// </summary>
  24. private readonly IConnection _connection;
  25. /// <summary>
  26. /// 路由键
  27. /// </summary>
  28. private readonly string _routeKey;
  29. /// <summary>
  30. /// 构造函数
  31. /// </summary>
  32. /// <param name="factory">连接工厂</param>
  33. /// <param name="routeKey">路由键</param>
  34. /// <param name="capacity">存储器最多能够处理多少消息,超过该容量进入等待写入</param>
  35. public RabbitMQEventSourceStore(ConnectionFactory factory, string routeKey, int capacity)
  36. {
  37. // 配置通道,设置超出默认容量后进入等待
  38. var boundedChannelOptions = new BoundedChannelOptions(capacity)
  39. {
  40. FullMode = BoundedChannelFullMode.Wait
  41. };
  42. // 创建有限容量通道
  43. _channel = Channel.CreateBounded<IEventSource>(boundedChannelOptions);
  44. // 创建连接
  45. _connection = factory.CreateConnection();
  46. _routeKey = routeKey;
  47. // 创建通道
  48. _model = _connection.CreateModel();
  49. // 声明路由队列
  50. _model.QueueDeclare(routeKey, false, false, false, null);
  51. // 创建消息订阅者
  52. var consumer = new EventingBasicConsumer(_model);
  53. // 订阅消息并写入内存 Channel
  54. consumer.Received += (ch, ea) =>
  55. {
  56. // 读取原始消息
  57. var stringEventSource = Encoding.UTF8.GetString(ea.Body.ToArray());
  58. // 转换为 IEventSource,如果自定义了 EventSource,注意属性是可读可写
  59. var eventSource = JSON.Deserialize<ChannelEventSource>(stringEventSource);
  60. // 写入内存管道存储器
  61. _channel.Writer.WriteAsync(eventSource);
  62. // 确认该消息已被消费
  63. _model.BasicAck(ea.DeliveryTag, false);
  64. };
  65. // 启动消费者且设置为手动应答消息
  66. _model.BasicConsume(routeKey, false, consumer);
  67. }
  68. /// <summary>
  69. /// 将事件源写入存储器
  70. /// </summary>
  71. /// <param name="eventSource">事件源对象</param>
  72. /// <param name="cancellationToken">取消任务 Token</param>
  73. /// <returns><see cref="ValueTask"/></returns>
  74. public async ValueTask WriteAsync(IEventSource eventSource, CancellationToken cancellationToken)
  75. {
  76. if (eventSource == default)
  77. throw new ArgumentNullException(nameof(eventSource));
  78. // 判断是否是 ChannelEventSource 或自定义的 EventSource
  79. if (eventSource is ChannelEventSource source)
  80. {
  81. // 序列化及发布
  82. var data = Encoding.UTF8.GetBytes(JSON.Serialize(source));
  83. _model.BasicPublish("", _routeKey, null, data);
  84. }
  85. else
  86. {
  87. // 处理动态订阅
  88. await _channel.Writer.WriteAsync(eventSource, cancellationToken);
  89. }
  90. }
  91. /// <summary>
  92. /// 从存储器中读取一条事件源
  93. /// </summary>
  94. /// <param name="cancellationToken">取消任务 Token</param>
  95. /// <returns>事件源对象</returns>
  96. public async ValueTask<IEventSource> ReadAsync(CancellationToken cancellationToken)
  97. {
  98. var eventSource = await _channel.Reader.ReadAsync(cancellationToken);
  99. return eventSource;
  100. }
  101. /// <summary>
  102. /// 释放非托管资源
  103. /// </summary>
  104. public void Dispose()
  105. {
  106. _model.Dispose();
  107. _connection.Dispose();
  108. }
  109. }