RabbitListener异常不将消息发送到死信队列

时间:2019-09-06 12:50:54

标签: spring spring-boot rabbitmq spring-rabbitmq

我在Spring Boot应用程序中有一个RabbitListener,它会在发生异常时重试并将消息发送到死信队列。但是,如果在我的消息侦听器上发生一个特定的异常,我不希望重试该异常并进入死信队列,但是我仍然希望事务回滚。有办法吗?

如下面的代码中所述,我尝试捕获Exception,但是使用者重新启动。如果我抛出AmqpRejectAndDontRequeueException消息,将不会重试该消息,但最终将导致死信队列

这是我的监听器方法:

    @RabbitListener(queues = "#{T(com.myproject.RabbitBinding).PROCESS_MESSAGE.getQueue()}")
    public void onMessage(MyMessage message)
    {
        if (log.isDebugEnabled())
        {
            log.debug("Received message: {}", message);
        }

        try
        {
            process.run(message);
        }
        catch (IllegalStateException e)
        {
            log.error("Exception occured: e", e);
        }

每当我捕获到异常时,它都会输出异常,但是由于我看到以下消息,使用者重新启动:

org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer $ AsyncMessageProcessingConsumer killOrRestart

这是我的Rabbit配置:

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            Jackson2JsonMessageConverter messageConverter,
            RetryAdviceChainFactory deadLetterRetryAdviceChainFactory,
            PlatformTransactionManager transactionManager)
    {
        final SimpleRabbitListenerContainerFactory cf = new SimpleRabbitListenerContainerFactory();
        cf.setConnectionFactory(connectionFactory);
        cf.setConcurrentConsumers(rabbitConcurrentConsumers);
        cf.setErrorHandler(defaultErrorHandler);
        cf.setAdviceChain(deadLetterRetryAdviceChainFactory.createDefaultRetryChain());
        cf.setChannelTransacted(true);
        cf.setTaskExecutor(taskExecutor);
        cf.setMessageConverter(messageConverter);
        cf.setTransactionManager(transactionManager);
        return cf;
    }
    @Bean
    protected RetryAdviceChainFactory deadLetterRetryAdviceChainFactory(
            MessageRecoverer deadLetterMessageRecoverer)
    {
        RetryAdviceChainFactory factory = new RetryAdviceChainFactory();
        factory.setMessageRecoverer(deadLetterMessageRecoverer);
        return factory;
    }

1 个答案:

答案 0 :(得分:1)

引发InstantAcknowledgeAmqpException。