3

Using Spring Retry, I have a retry going on in 1 thread.

In some circumstances, I would like to be able to cancel the retry before it has reached the configured number of retries.

What is the best way to achieve this with this library?

I have tried wrapping the lambda passed into execute so I can check whether or not the retry should be cancelled like this:

public <V, E extends Throwable> V execute(ThrowingSupplier<V, E> action) throws E {

    return retryTemplate.execute(context -> {
        if (shouldCancel()) {
            //Do something to cancel the retry here
        } else {
            return action.get()
        }
    });

}

However, I feel like I am working against, rather than with, the framework by wrapping the retry template in this way.

Rupert Madden-Abbott
  • 12,899
  • 14
  • 59
  • 74
  • 1
    Did you consider using your own `RetryPolicy` and check your indicator when to stop the retry there? https://docs.spring.io/spring-retry/docs/api/current/org/springframework/retry/RetryPolicy.html – Andreas Feb 12 '22 at 09:35

1 Answers1

0

Simply call:

context.setExhaustedOnly()

However, it seems the RetryTemplate itself cannot be externally interrupted, so if you need to interrupt while it is in a sleeping state, you have to call the Thread.interrupt() on the thread it is waiting on. This might be a little tricky if a thread pool is being used...

deje
  • 81
  • 4