当前位置:   article > 正文

源码解析: Spring RabbitMQ消费者_rabbitmqbeanprocessor

rabbitmqbeanprocessor

Spring RabbitMQ消费者启动,到接收消息和执行消费逻辑,一步步了解其实现。

1. 消费者如何启动过程

1.1 启动配置类

创建RabbitListenerAnnotationBeanPostProcessor

@Configuration
public class RabbitBootstrapConfiguration {
   

	@Bean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public RabbitListenerAnnotationBeanPostProcessor rabbitListenerAnnotationProcessor() {
   
		return new RabbitListenerAnnotationBeanPostProcessor();
	}
.....
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

1.2 创建消费者核心逻辑

核心逻辑在RabbitListenerAnnotationBeanPostProcessor,在Spring Bean初始化过程中执行。
对于每个消息监听都会创建对应的MessageListenerContainer(默认实现为SimpleMessageListenerContainer)

// 通过BeanPostProcessor在Bean创建后,创建消息监听器
public class RabbitListenerAnnotationBeanPostProcessor
		implements BeanPostProcessor, Ordered, BeanFactoryAware, BeanClassLoaderAware, EnvironmentAware,
			SmartInitializingSingleton {
   
  ......
      @Override
      public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
   
      Class<?> targetClass = AopUtils.getTargetClass(bean);
      // 通过反射获取@RabbitListener修饰的方法
      final TypeMetadata metadata = this.typeCache.computeIfAbsent(targetClass, this::buildMetadata);
      for (ListenerMethod lm : metadata.listenerMethods) {
   
          for (RabbitListener rabbitListener : lm.annotations) {
   
              // 创建MethodRabbitListenerEndpoint,并注册到RabbitListenerEndpointRegistrar
              processAmqpListener(rabbitListener, lm.method, bean, beanName);
          }
      }
      if (metadata.handlerMethods.length > 0) {
   
          processMultiMethodListeners(metadata.classAnnotations, metadata.handlerMethods, bean, beanName);
      }
      return bean;
  }

	protected void processAmqpListener(
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/424452
推荐阅读
相关标签
  

闽ICP备14008679号