0

Scenario : I need to modify maxAttempts value of @Retryable at runtime, so that the number of retries can be driven from database

@Service
public class PropertyHolder {
 private int retryCount= 2; 
 public int retryCount() { return retryCount; }
 
 @Scheduled(fixedRate=3600000) //Query DB and update retryCount every 60mins
 private void updateRetryCount(int in) {
   this.retryCount = 0; //Fetch retryCount from DB and update retryCount
 }
}


@Service
public class SimpleService{
 @Retryable( value={ Throwable.class }, maxAttemptsExpression="#{@propertyHolder.retryCount()}")
 private void performTask() {
   // do some opertion that throws Exception
 }
}

PropertyHolder will update retryCount once in every 60 minutes.
This PropertHolder#retryCount needs to be wired to @Retryable in SimpleService#performTask .

At present, it takes only the initial value of retryCount (2).

Is this a right approach or Am I making some terrible mistake?

Suresh Anbarasan
  • 943
  • 1
  • 8
  • 20

1 Answers1

0

No; currently the expression is only evaluated during context initialization; there is an open feature request to add runtime evaluation.

https://github.com/spring-projects/spring-retry/issues/184

Currently you have to wire up your own interceptor with a mutable retry policy and configure it via the interceptor property in @Retryable.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179