Failsafe - do some action when retries exceeded and try one more time

3k Views Asked by At

I have a use case, where I need to call UPDATE/DELETE Rest endpoint, which might return exception about entity being locked for editing. I want to wait some time for entity being unlocked, otherwise call force unlock endpoint and call one more time my original request. Is this possible with failsafe somehow?

What I tried so far:

lockRetryPolicy = new RetryPolicy()
            .retryOn(LockException.class)
            .withBackoff(1, 8, TimeUnit.MINUTES)
            .withMaxDuration(30, TimeUnit.MINUTES);

and

private <R, T> R lockRetryExecution(String elementUrl, Callable<R> task) {
    return Failsafe.with(lockRetryPolicy)
            .onRetriesExceeded((o, throwable) -> {
                forceUnlock(elementUrl);
                task.call();
            })
            .withFallback((o, throwable) -> o)
            .get(task);
}

However, with this approach, I have to do last "retry" myself and can't get result of this action.

1

There are 1 best solutions below

0
On

Looks like this works.

private <R, T> R lockRetryExecution(String elementUrl, Callable<R> task) {
    return Failsafe.with(lockRetryPolicy)
        .onRetriesExceeded((o, throwable) -> forceUnlock(elementUrl))
        .withFallback(task)
        .get(task);
}