Home > Software design >  How parametrize delay @BackOff spring boot, @Retry
How parametrize delay @BackOff spring boot, @Retry

Time:03-23

I can't use my propertie for asign value to delay:

 @Value("${delayReintentos}")
 private long delay;

@Retryable(value = { SQLException.class }, maxAttempts = 3, backoff = @Backoff(delay = delay))
    public String simpleRetry() throws SQLException {
        counter  ;
        LOGGER.info("Billing Service Failed "  counter);
        throw new SQLException();

    }

Java11, Spring boot

CodePudding user response:

Use delayExpression instead

/**
 * An expression evaluating to the canonical backoff period. Used as an initial value
 * in the exponential case, and as a minimum value in the uniform case. Overrides
 * {@link #delay()}.
 * @return the initial or canonical backoff period in milliseconds.
 * @since 1.2
 */
String delayExpression() default "";

See the readme: https://github.com/spring-projects/spring-retry#readme

You can use SpEL or property placeholders

@Backoff(delayExpression = "${my.delay}",
                        maxDelayExpression = "@integerFiveBean", multiplierExpression = "${onePointOne}")

CodePudding user response:

Try this by wrapping the property name in braces.

@Retryable(value = SQLException.class, maxAttempts = 3, backoff = @Backoff(delayExpression = "${delay}")
  • Related