RabbitHandler建立消费者和春季的致命异常重试队列倾听的RabbitMQ(RabbitHa

2019-10-30 13:37发布

我使用Spring AMQP RabbitHandler,并写了下面的代码: -

@RabbitListener(queues = "#{testQueue.name}")
public class Tut4Receiver {

  @RabbitHandler
  public void receiveMessage(String message){
       System.out.println("Message received "+message);
  }
}

也队列的定义如下: -

@Bean
public Queue testQueue() {
    return new AnonymousQueue();
}

我使用单独的代码初始化连接工厂。

我的问题是,如果RabbitMQ的是下跌了一段时间,它不断重试创建一个消费者,但只有当它临危一个ConnectionRefused错误。 但是,假如用户没有在RabbitMQ的存在,有其新的用户将产生的缝隙,然后将其从RabbitMQ的收到一个致命错误,它永远不会重试由于该结果是自动删除队列将兔MQ创建没有任何消费者。

堆栈跟踪 :-

    SimpleMessageListenerContainer] [SimpleAsyncTaskExecutor-11] [|] [|||] Consumer received fatal exception on startup 
org.springframework.amqp.rabbit.listener.exception.FatalListenerStartupException: Authentication failure
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:476)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1280)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.amqp.AmqpAuthenticationException: com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using     authentication mechanism PLAIN. For details see the broker logfile.
    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:65)
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:309)
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:547)
    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils$1.createConnection(ConnectionFactoryUtils.java:90)
    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.doGetTransactionalResourceHolder(ConnectionFactoryUtils.java:140)
    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.getTransactionalResourceHolder(ConnectionFactoryUtils.java:76)
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:472)
    ... 2 common frames omitted
Caused by: com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker     logfile.
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:339)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:813)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:767)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:887)
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:300)

 SimpleMessageListenerContainer] [SimpleAsyncTaskExecutor-11] [|] [|||] Stopping container from aborted consumer 
  [|] [|||] Waiting for workers to finish. 
  [|] [|||] Successfully waited for workers to finish. 

甚至任何方式重试致命的异常,如用户不存在?

Answer 1:

验证失败被认为是默认致命的,而不是重试。

您可以通过监听器容器(上设置属性覆盖此行为possibleAuthenticationFailureFatal )。 该物业是不是可以作为一个引导属性,所以你必须替换启动的容器厂...

    @Bean(name = "rabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {

        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setContainerConfigurer(smlc -> smlc.setPossibleAuthenticationFailureFatal(false));
        return factory;
    }


文章来源: RabbitHandler to create consumer and retry on Fatal Exception in Spring for queue on listening to rabbitmq