【问题标题】:How to prevent a specific exception from being retried in RabbitMQ如何防止在 RabbitMQ 中重试特定异常
【发布时间】:2023-02-15 13:25:02
【问题描述】:

在我的 Spring Boot 项目中,我使用 RabbitMQ 进行消息传递,并且有两个例外 CustomExceptionACustomExceptionB。我想让我的CustomExceptionA重试n次,CustomExceptionB不重试直接发到DLQ。

以下是我的配置:-

yaml文件

spring:
  rabbitmq:
    listener:
      simple:
        default-requeue-rejected: false
        retry:
          enabled: true
          initial-interval: 2s
          max-attempts: 3
          max-interval: 2s
          multiplier: 1

配置文件

 @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory,
                                                                               SimpleRabbitListenerContainerFactoryConfigurer configurer) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setErrorHandler(errorHandler());
        return factory;
    }

    @Bean
    public ErrorHandler errorHandler() {
        return new ConditionalRejectingErrorHandler(customExceptionStrategy());
    }

    @Bean
    FatalExceptionStrategy customExceptionStrategy() {
        return new CustomFatalExceptionStrategy();
    }
-----------------------------------------------------------------------------------------------------------------------------

@Component
public class CustomFatalExceptionStrategy extends ConditionalRejectingErrorHandler.DefaultExceptionStrategy {

    @Override
    public boolean isFatal(Throwable throwable) {
        return (throwable.getCause() instanceof CustomExceptionB);
    }
}

根据博客:https://www.baeldung.com/spring-amqp-error-handling 机制应该有效,但由于某种原因它对我不起作用。

有人请看问题。

【问题讨论】:

    标签: spring-boot rabbitmq spring-amqp spring-rabbit


    【解决方案1】:

    涵盖您对 Am​​qpRejectAndDontRequeueException 的异常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多