2016-10-04 21 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

我編輯了我的答案。 –

相關問題