【发布时间】:2023-02-15 13:25:02
【问题描述】:
在我的 Spring Boot 项目中,我使用 RabbitMQ 进行消息传递,并且有两个例外 CustomExceptionA 和 CustomExceptionB。我想让我的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