How can I make spring @retryable configurable?

Sabarish picture Sabarish · Jun 29, 2016 · Viewed 26.4k times · Source

I have this piece of code

@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 {

//some implementation here }

Is there a way I can make the maxAttempts , delay and multiplier configurable using @Value? Or is there any other approach to make such fields inside annotations configurable?

Answer

Satish picture Satish · Jan 23, 2017

with the release of Spring-retry version 1.2, it's possible. @Retryable can be configured using SPEL.

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

For more details refer: https://github.com/spring-projects/spring-retry/blob/master/README.md