RabbitMQClient.cs 3.8 KB

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