spring amqp enable retry by configuration and prevent it according to a specified exception

大兔子大兔子 提交于 2019-12-02 17:53:01

问题


I have the following two cases

  1. 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
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!