選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

82 行
2.8 KiB

  1. using EasyNetQ;
  2. using EasyNetQ.Topology;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using Newtonsoft.Json;
  7. using System.Threading.Tasks;
  8. namespace HKLib.RabbitMQ.Config
  9. {
  10. public class PushManage
  11. {
  12. /// <summary>
  13. /// 发布消息队列异步
  14. /// </summary>
  15. /// <typeparam name="T"></typeparam>
  16. /// <param name="Param"></param>
  17. /// <param name="Bus"></param>
  18. /// <returns></returns>
  19. public async Task SendMQAsync<T>(PushEntity<T> Param, IBus Bus)
  20. {
  21. //one to one
  22. var msg = new Message<T>(Param.BodyData);
  23. IExchange EX = null;
  24. if (Param.SendType == MQEnum.Sub)
  25. {
  26. EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Fanout);
  27. }
  28. if (Param.SendType == MQEnum.Push)
  29. {
  30. EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Direct);
  31. }
  32. if (Param.SendType == MQEnum.Top)
  33. {
  34. EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Topic);
  35. }
  36. await Bus.Advanced.PublishAsync(EX, Param.RouteName, false, msg).ContinueWith(t =>
  37. {
  38. //消息投递失败
  39. if (!t.IsCompleted && t.IsFaulted)
  40. {
  41. // //将消息记录到数据库轮询
  42. // IRepository Repository = new Repository();
  43. // SystemLog Log = new SystemLog
  44. // {
  45. // LogName = "消息队列",
  46. // Source = "发布队列",
  47. // EventData = JsonConvert.SerializeObject(Param.BodyData)
  48. // };
  49. // Repository.Insert(Log);
  50. }
  51. });
  52. }
  53. /// <summary>
  54. /// 发布消息队列同步
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="Param"></param>
  58. /// <param name="Bus"></param>
  59. public void SendMQ<T>(PushEntity<T> Param, IBus Bus)
  60. {
  61. //one to one
  62. var msg = new Message<T>(Param.BodyData);
  63. IExchange EX = null;
  64. if (Param.SendType == MQEnum.Sub)
  65. {
  66. EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Fanout);
  67. }
  68. if (Param.SendType == MQEnum.Push)
  69. {
  70. EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Direct);
  71. }
  72. if (Param.SendType == MQEnum.Top)
  73. {
  74. EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Topic);
  75. }
  76. Bus.Advanced.Publish(EX, Param.RouteName, false, msg);
  77. }
  78. }
  79. }