spring amqp通过配置启用重试,并根据指定的异常阻止它 [英] spring amqp enable retry by configuration and prevent it according to a specified exception
问题描述
我有以下两种情况
- 在 ExceptionA 的情况下:重试有限次 最后,当重试次数用完时,将消息写入 死信队列
- 对于 ExceptionB :只需将消息写入 死信队列
- In case of ExceptionA : retrying for finite number of times and finally when number of retrials exhausted, message is written in a dead letter queue
- In case of ExceptionB : simply, message should be written to dead letter queue
我想在相同的侦听器容器工厂和相同的队列上支持这两种情况.
I want to support the two cases on the same listener container factory and the same queue.
我已经具有以下配置来成功支持案例1 :
I already have the following configuration to support case 1 successfully:
@Bean
public RetryOperationsInterceptor workMessagesRetryInterceptor() {
return RetryInterceptorBuilder.stateless()
.maxAttempts(5)
.backOffOptions(1000, 2, 10000)
.recoverer(new RejectAndDontRequeueRecoverer())
.build();
}
@Bean
public SimpleRabbitListenerContainerFactory myRabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMaxConcurrentConsumers(8);
factory.setAdviceChain(workMessagesRetryInterceptor());
return factory;
}`
现在,我也想扩展先前的配置以支持案例2.
Now I want to extend the previous configuration to support case 2 too.
编辑,感谢Gary的快速回复.
Edit, thanks Gary for your fast response.
这是我的新配置,但是我仍然重试这两个例外:ListenerExecutionFailedException,AmqpRejectAndDontRequeueException
Here you are my new configuration, but I still get retrials on both the two exceptions : ListenerExecutionFailedException , AmqpRejectAndDontRequeueException
@Bean
public SimpleRetryPolicy rejectionRetryPolicy(){
Map<Class<? extends Throwable> , Boolean> exceptionsMap = new HashMap<Class<? extends Throwable> , Boolean>();
exceptionsMap.put(ListenerExecutionFailedException.class, true); //retriable
exceptionsMap.put(AmqpRejectAndDontRequeueException.class, false);//not retriable
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(5 , exceptionsMap );
return retryPolicy;
}
@Bean
public RetryOperationsInterceptor workMessagesRetryInterceptor() {
return RetryInterceptorBuilder.stateless().retryPolicy(rejectionRetryPolicy())
//.backOffOptions(1000, 2, 10000)
//.recoverer(new RejectAndDontRequeueRecoverer())
.build();
}
推荐答案
为SimpleRetryPolicy
提供异常和布尔值映射(是否重试).您可以选择遍历异常原因树以查找特定的异常.参见
Provide a SimpleRetryPolicy
with a map of exceptions and booleans (whether or not to retry). You can optionally traverse the exception cause tree to find the specific exception. See the Javadocs for SimpleRetryPolicy.
这篇关于spring amqp通过配置启用重试,并根据指定的异常阻止它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!