RabbitMQClient.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using RabbitMQ.Client;
  5. using RabbitMQ.Client.Events;
  6. using System.Threading;
  7. using Infrastructure;
  8. using Infrastructure.Model;
  9. namespace Common
  10. {
  11. public class RabbitMQClient
  12. {
  13. public readonly static RabbitMQClient Instance = new RabbitMQClient();
  14. string UserName,Password,HostName,VirtualHostName;
  15. private RabbitMQClient()
  16. {
  17. var options = App.OptionsSetting;
  18. RabbitMqConfigs RabbitMqConfigs = options.RabbitMqConfigs;
  19. UserName = RabbitMqConfigs.UserName;
  20. Password = RabbitMqConfigs.Password;
  21. HostName = RabbitMqConfigs.HostName;
  22. VirtualHostName = RabbitMqConfigs.VirtualHostName;
  23. }
  24. public static IConnection _connection;
  25. public void CreateConn()
  26. {
  27. var factory = new ConnectionFactory()
  28. {
  29. HostName = HostName,
  30. UserName = UserName,
  31. Password = Password,
  32. VirtualHost = VirtualHostName
  33. };
  34. _connection = factory.CreateConnection();
  35. }
  36. #region 单对单接收
  37. public void StartReceive(string QueueName, Action<string> CallBack)
  38. {
  39. if (_connection == null)
  40. {
  41. CreateConn();
  42. }
  43. else if (!_connection.IsOpen)
  44. {
  45. CreateConn();
  46. }
  47. var consumer = new EventingBasicConsumer(_channel[QueueName]);
  48. consumer.Received += (model, ea) =>
  49. {
  50. var body = ea.Body.ToArray();
  51. //获取接收的数据
  52. var message = Encoding.UTF8.GetString(body);
  53. // 模拟消息处理逻辑
  54. try
  55. {
  56. CallBack(message);
  57. // 手动确认消息
  58. _channel[QueueName].BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
  59. }
  60. catch (Exception ex)
  61. {
  62. // 如果处理失败,可以选择拒绝消息或重新入队
  63. _channel[QueueName].BasicNack(deliveryTag: ea.DeliveryTag, multiple: false, requeue: true);
  64. Utils.WriteLog(ex.ToString(), "MQ异常");
  65. }
  66. };
  67. // 设置 autoAck = false
  68. _channel[QueueName].BasicConsume(queue: QueueName, autoAck: false, consumer: consumer);
  69. }
  70. #endregion
  71. #region 单对单发送
  72. public Dictionary<string, IModel> _channel = new Dictionary<string, IModel>();
  73. public void Conn(string QueueName)
  74. {
  75. if (_connection == null)
  76. {
  77. CreateConn();
  78. }
  79. else if (!_connection.IsOpen)
  80. {
  81. CreateConn();
  82. }
  83. var channel = _connection.CreateModel();
  84. channel.ExchangeDeclare("kxs_direct_ranch", "direct", true);
  85. channel.QueueDeclare(QueueName, true, false, false);
  86. channel.QueueBind(QueueName, "kxs_direct_ranch", QueueName);
  87. if(!_channel.ContainsKey(QueueName)) _channel.Add(QueueName, channel);
  88. }
  89. public void Push(string QueueName, string Content)
  90. {
  91. _channel[QueueName].BasicPublish("", QueueName, null, Encoding.Default.GetBytes(Content));
  92. }
  93. #endregion
  94. }
  95. }