I have a RabbitListener in my Spring AMQP application and I'm using a custom RabbitListenerFactory with a SimpleRetryPolicy configured for message retries. However, I want to exclude certain exceptions from being retried. Currently, my configuration looks like this:
@Bean
fun retryPolicy(): SimpleRetryPolicy {
val exceptions = mutableMapOf<Class<out Throwable>, Boolean>(
ValidationException::class.java to false,
IllegalArgumentException::class.java to false,
EntityNotFoundException::class.java to false,
)
return SimpleRetryPolicy(6, exceptions, true)
}
@Bean
fun retryInterceptor(customMessageRecoverer: CustomMessageRecoverer): RetryOperationsInterceptor? {
return RetryInterceptorBuilder.stateless().retryPolicy(retryPolicy())
.backOffOptions(1000, 2.0, 5000)
.recoverer(customMessageRecoverer)
.build()
}
@Bean
fun myFactory(
configurer: SimpleRabbitListenerContainerFactoryConfigurer,
connectionFactory: ConnectionFactory,
customsMessageRecoverer: CustomMessageRecoverer,
): SimpleRabbitListenerContainerFactory {
val factory = SimpleRabbitListenerContainerFactory()
configurer.configure(factory, connectionFactory)
factory.setAdviceChain(workMessagesRetryInterceptor(customsMessageRecoverer))
return factory;
}
However, I've noticed that with this configuration, no retries are being executed at all, even for exceptions that are not in the exclusion list. How can I modify this configuration to both exclude specific exceptions from being retried, such as AuthoraizationException or IllegalArgumentException, and ensure that other exceptions are still retried according to the retry policy? Any code examples would be appreciated. Thanks in advance!