rabbitmq 配置 SimpleRabbitListenerContainerFactory 如何 新增消费者

本文介绍了如何配置RabbitMQ的SimpleRabbitListenerContainerFactory以新增消费者,详细阐述了从配置containerFactory属性开始,经过spring初始化流程,直到消费者启动和动态调整的整个过程。在启动过程中涉及了RabbitListenerEndpointRegistrar、AbstractMessageListenerContainer等多个关键组件,还讲解了在特定条件下的消费者增加和减少策略。同时,文中也提到了自定义日志记录和任务代码逻辑的执行情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码

	
@Configuration
@EnableRabbit
public class RabbitMqConfigurer {
   
   

// 在容器中注入  SimpleRabbitListenerContainerFactory 
	    @Bean("customContainerFactory")
	    public SimpleRabbitListenerContainerFactory customContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
	                                                                       ConnectionFactory connectionFactory) {
   
   
	        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
	        factory.setConcurrentConsumers(4);  //设置线程数
	        factory.setMaxConcurrentConsumers(4); //最大线程数
	        configurer.configure(factory, connectionFactory);
	        return factory;
	    }
}

rabbitmq 配置 containerFactory 属性

      @RabbitListener(queues = "${custom.queue}", containerFactory = "customContainerFactory")
    public void publish(Entity dto) {
   
   
    
    }

rabbitmq 执行流程

RabbitListenerEndpointRegistrar 实现 InitializingBean 接口,启动会自动被调用

org.springframework.beans.factory.InitializingBean#afterPropertiesSet方法

	@Override
	public void afterPropertiesSet() {
   
   
		registerAllEndpoints();
	}

注册所有的端点

protected void registerAllEndpoints() {
   
   
		Assert.state(this.endpointRegistry != null, "No registry available");
		synchronized (this.endpointDescriptors) {
   
   
			for (AmqpListenerEndpointDescriptor descriptor : this.endpointDescriptors) {
   
   
				if (descriptor.endpoint instanceof MultiMethodRabbitListenerEndpoint && this.validator != null) {
   
   
					((MultiMethodRabbitListenerEndpoint) descriptor.endpoint).setValidator(this.validator);
				}
				this.endpointRegistry.registerListenerContainer(// NOSONAR never null
						descriptor.endpoint, 
                   
                    resolveContainerFactory(descriptor));
			}
			this.startImmediately = true;  // trigger immediate startup
		}
	}

org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar#resolveContainerFactory

private RabbitListenerContainerFactory<?> resolveContainerFactory(AmqpListenerEndpointDescriptor descriptor) {
   
   
		if (descriptor.containerFactory != null) {
   
   
			return descriptor.containerFactory;
		}
		else if (this.containerFactory != null) {
   
   
			return this.containerFactory;
		}
		else if (this.containerFactoryBeanName != null) {
   
   
			Assert.state(this.beanFactory != null, "BeanFactory must be set to obtain container factory by bean name");
            // 得到注入容器中的  containerFactory
			this.containerFactory = this.beanFactory.getBean(
					this.containerFactoryBeanName, RabbitListenerContainerFactory.class);
			return this.containerFactory;  // Consider changing this if live change of the factory is required
		}
		else {
   
   
			throw new IllegalStateException("Could not resolve the " +
					RabbitListenerContainerFactory.class.getSimpleName() + " to use for [" +
					descriptor.endpoint + "] no factory was given and no default is set.");
		}
	}

spring执行到 org.springframework.context.support.AbstractApplicationContext#finishRefresh 方法

	protected void finishRefresh() {
   
   
		// Clear context-level resource caches (such as ASM metadata from scanning).
		clearResourceCaches();

		// Initialize lifecycle processor for this context.
		initLifecycleProcessor();
		// 这一步
		// Propagate refresh to lifecycle processor first.
		getLifecycleProcessor().onRefresh();

		// Publish the final event.
		publishEvent(new ContextRefreshedEvent(this));

		// Participate in LiveBeansView MBean, if active.
		if (!NativeDetector.inNativeImage()) {
   
   
			LiveBeansView.registerApplicationContext(this);
		}
	}

org.springframework.context.support.DefaultLifecycleProcessor#startBeans

private void startBeans(boolean autoStartupOnly) {
		Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
		Map<Integer, LifecycleGroup> phases = new TreeMap<>();

		lifecycleBeans.forEach((beanName, bean) -> {
			if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
				int phase = getPhase(bean);
				phases.computeIfAbsent(
						phase,
						p -> new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly)
				).add(beanName, bean);
			}
		});
		if (!phases.isEmpty()) {
		//执行start
			phases.values().forEach(LifecycleGroup::start);
		}
	}

org.springframework.context.support.DefaultLifecycleProcessor.LifecycleGroup#start

public void start() {
   
   
			if (this.members.isEmpty()) {
   
   
				return;
			}
			if (logger.isDebugEnabled()) {
   
   
				logger.debug("Starting beans in phase " + this.phase);
			}
			Collections.sort(this.members);
			for (LifecycleGroupMember member : this.members) {
   
   
				doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);
			}
		}

org.springframework.context.support.DefaultLifecycleProcessor#doStart

private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
   
   
		Lifecycle bean = lifecycleBeans.remove(beanName);
		if (bean != null && bean != this) {
   
   
			String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
			for (String dependency : dependenciesForBean) {
   
   
				doStart(lifecycleBeans, dependency, autoStartupOnly);
			}
			if (!bean.isRunning() &&
					(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
   
   
				if (logger.isTraceEnabled()) {
   
   
					logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
				}
				try {
   
   
                    // 调用start
					bean.start();
				}
				catch (Throwable ex) {
   
   
					throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
				}
				if (logger.isDebugEnabled()) {
   
   
					logger.debug("Successfully started bean '" + beanName + "'");
				}
			}
		}
	}

执行 RabbitListenerEndpointRegistry 的start方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值