RabbitMQ 重启后,为啥消费者也要重启

RabbitMQ 重启后,为啥消费者也要重启

 

 

xml 配置


    <bean id="xxxx"  class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">  
        <property name="queueNames" value="xxxx"></property> 
        <property name="connectionFactory" ref="jyMqConnectionFactory4Common"></property>  
        <property name="messageListener" ref="xxxx"></property>
        <property name="maxConcurrentConsumers" value="100" />
        <property name="concurrentConsumers" value="20" />
        <property name="adviceChain">
            <array>
                <ref bean="retryInterceptor" />
            </array>
        </property>
        <property name="autoStartup" value="false" />
    </bean>

BlockingQueueConsumer

public void start() throws AmqpException {
		if (logger.isDebugEnabled()) {
			logger.debug("Starting consumer " + this);
		}
		try {
			this.resourceHolder = ConnectionFactoryUtils.getTransactionalResourceHolder(connectionFactory, transactional);
			this.channel = resourceHolder.getChannel();
		}
		catch (AmqpAuthenticationException e) {
			throw new FatalListenerStartupException("Authentication failure", e);
		}
		this.consumer = new InternalConsumer(channel);
		this.deliveryTags.clear();
		this.activeObjectCounter.add(this);

		// mirrored queue might be being moved
		int passiveDeclareTries = 3;
		do {
			try {
				attemptPassiveDeclarations();
				if (passiveDeclareTries < 3 && logger.isInfoEnabled()) {
					logger.info("Queue declaration succeeded after retrying");
				}
				passiveDeclareTries = 0;
			}
			catch (DeclarationException e) {
				if (passiveDeclareTries > 0 && channel.isOpen()) {
					if (logger.isWarnEnabled()) {
						logger.warn("Queue declaration failed; retries left=" + (passiveDeclareTries-1), e);
						try {
							Thread.sleep(5000);
						}
						catch (InterruptedException e1) {
							Thread.currentThread().interrupt();
						}
					}
				}
				else if (e.getFailedQueues().size() < this.queues.length) {
					if (logger.isWarnEnabled()) {
						logger.warn("Not all queues are available; only listening on those that are - configured: "
								+ Arrays.asList(this.queues) + "; not available: " + e.getFailedQueues());
					}
					this.missingQueues.addAll(e.getFailedQueues());
					this.lastRetryDeclaration = System.currentTimeMillis();
				}
				else {
					this.activeObjectCounter.release(this);
					throw new QueuesNotAvailableException("Cannot prepare queue for listener. "
							+ "Either the queue doesn't exist or the broker will not allow us to use it.", e);
				}
			}
		}
		while (passiveDeclareTries-- > 0);

		if (!acknowledgeMode.isAutoAck()) {
			// Set basicQos before calling basicConsume (otherwise if we are not acking the broker
			// will send blocks of 100 messages)
			try {
				channel.basicQos(prefetchCount);
			}
			catch (IOException e) {
				this.activeObjectCounter.release(this);
				throw new FatalListenerStartupException("Cannot set basicQos.", e);
			}
		}


		try {
			for (String queueName : queues) {
				if (!this.missingQueues.contains(queueName)) {
					consumeFromQueue(queueName);
				}
			}
		}
		catch (IOException e) {
			throw RabbitExceptionTranslator.convertRabbitAccessException(e);
		}
	}

中最重要的

 

try {
			for (String queueName : queues) {
				if (!this.missingQueues.contains(queueName)) {
					consumeFromQueue(queueName);
				}
			}
		}
		catch (IOException e) {
			throw RabbitExceptionTranslator.convertRabbitAccessException(e);
		}

 

wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com
©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值