Skip to content

Instantly share code, notes, and snippets.

@adelinor
Created April 8, 2018 19:58
Show Gist options
  • Save adelinor/fa80fc3287bb388a6d55631244f8697f to your computer and use it in GitHub Desktop.
Save adelinor/fa80fc3287bb388a6d55631244f8697f to your computer and use it in GitHub Desktop.
/**
* Implements a common retry policy for async operations
* @param fn Async method to retry if necessary
* @return Result of the function invoked
*/
private <T> ListenableFuture<T> invoke(Function<SharePointFacade, ListenableFuture<T>> fn) {
// Define an executor with Retry
RetryExecutor retryExecutor
= new AsyncRetryExecutor(this.scheduledExecutor)
.retryOn(AuthenticationExpiredException.class,
ConnectionRefusedException.class,
ConnectionFailedException.class,
UncategorizedException.class)
.withExponentialBackoff(500, 1.5)
.withUniformJitter()
.withMaxRetries(5);
return retryExecutor.getFutureWithRetry(ctx -> {
// calls the async method and will perform some extra
// actions based on the retry context: for instance re-authenticate
// if the failure was due to a connection expired exception
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment