解决RabbitMq queue异常导致consumer停止问题

 更新时间:2026年05月06日 10:35:11   作者:Leon_Jinhai_Sun  
该问题为RabbitMQ队列异常导致消费者停止消费,错误码404表示队列不存在或broker不允许使用该队列,原因可能为RabbitMQ服务异常或配置问题,建议检查RabbitMQ服务状态,确认队列名称正确,并适当调整消费者重试策略

RabbitMq queue异常导致consumer停止

偶发性rabbitmq出问题或者认为操作错误,访问不了queue,导致消费端停止消费

org.springframework.amqp.rabbit.listener.QueuesNotAvailableException: Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:599)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1424)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[s.message.like.add]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:672)
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:571)
    ... 2 common frames omitted
Caused by: java.io.IOException: null
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105)
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:123)
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:992)
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:50)
    at sun.reflect.GeneratedMethodAccessor711.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:980)
    at com.sun.proxy.$Proxy231.queueDeclarePassive(Unknown Source)
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:651)
    ... 3 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - failed to perform operation on queue 'q.hell' in vhost '/' due to timeout, class-id=50, method-id=10)
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:32)
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:366)
    at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:229)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:117)
    ... 11 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - failed to perform operation on queue 's.message.like.add' in vhost '/' due to timeout, class-id=50, method-id=10)
    at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:505)
    at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:336)
    at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:143)
    at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:90)
    at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:634)
    at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:47)
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:572)
    ... 1 common frames omitted
    
    Stopping container from aborted consumer

跟踪

SimpleMessageListenerContainer

catch (QueuesNotAvailableException ex) {
				logger.error("Consumer received fatal=" + SimpleMessageListenerContainer.this.mismatchedQueuesFatal
						+ " exception on startup", ex);
				if (SimpleMessageListenerContainer.this.missingQueuesFatal) {
					logger.error("Consumer received fatal exception on startup", ex);
					this.startupException = ex;
					// Fatal, but no point re-throwing, so just abort.
					aborted = true;
				}
				publishConsumerFailedEvent("Consumer queue(s) not available", aborted, ex);
			}

SimpleMessageListenerContainer.this.missingQueuesFatal = true 将aborted

查代码跟踪,如下配置设置

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitConfiguration {

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

        //Whether to fail if the queues declared by the container are not available on the broker and/or whether to stop the container if one or more queues are deleted at runtime.
        factory.setMissingQueuesFatal(false);
        configurer.configure(factory, connectionFactory);
        return factory;
    }

}

至此涉及queue访问失败场景 会一直重试 直到可用,默认5次

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot连接Hive实现自助取数的示例

    SpringBoot连接Hive实现自助取数的示例

    这篇文章主要介绍了SpringBoot连接Hive实现自助取数的示例,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下
    2020-12-12
  • Java实现ArrayList自动扩容

    Java实现ArrayList自动扩容

    ArrayList的扩容规则是非常简单的,它会根据需要自动扩容,本文就来介绍一下Java实现ArrayList自动扩容,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • 解决Java中的java.io.IOException: Broken pipe问题

    解决Java中的java.io.IOException: Broken pipe问题

    这篇文章主要介绍了解决Java中 java.io.IOException: Broken pipe的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • 如何更快乐的使用Java 8中的Lambda特性

    如何更快乐的使用Java 8中的Lambda特性

    从java8出现以来lambda是最重要的特性之一,它可以让我们用简洁流畅的代码完成一个功能。下面这篇文章主要给大家介绍了关于如何更快乐的使用Java 8中的Lambda特性的相关资料,需要的朋友可以参考下
    2018-11-11
  • 浅谈Java继承中的转型及其内存分配

    浅谈Java继承中的转型及其内存分配

    这篇文章主要介绍了浅谈Java继承中的转型及其内存分配,首先分享了简单的代码及运行结果,然后对其进行分析,继而引出了
    2017-11-11
  • 深入剖析Java编程中的序列化

    深入剖析Java编程中的序列化

    这篇文章主要介绍了深入剖析Java编程中的序列化,文中谈到了序列化时对象的继承等各种问题,案例详尽,强烈推荐!需要的朋友可以参考下
    2015-07-07
  • Java语言----三种循环语句的区别介绍

    Java语言----三种循环语句的区别介绍

    下面小编就为大家带来一篇Java语言----三种循环语句的区别介绍。小编举得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • JavaScript中栈和队列应用详情

    JavaScript中栈和队列应用详情

    这篇文章主要介绍了JavaScript中栈和队列应用详情,栈如果用数组模拟的话是类似于一个U形桶状堆栈空间,文章围绕制图展开详细的内容展开更多相关内容,需要的小伙伴可以参考一下
    2022-06-06
  • Spring mvc Controller和RestFul原理解析

    Spring mvc Controller和RestFul原理解析

    这篇文章主要介绍了Spring mvc Controller和RestFul原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • spring boot 测试单元修改数据库不成功的解决

    spring boot 测试单元修改数据库不成功的解决

    这篇文章主要介绍了spring boot 测试单元修改数据库不成功的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09

最新评论