首页 > 解决方案 > 当@Sendto失败时,rabbitMQ与spring boot amqp连接无限错误

问题描述

我是使用带有 Spring Boot 2.0.3 的 rabbitMQ。

目前我正在使用:

    @Bean
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory()
    {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

        factory.setConnectionFactory(cachingConnectionFactory);
...
        return factory;
    }

当我尝试rabbitTemplate.convertAndSend(exchange, routingKey, payload)不存在的交换时,

错误显示一次,这是可取的。

2021-03-04 16:20:15.746 ERROR CachingConnectionFactory:1302 [AMQP Connection ip:5672] - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'exchange' in vhost 'vhost', class-id=60, method-id=40)

但是,当我使用@SendTowith 时@RabbitListener,例如

@RabbitListener(queues = "test_mq_queue")
@SendTo("exchange/routingKey")

如果交换不存在,将显示错误无穷大。例如

2021-03-04 16:45:23.079 ERROR CachingConnectionFactory:1302 [AMQP Connection ip:5672] - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'exchange' in vhost 'vhost', class-id=60, method-id=40)
2021-03-04 16:45:24.100 ERROR CachingConnectionFactory:1302 [AMQP Connection ip:5672] - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'exchange' in vhost 'vhost', class-id=60, method-id=40)
2021-03-04 16:45:25.125 ERROR CachingConnectionFactory:1302 [AMQP Connection ip:5672] - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'exchange' in vhost 'vhost', class-id=60, method-id=40)
2021-03-04 16:45:26.149 ERROR CachingConnectionFactory:1302 [AMQP Connection ip:5672] - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'exchange' in vhost 'vhost', class-id=60, method-id=40)
2021-03-04 16:45:27.181 ERROR CachingConnectionFactory:1302 [AMQP Connection ip:5672] - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'exchange' in vhost 'vhost', class-id=60, method-id=40)
...

我错过了什么吗?如果需要更多信息,请告诉我。

标签: javaspringspring-bootrabbitmqspring-amqp

解决方案


默认情况下,如果处理的任何部分失败,则将消息重新排队并重新传递(无限期地)。这可以配置为拒绝并且不重新排队。

但是,当发送失败并出现此错误时,通道会被代理关闭,消息会重新排队并重新传递。Spring 没有机会进行干预以阻止重新交付。

您需要避免这种情况才能解决此问题。


推荐阅读