You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.7 KiB

  1. using EasyNetQ;
  2. using HKLib.RabbitMQ.Config;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace HKLib.RabbitMQ.Publisher
  8. {
  9. public class ServiceQueryPush
  10. {
  11. public static ServiceQueryPush QueryPush => new Lazy<ServiceQueryPush>().Value;
  12. public async Task<bool> PushMQAsync<T>(T Entity,string Key, MQEnum MQType = MQEnum.Push)
  13. {
  14. //推送模式
  15. //推送模式下需指定管道名称和路由键值名称
  16. //消息只会被发送到指定的队列中去
  17. //订阅模式
  18. //订阅模式下只需指定管道名称
  19. //消息会被发送到该管道下的所有队列中
  20. //主题路由模式
  21. //路由模式下需指定管道名称和路由值
  22. //消息会被发送到该管道下,和路由值匹配的队列中去
  23. PushEntity<T> entity = new PushEntity<T>();
  24. entity.BodyData = Entity;
  25. entity.SendType = MQType;
  26. if (MQType == MQEnum.Push)
  27. {
  28. entity.ExchangeName = "Message.Direct";
  29. entity.RouteName = Key;
  30. }
  31. else if (MQType == MQEnum.Sub)
  32. {
  33. entity.ExchangeName = "Message.Fanout";
  34. }
  35. else
  36. {
  37. entity.ExchangeName = "Message.Topic";
  38. entity.RouteName = Key;
  39. }
  40. return await MQFactory.SendMQAsync(entity).ContinueWith(t => { return t.IsCompleted ? true : false; });
  41. }
  42. public bool PushMQ<T>(T Entity, string Key, MQEnum MQType = MQEnum.Push)
  43. {
  44. //推送模式
  45. //推送模式下需指定管道名称和路由键值名称
  46. //消息只会被发送到指定的队列中去
  47. //订阅模式
  48. //订阅模式下只需指定管道名称
  49. //消息会被发送到该管道下的所有队列中
  50. //主题路由模式
  51. //路由模式下需指定管道名称和路由值
  52. //消息会被发送到该管道下,和路由值匹配的队列中去
  53. PushEntity<T> entity = new PushEntity<T>();
  54. entity.BodyData = Entity;
  55. entity.SendType = MQType;
  56. if (MQType == MQEnum.Push)
  57. {
  58. entity.ExchangeName = "Message.Direct";
  59. entity.RouteName = Key;
  60. }
  61. else if (MQType == MQEnum.Sub)
  62. {
  63. entity.ExchangeName = "Message.Fanout";
  64. }
  65. else
  66. {
  67. entity.ExchangeName = "Message.Topic";
  68. entity.RouteName = Key;
  69. }
  70. return MQFactory.SendMQ(entity);
  71. }
  72. }
  73. }