Search code examples
restconfigurationstaticannotationsspring-retry

How to pass variable value in @retryable annotation


I am using spring retry mechanism when calling rest API. Using below annotation

 @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 100))

I have a java file using @Configuration which is getting properties from a properties file. Instead of passing a static value, is there a way I can pass value from the above mentioned java file.

unable to use @PropertySource("classpath:retryConfig.properties") as I am getting values from java class and not properties file.

seeing below error when trying to pass an variable

The value for annotation attribute Retryable.maxAttempts must be a constant expression

Solution

  • Use the ...expression variants of the properties; here is an example from one of the test cases...

    @Retryable(exceptionExpression = "#{@exceptionChecker.${retryMethod}(#root)}",
                maxAttemptsExpression = "#{@integerFiveBean}", backoff = @Backoff(delayExpression = "#{${one}}",
                        maxDelayExpression = "#{${five}}", multiplierExpression = "#{${onePointOne}}"))
    public void service3() {
    
    

    where one and five and onePointOne are properties.