How to inject config properties in Spring Boot to Spring Retry annotation?
Staring from spring-retry-1.2.0 we can use configurable properties in @Retryable annotation.
Use "maxAttemptsExpression", Refer the below code for usage,
@Retryable(maxAttemptsExpression = "#{${my.app.maxAttempts}}", backoff = @Backoff(delayExpression = "#{${my.app. backOffDelay}}"))
It will not work if you use any version less than 1.2.0.Also you don't require any configurable property classes.
You can also use existing beans in expression attributes.
@Retryable(include = RuntimeException.class, maxAttemptsExpression = "#{@retryProperties.getMaxAttempts()}", backoff = @Backoff(delayExpression = "#{@retryProperties.getBackOffInitialInterval()}", maxDelayExpression = "#{@retryProperties.getBackOffMaxInterval" + "()}", multiplierExpression = "#{@retryProperties.getBackOffIntervalMultiplier()}")) String perform(); @Recover String recover(RuntimeException exception);
where
retryProperties
is your bean which holds retry related properties as in your case.
You can use Spring EL as shown below to load the properties:
@Retryable(maxAttempts="${my.app.maxAttempts}", include=TimeoutException.class, backoff=@Backoff(value ="${my.app.backOffDelay}"))