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

marcterenzi :

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))
VelNaga :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How set up retry config fetching in spring boot?

Spring boot config client properties

How to assign annotation value from application.properties in Spring Boot?

Spring Boot - inject map from properties file

Inject value from properties in Spring Boot

Inject custom properties folder in Spring Boot Application

Spring Boot: How to inject a Repository

How to inject a Map using the @Value Spring Annotation?

Spring boot - how to load credentials from aws secret manger in inject properties into spring?

Spring Retry Framework with Annotation not Working

Spring boot, declare Service and inject class without annotation

Spring inject xml values to properties-config.xml then to a bean

How to inject array in properties file using Spring

How to configure delay time in Spring-retry (Spring Boot)

How to retry in the new HTTP interface in Spring 6 and Spring Boot 3

Precedence of Spring boot profile specific and spring.config.import properties

How to override spring boot properties?

Spring Boot: how to configure a set of implementations to inject?

How to inject custom list of beans in spring boot

spring boot : How to set spring properties dynamically

Inject with Spring Boot depending on properties from application.yml

Inject List of Custom Object to map using spring boot configuration properties

Spring inject without autowire annotation

How to work with annotation based properties in Spring

Where is hided <context:annotation-config/> in spring boot?

ActiveMQ How to lookup external context.xml from application.properties or @Annotation [Spring Boot 1.5.8]

@Value annotation unable to read properties file in spring boot camel application

Spring boot. How to Create TaskExecutor with Annotation?

How to create a custom annotation in spring boot?