3
@Retryable(value = Exception.class, maxAttempts = 3)
public Boolean sendMessageService(Request request){
   ...
}

maxAttempts argument in @Retryable annotation is hard coded. Can i read that value from application.properties file?

something like

@Retryable(value = Exception.class, maxAttempts = "${MAX_ATTEMPTS}")
tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
Sheik Mubaraq
  • 31
  • 1
  • 2

2 Answers2

2

Yes, you can by using maxAttemptsExpression:

@Retryable(value = {Exception.class}, maxAttemptsExpression = "#{${maxAttempts}}")

Add @EnableRetry above your class if you have not already done so.

Dchris
  • 2,867
  • 10
  • 42
  • 73
1

No; it's not possible to set via a property when using the annotation.

You can wire up the RetryOperationsInterceptor bean manually and apply it to your method using Spring AOP...

EDIT

<bean id="retryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
    <property name="retryOperations">
        <bean class="org.springframework.retry.support.RetryTemplate">
            <property name="retryPolicy">
                <bean class="org.springframework.retry.policy.SimpleRetryPolicy">
                    <property name="maxAttempts" value="${max.attempts}" />
                </bean>
            </property>
            <property name="backOffPolicy">
                <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
                    <property name="initialInterval" value="${delay}" />
                    <property name="multiplier" value="${multiplier}" />
                </bean>
            </property>
        </bean>
    </property>
</bean>

<aop:config>
    <aop:pointcut id="retries"
        expression="execution(* org..EchoService.test(..))" />
    <aop:advisor pointcut-ref="retries" advice-ref="retryAdvice"
        order="-1" />
</aop:config>

Where EchoService.test is the method you want to apply retries to.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179