问题
I have the following two cases
- 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.
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;
}`
Now I want to extend the previous configuration to support case 2 too.
Edit, thanks Gary for your fast response.
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();
}
回答1:
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.
来源:https://stackoverflow.com/questions/39272648/spring-amqp-enable-retry-by-configuration-and-prevent-it-according-to-a-specifie