'How to disable retry in spring boot without removing it?
i have 2 methods in class:
@Retryable(maxAttemptsExpression="${retry.maxAttempts}", backoff=@Backoff(delayExpression = "${retry.delay}", multiplierExpression = "${retry.multiplier}"))
public void foo() {
/**some body**/
}
@Recover
public void fooRecover() {
/**some body**/
}
In some cases i need to disable retrying, but maxAttempts can't be equals zero, so i can't simply do it. So how to correctly disable retrying in some cases?
Solution 1:[1]
@Retryable annotation has a property exceptionExpression where you can specify SpEL expression that evaluates to true or false.
/**
* Specify an expression to be evaluated after the
* {@code SimpleRetryPolicy.canRetry()} returns true - can be used to conditionally
* suppress the retry. Only invoked after an exception is thrown. The root object for
* the evaluation is the last {@code Throwable}. Other beans in the context can be
* referenced. For example: <pre class=code>
* {@code "message.contains('you can retry this')"}.
* </pre> and <pre class=code>
* {@code "@someBean.shouldRetry(#root)"}.
* </pre>
* @return the expression.
* @since 1.2
*/
String exceptionExpression() default "";
So if you want to disable retry you can inject boolean string parameter with value "false"
@Retryable(exceptionExpression = "${retry.shouldRetry}", ...other stuff...)
Solution 2:[2]
I think the problems are on the conditions. In which case you want to retry and which you want to stop?
So then you can define the value=?Exception.class in the annotation.
For example:
@Retryable(value=MUST_RETRY_EXCEPTION.class)
public void foo() {
if (condition) {
throw new MUST_RETRY_EXCEPTION(...);
}
if (condition2) {
throw new NO_RETRY_EXCEPTION(...);
}
}
Solution 3:[3]
remove @Retryable, you simply don't want what code dose then remove it
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nikolai Shevchenko |
| Solution 2 | Hung Nguyen Duy |
| Solution 3 | Java Developer |
