RabbitMQEventSourceStore.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using RabbitMQ.Client;
  7. using RabbitMQ.Client.Events;
  8. using System.Threading.Channels;
  9. namespace Admin.NET.Core;
  10. /// <summary>
  11. /// RabbitMQ自定义事件源存储器
  12. /// </summary>
  13. public class RabbitMQEventSourceStore : IEventSourceStorer
  14. {
  15. /// <summary>
  16. /// 内存通道事件源存储器
  17. /// </summary>
  18. private readonly Channel<IEventSource> _channel;
  19. /// <summary>
  20. /// 通道对象
  21. /// </summary>
  22. private readonly IModel _model;
  23. /// <summary>
  24. /// 连接对象
  25. /// </summary>
  26. private readonly IConnection _connection;
  27. /// <summary>
  28. /// 路由键
  29. /// </summary>
  30. private readonly string _routeKey;
  31. /// <summary>
  32. /// 构造函数
  33. /// </summary>
  34. /// <param name="factory">连接工厂</param>
  35. /// <param name="routeKey">路由键</param>
  36. /// <param name="capacity">存储器最多能够处理多少消息,超过该容量进入等待写入</param>
  37. public RabbitMQEventSourceStore(ConnectionFactory factory, string routeKey, int capacity)
  38. {
  39. // 配置通道,设置超出默认容量后进入等待
  40. var boundedChannelOptions = new BoundedChannelOptions(capacity)
  41. {
  42. FullMode = BoundedChannelFullMode.Wait
  43. };
  44. // 创建有限容量通道
  45. _channel = Channel.CreateBounded<IEventSource>(boundedChannelOptions);
  46. // 创建连接
  47. _connection = factory.CreateConnection();
  48. _routeKey = routeKey;
  49. // 创建通道
  50. _model = _connection.CreateModel();
  51. // 声明路由队列
  52. _model.QueueDeclare(routeKey, false, false, false, null);
  53. // 创建消息订阅者
  54. var consumer = new EventingBasicConsumer(_model);
  55. // 订阅消息并写入内存 Channel
  56. consumer.Received += (ch, ea) =>
  57. {
  58. // 读取原始消息
  59. var stringEventSource = Encoding.UTF8.GetString(ea.Body.ToArray());
  60. // 转换为 IEventSource,如果自定义了 EventSource,注意属性是可读可写
  61. var eventSource = JSON.Deserialize<ChannelEventSource>(stringEventSource);
  62. // 写入内存管道存储器
  63. _channel.Writer.WriteAsync(eventSource);
  64. // 确认该消息已被消费
  65. _model.BasicAck(ea.DeliveryTag, false);
  66. };
  67. // 启动消费者且设置为手动应答消息
  68. _model.BasicConsume(routeKey, false, consumer);
  69. }
  70. /// <summary>
  71. /// 将事件源写入存储器
  72. /// </summary>
  73. /// <param name="eventSource">事件源对象</param>
  74. /// <param name="cancellationToken">取消任务 Token</param>
  75. /// <returns><see cref="ValueTask"/></returns>
  76. public async ValueTask WriteAsync(IEventSource eventSource, CancellationToken cancellationToken)
  77. {
  78. if (eventSource == default)
  79. throw new ArgumentNullException(nameof(eventSource));
  80. // 判断是否是 ChannelEventSource 或自定义的 EventSource
  81. if (eventSource is ChannelEventSource source)
  82. {
  83. // 序列化及发布
  84. var data = Encoding.UTF8.GetBytes(JSON.Serialize(source));
  85. _model.BasicPublish("", _routeKey, null, data);
  86. }
  87. else
  88. {
  89. // 处理动态订阅
  90. await _channel.Writer.WriteAsync(eventSource, cancellationToken);
  91. }
  92. }
  93. /// <summary>
  94. /// 从存储器中读取一条事件源
  95. /// </summary>
  96. /// <param name="cancellationToken">取消任务 Token</param>
  97. /// <returns>事件源对象</returns>
  98. public async ValueTask<IEventSource> ReadAsync(CancellationToken cancellationToken)
  99. {
  100. var eventSource = await _channel.Reader.ReadAsync(cancellationToken);
  101. return eventSource;
  102. }
  103. /// <summary>
  104. /// 释放非托管资源
  105. /// </summary>
  106. public void Dispose()
  107. {
  108. _model.Dispose();
  109. _connection.Dispose();
  110. }
  111. }