2016-06-29 4 views
3

나는 spring @retryable을 구성 가능하게 만들 수있는 방법은 무엇입니까?

@Retryable(maxAttempts = 3, stateful = true, include = ServiceUnavailableException.class, 
     exclude = URISyntaxException.class, backoff = @Backoff(delay = 1000, multiplier = 2)) 
public void testThatService(String serviceAccountId) 
     throws ServiceUnavailableException, URISyntaxException { 

코드

이 조각이 여기 // 일부 구현}

내가 @Value를 사용하여 maxAttempts, 지연 및 구성 승수를 만들 수있는 방법이 있나요? 또는 주석 내에서 해당 필드를 구성 가능하게 만드는 다른 방법이 있습니까?

답변

2

현재로서는 불가능합니다. 속성을 연결하려면 주석을 String 값으로 변경해야하고 주석 bean 후 처리기는 자리 표시 자 및/또는 SpEL 표현식을 해결해야합니다.

대체 방법으로는 this answer을 참조하십시오. 그러나 현재 주석을 통해 수행 할 수는 없습니다.

편집 EchoService.test는 당신이 시도를 적용 할 방법

<bean id="retryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor"> 
    <property name="retryOperations"> 
     <bean class="org.springframework.retry.support.RetryTemplate"> 
      <property name="retryPolicy"> 
       <bean class="org.springframework.retry.policy.SimpleRetryPolicy"> 
        <property name="maxAttempts" value="${max.attempts}" /> 
       </bean> 
      </property> 
      <property name="backOffPolicy"> 
       <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy"> 
        <property name="initialInterval" value="${delay}" /> 
        <property name="multiplier" value="${multiplier}" /> 
       </bean> 
      </property> 
     </bean> 
    </property> 
</bean> 

<aop:config> 
    <aop:pointcut id="retries" 
     expression="execution(* org..EchoService.test(..))" /> 
    <aop:advisor pointcut-ref="retries" advice-ref="retryAdvice" 
     order="-1" /> 
</aop:config> 

.

@Value("${retry.max-attempts}") 
private int maxAttempts; 
@Value("${retry.delay}") 
private long delay; 

@Bean 
public RetryTemplate retryTemplate() { 
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); 
    retryPolicy.setMaxAttempts(maxAttempts); 

    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); 
    backOffPolicy.setBackOffPeriod(delay); 

    RetryTemplate template = new RetryTemplate(); 
    template.setRetryPolicy(retryPolicy); 
    template.setBackOffPolicy(backOffPolicy); 
    return template; 
} 

를 그리고이 템플릿의 execute 방법을 사용합니다 :

+0

당신이 마음을 해달라고하면, 당신은 RetryOperationsInterceptor를 사용뿐만 아니라 예를 제공 할 수있다 :

그래서 당신이 뭔가를해야합니까? – Sabarish

+1

XML config를 사용하여 예제를 추가했습니다. –

0

당신은 다음과 같이 RetryTemplate 콩 대신 @Retryable 주석을 사용할 수 있습니다 봄 재시도 버전 1.2의 출시와 함께

@Autowired 
private RetryTemplate retryTemplate; 

public ResponseVo doSomething(final Object data) { 
    RetryCallback<ResponseVo, SomeException> retryCallback = new RetryCallback<ResponseVo, SomeException>() { 
     @Override 
     public ResponseVo doWithRetry(RetryContext context) throws SomeException { 
      // do the business 
      return responseVo; 
     } 
    }; 
    return retryTemplate.execute(retryCallback); 
} 
4

을, 있을 수있다. @Retryable은 SPEL을 사용하여 구성 할 수 있습니다. 자세한 내용은

@Retryable(value = { SomeException.class,AnotherException.class }, maxAttemptsExpression = "#{@myBean.getMyProperties('retryCount')}", backoff = @Backoff(delayExpression = "#{@myBean.getMyProperties('retryInitalInterval')}")) 
    public void doJob(){ 
    //your code here 
     } 

은 참조 : https://github.com/spring-projects/spring-retry/blob/master/README.md는 여기에서 설명하는 바와 같이

0

: https://stackoverflow.com/a/43144064

버전 1.2은 특정 속성에 대한 표현식을 사용할 수있는 기능을 소개합니다.

@Retryable(maxAttempts = 3, stateful = true, include = ServiceUnavailableException.class, 
     exclude = URISyntaxException.class, backoff = @Backoff(delayExpression = "#{${your.delay}}" , multiplier = 2)) 
public void testThatService(String serviceAccountId) 
     throws ServiceUnavailableException, URISyntaxException {