RabbitMQClient.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Library;
  8. namespace MySystem
  9. {
  10. public class RabbitMQClient
  11. {
  12. public readonly static RabbitMQClient Instance = new RabbitMQClient();
  13. string UserName,Password,HostName,OneHostName,OneOnePort;
  14. private RabbitMQClient()
  15. {
  16. UserName = ConfigurationManager.AppSettings["MqUserName"].ToString();
  17. Password = ConfigurationManager.AppSettings["MqPassword"].ToString();
  18. HostName = ConfigurationManager.AppSettings["MqHostName"].ToString();
  19. // OneHostName = ConfigurationManager.AppSettings["MqOneHostName"].ToString();
  20. // OneOnePort = ConfigurationManager.AppSettings["MqOnePort"].ToString();
  21. }
  22. public static IConnection _connection;
  23. public void CreateConn()
  24. {
  25. var factory = new ConnectionFactory()
  26. {
  27. UserName = UserName,
  28. Password = Password,
  29. AutomaticRecoveryEnabled = true, //如果connection挂掉是否重新连接
  30. TopologyRecoveryEnabled = true, //连接恢复后,连接的交换机,队列等是否一同恢复
  31. RequestedHeartbeat = TimeSpan.FromMinutes(1),
  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 SendMsg(string content, string QueueName = "")
  44. {
  45. return;
  46. if (_connection == null)
  47. {
  48. CreateConn();
  49. }
  50. else if (!_connection.IsOpen)
  51. {
  52. CreateConn();
  53. }
  54. var channel = _connection.CreateModel();
  55. channel.QueueDeclare(QueueName, true, false, false);
  56. channel.BasicPublish("", QueueName, null, Encoding.Default.GetBytes(content));
  57. channel.Dispose();
  58. _connection.Dispose();
  59. }
  60. #endregion
  61. #region 单对单接收
  62. public void StartReceive(string QueueName)
  63. {
  64. if (_connection == null)
  65. {
  66. CreateConn();
  67. }
  68. else if (!_connection.IsOpen)
  69. {
  70. CreateConn();
  71. }
  72. var channel = _connection.CreateModel();
  73. channel.QueueDeclare(QueueName, true, false, false);
  74. EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
  75. consumer.Received += (a, e) =>
  76. {
  77. string MsgContent = Encoding.Default.GetString(e.Body.ToArray());
  78. if (QueueName == "SetRedisDataList")
  79. {
  80. // ReceiveSycnTmpTableService.Instance.Start(MsgContent);
  81. }
  82. channel.BasicAck(e.DeliveryTag, true); //收到回复后,RabbitMQ会直接在队列中删除这条消息
  83. };
  84. channel.BasicConsume(QueueName, false, consumer);
  85. }
  86. #endregion
  87. #region 单对多发送
  88. public void SendMsgToExchange(string content, string Exchange = "")
  89. {
  90. //创建连接对象工厂
  91. var factory = new ConnectionFactory()
  92. {
  93. UserName = UserName,
  94. Password = Password,
  95. AutomaticRecoveryEnabled = true, //如果connection挂掉是否重新连接
  96. TopologyRecoveryEnabled = true //连接恢复后,连接的交换机,队列等是否一同恢复
  97. };
  98. List<AmqpTcpEndpoint> p = new List<AmqpTcpEndpoint>();
  99. string[] HostNames = HostName.Split(',');
  100. foreach (string subHostName in HostNames)
  101. {
  102. string[] subHostNameData = subHostName.Split(':');
  103. p.Add(new AmqpTcpEndpoint(subHostNameData[0], int.Parse(subHostNameData[1])));
  104. }
  105. var conn = factory.CreateConnection(p);
  106. var channel = conn.CreateModel();
  107. channel.ExchangeDeclare(Exchange, ExchangeType.Fanout, true, false);
  108. channel.BasicPublish(Exchange, "", null, Encoding.Default.GetBytes(content));
  109. channel.Dispose();
  110. conn.Dispose();
  111. }
  112. #endregion
  113. #region 单对多接收
  114. public void StartReceiveFromExchange(string QueueName = "", string Exchange = "")
  115. {
  116. var factory = new ConnectionFactory()
  117. {
  118. UserName = UserName,
  119. Password = Password,
  120. AutomaticRecoveryEnabled = true, //如果connection挂掉是否重新连接
  121. TopologyRecoveryEnabled = true //连接恢复后,连接的交换机,队列等是否一同恢复
  122. };
  123. List<AmqpTcpEndpoint> p = new List<AmqpTcpEndpoint>();
  124. string[] HostNames = HostName.Split(',');
  125. foreach (string subHostName in HostNames)
  126. {
  127. string[] subHostNameData = subHostName.Split(':');
  128. p.Add(new AmqpTcpEndpoint(subHostNameData[0], int.Parse(subHostNameData[1])));
  129. }
  130. var conn = factory.CreateConnection(p);
  131. var channel = conn.CreateModel();
  132. //定义队列
  133. channel.QueueDeclare(QueueName, true, false, false);
  134. //定义交换机
  135. channel.ExchangeDeclare(Exchange, ExchangeType.Fanout, true, false);
  136. //绑定队列到交换机
  137. channel.QueueBind(QueueName, Exchange, "");
  138. var consumer = new EventingBasicConsumer(channel);
  139. consumer.Received += (a, e) =>
  140. {
  141. Library.function.WriteLog(Encoding.Default.GetString(e.Body.ToArray()), "接收到的MQ消息");
  142. channel.BasicAck(e.DeliveryTag, true); //收到回复后,RabbitMQ会直接在队列中删除这条消息
  143. };
  144. channel.BasicConsume(QueueName, false, consumer);
  145. }
  146. #endregion
  147. }
  148. }