23

In spring boot application, I define some config properties in yaml file as below.

my.app.maxAttempts = 10
my.app.backOffDelay = 500L

And an example bean

@ConfigurationProperties(prefix = "my.app")
public class ConfigProperties {
  private int maxAttempts;
  private long backOffDelay;

  public int getMaxAttempts() {
    return maxAttempts;
  }

  public void setMaxAttempts(int maxAttempts) {
    this.maxAttempts = maxAttempts;
  }

  public void setBackOffDelay(long backOffDelay) {
    this.backOffDelay = backOffDelay;
  }

  public long getBackOffDelay() {
    return backOffDelay;
  }

How can I inject the values of my.app.maxAttempts and my.app.backOffdelay to Spring Retry annotation? In the example below, I want to replace the value 10 of maxAttempts and 500Lof backoff value with the corresponding references of config properties.

@Retryable(maxAttempts=10, include=TimeoutException.class, backoff=@Backoff(value = 500L))
marcterenzi
  • 319
  • 1
  • 2
  • 10

3 Answers3

29

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.

VelNaga
  • 3,593
  • 6
  • 48
  • 82
  • Is there any way to have the properties in yaml file only, meaning without adding the @Retryable annotation? In other words, are there any yaml config properties for the above? – Dchris Jul 25 '19 at 13:46
  • 1
    I get a `After parsing a valid expression, there is still more data in the expression: 'lcurly({)'` running this – Baptiste Pernet Aug 19 '20 at 20:58
  • I am getting the same error - there is still more data in the expression: 'lcurly({)' – Chandresh Mishra Aug 26 '20 at 12:35
12

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.

isaolmez
  • 1,015
  • 11
  • 14
0

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}"))
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • 7
    Strange that you had a few upvotes, for me it fails with a `Error:(21, 27) java: incompatible types: java.lang.String cannot be converted to int`. Seems that the EL expression is not being evaluated. – Joel Mata Sep 23 '19 at 10:14