Reading maxAttempts from spring @Retryable from application.properties file

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

The maxAttempts argument in the @Retryableannotation is hard-coded. Can I read this value from a application.propertiesfile?

sort of

@Retryable(value = Exception.class, maxAttempts = "${MAX_ATTEMPTS}")
+2
source share
2 answers

No; this cannot be set via a property when using annotation.

You can connect the RetryOperationsInterceptorbean 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.testis the method to which you want to apply repeats.

+1
source

Yes, you can use maxAttemptsExpression:

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

@EnableRetry , .

0

All Articles