RabbitMqPublisher.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Core.RabbitMQBus.Log;
  2. using RabbitMQ.Client;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Core.RabbitMQBus.EventBus
  9. {
  10. public class RabbitMqPublisher : IRabbitMqPublisher
  11. {
  12. private readonly ILoggerHelper _loggerHelper;
  13. private readonly IConnectionChannel _connectionChannel;
  14. private readonly IModel _channel;
  15. IBasicProperties properties;
  16. ConcurrentDictionary<ulong, string> confirmedDictionary = new ConcurrentDictionary<ulong, string>();
  17. public RabbitMqPublisher(IConnectionChannel connectionChannel, ILoggerHelper loggerHelper)
  18. {
  19. _loggerHelper = loggerHelper;
  20. _connectionChannel = connectionChannel;
  21. _channel = _connectionChannel.GetChannel();
  22. //声名交换机
  23. _channel.ExchangeDeclare(exchange: _connectionChannel.ExchangeName, type: "topic", durable: true);
  24. //启用发布者确认
  25. _channel.ConfirmSelect();
  26. //肯定确认
  27. _channel.BasicAcks += (s, e) =>
  28. {
  29. //多条
  30. if (e.Multiple)
  31. {
  32. var confirmed = confirmedDictionary.Where(k => k.Key <= e.DeliveryTag);
  33. foreach (var entry in confirmed)
  34. {
  35. confirmedDictionary.TryRemove(entry.Key, out _);
  36. }
  37. }
  38. //单条
  39. else
  40. {
  41. confirmedDictionary.TryRemove(e.DeliveryTag, out _);
  42. }
  43. };
  44. //否定确认
  45. _channel.BasicNacks += (s, e) =>
  46. {
  47. //多条
  48. if (e.Multiple)
  49. {
  50. var confirmed = confirmedDictionary.Where(k => k.Key <= e.DeliveryTag);
  51. foreach (var entry in confirmed)
  52. {
  53. confirmedBasicNackLog(entry.Key);
  54. }
  55. }
  56. //单条
  57. else
  58. {
  59. confirmedBasicNackLog(e.DeliveryTag);
  60. }
  61. };
  62. }
  63. /// <summary>
  64. /// 否定确认日志
  65. /// </summary>
  66. /// <param name="sequenceNumber"></param>
  67. public void confirmedBasicNackLog(ulong sequenceNumber)
  68. {
  69. confirmedDictionary.TryGetValue(sequenceNumber, out string body);
  70. _loggerHelper.LogInfo("RabbitMQ发布者确认异常", body);
  71. confirmedDictionary.TryRemove(sequenceNumber, out _);
  72. }
  73. /// <summary>
  74. /// 发布事件
  75. /// </summary>
  76. /// <returns></returns>
  77. public void Publish(string queueName, object content)
  78. {
  79. try
  80. {
  81. var message = SwifterJsonSerializer.SerializeObject(content);
  82. confirmedDictionary.TryAdd(_channel.NextPublishSeqNo, message);
  83. var body = Encoding.Default.GetBytes(message);
  84. //队列持久化
  85. //var dictionary = new Dictionary<string, object>();
  86. //dictionary.TryAdd(queueName, content);
  87. properties = _channel.CreateBasicProperties();
  88. properties.Persistent = true;
  89. //properties.Headers = dictionary;
  90. //发送消息
  91. _channel.BasicPublish(_connectionChannel.ExchangeName, queueName, properties, body);
  92. }
  93. catch (Exception ex)
  94. {
  95. _loggerHelper.LogInfo("发布消息异常", ex.Message);
  96. throw;
  97. }
  98. }
  99. /// <summary>
  100. /// 异步发布事件
  101. /// </summary>
  102. /// <returns></returns>
  103. public async Task PublishAsync(string queueName, object content)
  104. {
  105. try
  106. {
  107. var message = SwifterJsonSerializer.SerializeObject(content);
  108. confirmedDictionary.TryAdd(_channel.NextPublishSeqNo, message);
  109. var body = Encoding.Default.GetBytes(message);
  110. //队列持久化
  111. properties = _channel.CreateBasicProperties();
  112. properties.Persistent = true;
  113. //发送消息
  114. _channel.BasicPublish(_connectionChannel.ExchangeName, queueName, properties, body);
  115. await Task.CompletedTask;
  116. }
  117. catch (Exception ex)
  118. {
  119. await _loggerHelper.LogInfoAsync("发布消息异常", ex.Message);
  120. throw;
  121. }
  122. }
  123. }
  124. }