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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/wangming520liwei/article/details/101211304

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);
		}

猜你喜欢

转载自blog.csdn.net/wangming520liwei/article/details/101211304