Dmirii Berrington
Dmirii Berrington

Reputation: 89

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?

Upvotes: 2

Views: 3996

Answers (3)

NickNgn
NickNgn

Reputation: 340

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(...);
    }
}

Upvotes: 1

Nikolai  Shevchenko
Nikolai Shevchenko

Reputation: 7521

@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...)

Upvotes: 3

noob
noob

Reputation: 399

remove @Retryable, you simply don't want what code dose then remove it

Upvotes: 0

Related Questions