RabbitMQClient.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using RabbitMQ.Client;
  5. using RabbitMQ.Client.Events;
  6. using Library;
  7. namespace MySystem
  8. {
  9. public class RabbitMQClient
  10. {
  11. public readonly static RabbitMQClient Instance = new RabbitMQClient();
  12. string UserName,Password,HostName;
  13. private RabbitMQClient()
  14. {
  15. UserName = ConfigurationManager.AppSettings["MqUserName"].ToString();
  16. Password = ConfigurationManager.AppSettings["MqPassword"].ToString();
  17. HostName = ConfigurationManager.AppSettings["MqHostName"].ToString();
  18. }
  19. #region 单对单发送
  20. public void SendMsg(string content, string QueueName = "")
  21. {
  22. //创建连接对象工厂
  23. var factory = new ConnectionFactory()
  24. {
  25. UserName = UserName,
  26. Password = Password,
  27. AutomaticRecoveryEnabled = true, //如果connection挂掉是否重新连接
  28. TopologyRecoveryEnabled = true //连接恢复后,连接的交换机,队列等是否一同恢复
  29. };
  30. List<AmqpTcpEndpoint> p = new List<AmqpTcpEndpoint>();
  31. string[] HostNames = HostName.Split(',');
  32. foreach (string subHostName in HostNames)
  33. {
  34. string[] subHostNameData = subHostName.Split(':');
  35. p.Add(new AmqpTcpEndpoint(subHostNameData[0], int.Parse(subHostNameData[1])));
  36. }
  37. var conn = factory.CreateConnection(p);
  38. var channel = conn.CreateModel();
  39. channel.QueueDeclare(QueueName, true, false, false);
  40. channel.BasicPublish("", QueueName, null, Encoding.Default.GetBytes(content));
  41. channel.Dispose();
  42. conn.Dispose();
  43. }
  44. #endregion
  45. #region 单对单接收
  46. public void StartReceive(string QueueName)
  47. {
  48. var factory = new ConnectionFactory()
  49. {
  50. UserName = UserName,
  51. Password = Password,
  52. AutomaticRecoveryEnabled = true, //如果connection挂掉是否重新连接
  53. TopologyRecoveryEnabled = true //连接恢复后,连接的交换机,队列等是否一同恢复
  54. };
  55. List<AmqpTcpEndpoint> p = new List<AmqpTcpEndpoint>();
  56. string[] HostNames = HostName.Split(',');
  57. foreach (string subHostName in HostNames)
  58. {
  59. string[] subHostNameData = subHostName.Split(':');
  60. p.Add(new AmqpTcpEndpoint(subHostNameData[0], int.Parse(subHostNameData[1])));
  61. }
  62. var conn = factory.CreateConnection(p);
  63. var channel = conn.CreateModel();
  64. channel.QueueDeclare(QueueName, true, false, false);
  65. EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
  66. consumer.Received += (a, e) =>
  67. {
  68. string MsgContent = Encoding.Default.GetString(e.Body.ToArray());
  69. if (QueueName == "PublicMainServer")
  70. {
  71. JobMqMsg job = Newtonsoft.Json.JsonConvert.DeserializeObject<JobMqMsg>(MsgContent);
  72. ReceiveTaskService.Instance.Start(job);
  73. }
  74. else if (QueueName == "CheckWeChatSign")
  75. {
  76. JobMqMsg job = Newtonsoft.Json.JsonConvert.DeserializeObject<JobMqMsg>(MsgContent);
  77. CheckWeChatSignService.Instance.Start(job);
  78. }
  79. else if (QueueName == "MerchantConfirmList")
  80. {
  81. MerchantConfirmService.Instance.Start(MsgContent);
  82. }
  83. else if (QueueName == "SycnTableData")
  84. {
  85. JobMqMsg job = Newtonsoft.Json.JsonConvert.DeserializeObject<JobMqMsg>(MsgContent);
  86. if (job.BrandInfo.DataType == 1)
  87. {
  88. //同步激活
  89. SycnActiveRewardService.Instance.Start(job);
  90. }
  91. else if (job.BrandInfo.DataType == 2)
  92. {
  93. //同步交易
  94. SycnTradeRecordService.Instance.Start(job);
  95. }
  96. else if (job.BrandInfo.DataType == 8)
  97. {
  98. //同步商户
  99. SycnMerchantInfoService.Instance.Start(job);
  100. }
  101. }
  102. else if (QueueName == "ProfitForEverMonth")
  103. {
  104. JobMqMsg job = Newtonsoft.Json.JsonConvert.DeserializeObject<JobMqMsg>(MsgContent);
  105. ProfitService.Instance.Start(job);
  106. }
  107. else if (QueueName == "FluxPrize")
  108. {
  109. JobMqMsg job = Newtonsoft.Json.JsonConvert.DeserializeObject<JobMqMsg>(MsgContent);
  110. FluxService.Instance.Start(job);
  111. }
  112. channel.BasicAck(e.DeliveryTag, true); //收到回复后,RabbitMQ会直接在队列中删除这条消息
  113. };
  114. channel.BasicConsume(QueueName, false, consumer);
  115. }
  116. #endregion
  117. #region 单对多发送
  118. public void SendMsgToExchange(string content, string Exchange = "")
  119. {
  120. //创建连接对象工厂
  121. var factory = new ConnectionFactory()
  122. {
  123. UserName = UserName,
  124. Password = Password,
  125. AutomaticRecoveryEnabled = true, //如果connection挂掉是否重新连接
  126. TopologyRecoveryEnabled = true //连接恢复后,连接的交换机,队列等是否一同恢复
  127. };
  128. List<AmqpTcpEndpoint> p = new List<AmqpTcpEndpoint>();
  129. string[] HostNames = HostName.Split(',');
  130. foreach (string subHostName in HostNames)
  131. {
  132. string[] subHostNameData = subHostName.Split(':');
  133. p.Add(new AmqpTcpEndpoint(subHostNameData[0], int.Parse(subHostNameData[1])));
  134. }
  135. var conn = factory.CreateConnection(p);
  136. var channel = conn.CreateModel();
  137. channel.ExchangeDeclare(Exchange, ExchangeType.Fanout, true, false);
  138. channel.BasicPublish(Exchange, "", null, Encoding.Default.GetBytes(content));
  139. channel.Dispose();
  140. conn.Dispose();
  141. }
  142. #endregion
  143. #region 单对多接收
  144. public void StartReceiveFromExchange(string QueueName = "", string Exchange = "")
  145. {
  146. var factory = new ConnectionFactory()
  147. {
  148. UserName = UserName,
  149. Password = Password,
  150. AutomaticRecoveryEnabled = true, //如果connection挂掉是否重新连接
  151. TopologyRecoveryEnabled = true //连接恢复后,连接的交换机,队列等是否一同恢复
  152. };
  153. List<AmqpTcpEndpoint> p = new List<AmqpTcpEndpoint>();
  154. string[] HostNames = HostName.Split(',');
  155. foreach (string subHostName in HostNames)
  156. {
  157. string[] subHostNameData = subHostName.Split(':');
  158. p.Add(new AmqpTcpEndpoint(subHostNameData[0], int.Parse(subHostNameData[1])));
  159. }
  160. var conn = factory.CreateConnection(p);
  161. var channel = conn.CreateModel();
  162. //定义队列
  163. channel.QueueDeclare(QueueName, true, false, false);
  164. //定义交换机
  165. channel.ExchangeDeclare(Exchange, ExchangeType.Fanout, true, false);
  166. //绑定队列到交换机
  167. channel.QueueBind(QueueName, Exchange, "");
  168. var consumer = new EventingBasicConsumer(channel);
  169. consumer.Received += (a, e) =>
  170. {
  171. Library.function.WriteLog(Encoding.Default.GetString(e.Body.ToArray()), "接收到的MQ消息");
  172. channel.BasicAck(e.DeliveryTag, true); //收到回复后,RabbitMQ会直接在队列中删除这条消息
  173. };
  174. channel.BasicConsume(QueueName, false, consumer);
  175. }
  176. #endregion
  177. }
  178. }