2016-10-04 62 views
1

我有以下的配置春季兔子重试提供被拒绝的消息..它可以吗?

spring.rabbitmq.listener.prefetch=1 
spring.rabbitmq.listener.concurrency=1 
spring.rabbitmq.listener.retry.enabled=true 
spring.rabbitmq.listener.retry.max-attempts=3 
spring.rabbitmq.listener.retry.max-interval=1000 
spring.rabbitmq.listener.default-requeue-rejected=false //I have also changed it to true but the same behavior still happens 

,并在我的听众我抛出异常AmqpRejectAndDontRequeueException拒绝邮件和执行兔子不要试图重新提交它 ...但兔子redilvers它的3倍然后最终将其路由到死信队列。

这是标准的行为根据我提供的配置还是我错过了什么?

回答

1

您必须将重试策略配置为不针对该异常重试。

你不能用属性来做到这一点,你必须自己配置重试建议。

我稍后会发布一个例子,如果您需要帮助。

requeue-rejected位于容器级别(低于堆栈重试)。

编辑

@SpringBootApplication 
public class So39853762Application { 

    public static void main(String[] args) throws Exception { 
     ConfigurableApplicationContext context = SpringApplication.run(So39853762Application.class, args); 
     Thread.sleep(60000); 
     context.close(); 
    } 

    @RabbitListener(queues = "foo") 
    public void foo(String foo) { 
     System.out.println(foo); 
     if ("foo".equals(foo)) { 
      throw new AmqpRejectAndDontRequeueException("foo"); // won't be retried. 
     } 
     else { 
      throw new IllegalStateException("bar"); // will be retried 
     } 
    } 

    @Bean 
    public ListenerRetryAdviceCustomizer retryCustomizer(SimpleRabbitListenerContainerFactory containerFactory, 
      RabbitProperties rabbitPropeties) { 
     return new ListenerRetryAdviceCustomizer(containerFactory, rabbitPropeties); 
    } 

    public static class ListenerRetryAdviceCustomizer implements InitializingBean { 

     private final SimpleRabbitListenerContainerFactory containerFactory; 

     private final RabbitProperties rabbitPropeties; 

     public ListenerRetryAdviceCustomizer(SimpleRabbitListenerContainerFactory containerFactory, 
       RabbitProperties rabbitPropeties) { 
      this.containerFactory = containerFactory; 
      this.rabbitPropeties = rabbitPropeties; 
     } 

     @Override 
     public void afterPropertiesSet() throws Exception { 
      ListenerRetry retryConfig = this.rabbitPropeties.getListener().getRetry(); 
      if (retryConfig.isEnabled()) { 
       RetryInterceptorBuilder<?> builder = (retryConfig.isStateless() 
         ? RetryInterceptorBuilder.stateless() 
         : RetryInterceptorBuilder.stateful()); 
       Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>(); 
       retryableExceptions.put(AmqpRejectAndDontRequeueException.class, false); 
       retryableExceptions.put(IllegalStateException.class, true); 
       SimpleRetryPolicy policy = 
         new SimpleRetryPolicy(retryConfig.getMaxAttempts(), retryableExceptions, true); 
       ExponentialBackOffPolicy backOff = new ExponentialBackOffPolicy(); 
       backOff.setInitialInterval(retryConfig.getInitialInterval()); 
       backOff.setMultiplier(retryConfig.getMultiplier()); 
       backOff.setMaxInterval(retryConfig.getMaxInterval()); 
       builder.retryPolicy(policy) 
        .backOffPolicy(backOff) 
        .recoverer(new RejectAndDontRequeueRecoverer()); 
       this.containerFactory.setAdviceChain(builder.build()); 
      } 
     } 

    } 

} 

注:您当前无法配置策略重试所有的异常,“除了”这个 - 你有你想要重试的所有异常(分类,他们不能成为AmqpRejectAndDontRequeueException的超类)。我已打开issue to support this

+0

thnx加里......等你的帖子 –

+0

我编辑了我的答案。 –

相关问题