Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make spring @retryable configurable?

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?

like image 621
Sabarish Avatar asked Jun 29 '16 00:06

Sabarish


People also ask

How spring Retryable works?

Spring Retry provides the ability to automatically re-invoke a failed operation. This is helpful when errors may be transient in nature. For example, a momentary network glitch, network outage, server down, or deadlock.

What is Retry template?

public class RetryTemplate extends Object implements RetryOperations. Template class that simplifies the execution of operations with retry semantics. Retryable operations are encapsulated in implementations of the RetryCallback interface and are executed using one of the supplied execute methods.

What is RetryContext?

public interface RetryContext extends org.springframework.core.AttributeAccessor. Low-level access to ongoing retry operation. Normally not needed by clients, but can be used to alter the course of the retry, e.g. force an early termination.

Does spring retry block thread?

Does the default spring-retry implementation block threads while retrying? The implementation in github indicates that it does.


1 Answers

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

like image 85
Satish Avatar answered Oct 12 '22 22:10

Satish