Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject config properties in Spring Boot to Spring Retry annotation?

Tags:

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))
like image 834
marcterenzi Avatar asked Mar 31 '17 14:03

marcterenzi


People also ask

How do I enable retry in spring boot?

First, you need to enable Spring Retry. You can achieve this by adding the @EnableRetry annotation to your @SpringBootApplication or @Configuration class. You can now use @Retryable to annotate any method to be a candidate or retry and @Recover to specify fallback 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?

However, whenever the execution of a retryable method fails with an exception, Spring will automatically retry to call the method up to three times. By default Spring uses a 1 second delay between method calls. Please note that the calling thread blocks during retry handling.

What is FixedBackOffPolicy?

I see FixedBackOffPolicy as an implementation of BackOffPolicy that pauses for a fixed period of time before continuing and ExponentialBackOffPolicy as an implementation of BackOffPolicy that increases the back off period for each retry attempt in a given set.


2 Answers

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.

like image 67
VelNaga Avatar answered Sep 22 '22 10:09

VelNaga


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.

like image 40
isaolmez Avatar answered Sep 22 '22 10:09

isaolmez