RegisterSubscriberCache.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using Core.RabbitMQBus.Common;
  8. using Core.RabbitMQBus.IocHelper;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.Extensions.DependencyInjection;
  11. namespace Core.RabbitMQBus.EventBus
  12. {
  13. /// <summary>
  14. /// 订阅者注册缓存
  15. /// </summary>
  16. public static class RegisterSubscriberCache
  17. {
  18. static ConcurrentDictionary<string, SubscriberServiceMethodInfo> SubscriberServiceMethodDictionary = new ConcurrentDictionary<string, SubscriberServiceMethodInfo>();
  19. /// <summary>
  20. /// 注册订阅
  21. /// </summary>
  22. /// <param name="service">注入容器</param>
  23. /// <param name="assemblyName">订阅者的程序集名称</param>
  24. /// <param name="subscribeClassEndsWithName">订阅者类的后缀名称</param>
  25. public static void RabbitMQRegisterSubscriber(this IServiceCollection service, string assemblyName, string subscribeClassEndsWithName)
  26. {
  27. if (string.IsNullOrEmpty(assemblyName))
  28. throw new ArgumentNullException(nameof(assemblyName));
  29. var assembly = RuntimeHelper.GetAssembly(assemblyName);
  30. if (assembly == null)
  31. {
  32. throw new DllNotFoundException($"the dll \"{assemblyName}\" not be found");
  33. }
  34. //获取程序集中实现了ISubscribe接口的类型
  35. var subscribeTypes = assembly.GetTypes().Where(t => t.GetTypeInfo().IsClass && typeof(ISubscribe).IsAssignableFrom(t.GetTypeInfo()));
  36. var serviceProvider = service.BuildServiceProvider();
  37. var rabbitMqSubscriber = serviceProvider.GetService<IRabbitMqSubscriber>();
  38. var subscribeTypeList = subscribeTypes.Where(s => s.GetMethods().Where(e => e.GetCustomAttribute(typeof(SubscribeAttribute)) != null).Any()).ToList();
  39. foreach (var type in subscribeTypeList)
  40. {
  41. var subscribeType = type.GetInterfaces().FirstOrDefault(e => e.Name.EndsWith(subscribeClassEndsWithName));
  42. var subscribeService = serviceProvider.GetService(subscribeType);
  43. var methodList = type.GetMethods().Where(e => e.GetCustomAttribute(typeof(SubscribeAttribute)) != null).ToList();
  44. foreach (var method in methodList)
  45. {
  46. SubscriberServiceMethodInfo subscriberServiceMethod = new SubscriberServiceMethodInfo();
  47. subscriberServiceMethod.SubscriberService = subscribeService;
  48. var subscribeAttribute = method.GetCustomAttribute(typeof(SubscribeAttribute)) as SubscribeAttribute;
  49. if (subscribeAttribute != null)
  50. {
  51. var methodParameters = method.GetParameters().FirstOrDefault();
  52. var parameterType = methodParameters?.ParameterType;
  53. var queueName = subscribeAttribute.QueueName;
  54. subscriberServiceMethod.MethodInfo = method;
  55. subscriberServiceMethod.MethodParameterType = parameterType;
  56. SubscriberServiceMethodDictionary.GetOrAdd(queueName, subscriberServiceMethod);
  57. rabbitMqSubscriber.Subscriber(queueName);
  58. }
  59. }
  60. }
  61. }
  62. public static void RabbitMQControllerRegisterSubscriber(this IServiceCollection service, string assemblyName)
  63. {
  64. if (string.IsNullOrEmpty(assemblyName))
  65. throw new ArgumentNullException(nameof(assemblyName));
  66. var assembly = RuntimeHelper.GetAssembly(assemblyName);
  67. if (assembly == null)
  68. {
  69. throw new DllNotFoundException($"the dll \"{assemblyName}\" not be found");
  70. }
  71. var assemblys = assembly.GetTypes().AsEnumerable()
  72. .Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToList();
  73. var serviceProvider = service.BuildServiceProvider();
  74. var rabbitMqSubscriber = serviceProvider.GetService<IRabbitMqSubscriber>();
  75. assemblys.ForEach(type =>
  76. {
  77. var subscribeService = serviceProvider.GetService(type);
  78. var methodList = type.GetMethods().Where(e => e.GetCustomAttribute(typeof(SubscribeAttribute)) != null).ToList();
  79. foreach (var method in methodList)
  80. {
  81. SubscriberServiceMethodInfo subscriberServiceMethod = new SubscriberServiceMethodInfo();
  82. subscriberServiceMethod.SubscriberService = subscribeService;
  83. if (method.GetCustomAttribute(typeof(SubscribeAttribute)) is SubscribeAttribute subscribeAttribute)
  84. {
  85. var methodParameters = method.GetParameters().FirstOrDefault();
  86. var parameterType = methodParameters?.ParameterType;
  87. var queueName = subscribeAttribute.QueueName;
  88. subscriberServiceMethod.MethodInfo = method;
  89. subscriberServiceMethod.MethodParameterType = parameterType;
  90. SubscriberServiceMethodDictionary.GetOrAdd(queueName, subscriberServiceMethod);
  91. rabbitMqSubscriber.Subscriber(queueName);
  92. }
  93. }
  94. });
  95. }
  96. /// <summary>
  97. /// 根据队列名称获取订阅者的方法
  98. /// </summary>
  99. /// <param name="queueName"></param>
  100. /// <returns></returns>
  101. public static SubscriberServiceMethodInfo GetSubscriberMethod(string queueName)
  102. {
  103. SubscriberServiceMethodDictionary.TryGetValue(queueName, out SubscriberServiceMethodInfo subscriberServiceMethod);
  104. return subscriberServiceMethod;
  105. }
  106. }
  107. public class SubscriberServiceMethodInfo
  108. {
  109. /// <summary>
  110. /// 方法
  111. /// </summary>
  112. public MethodInfo MethodInfo { get; set; }
  113. /// <summary>
  114. /// 订阅者Service
  115. /// </summary>
  116. public object SubscriberService { get; set; }
  117. /// <summary>
  118. /// 参数类型
  119. /// </summary>
  120. public Type MethodParameterType { get; set; }
  121. }
  122. }