Package com.helixframework.retry
Class Retry
java.lang.Object
com.helixframework.retry.Retry
Wrapper function that retries, with exponential backoff and jitter, in the event of an exception.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final LongDefault number of milliseconds to initially wait for the retry backoff.static final intDefault number of retries attempted by this function before throwingRetriesExhaustedException. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Texecute(RetryableFunction<T> fn) Executes the wrapped function with default maximum number of retries in the event of an exception.static <T> Texecute(RetryableFunction<T> fn, int maxRetries) Executes the wrapped function with the specified maximum number of retries in the event of an exception.static <T> Texecute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff) Executes the wrapped function with the specified maximum number of retries, and an initial backoff, in the event of an exception.static <T> Texecute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Collection<Class<? extends Throwable>> expectedErrors) Executes the wrapped function, terminating immediately if one of the expected errors is encountered, otherwise the function will be retried up to the specified maximum number of retries.static <T> Texecute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Supplier<T> fallback) Executes the wrapped function.static <T> Texecute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Supplier<T> fallback, Collection<Class<? extends Throwable>> expectedErrors) Executes the provided RetryableFunction with the given parameters.static <T> Texecute(RetryableFunction<T> fn, int maxRetries, Collection<Class<? extends Throwable>> expectedErrors) Executes the wrapped function, terminating immediately if one of the expected errors is encountered, otherwise the function will be retried up to the specified maximum number of retries.static <T> Texecute(RetryableFunction<T> fn, int maxRetries, Supplier<T> fallback) Executes the wrapped function.static <T> Texecute(RetryableFunction<T> fn, Collection<Class<? extends Throwable>> expectedErrors) Executes the wrapped function, terminating immediately if one of the expected errors is encountered.static <T> Texecute(RetryableFunction<T> fn, Supplier<T> fallback) Executes the wrapped function.
-
Field Details
-
DEFAULT_MAX_RETRIES
public static final int DEFAULT_MAX_RETRIESDefault number of retries attempted by this function before throwingRetriesExhaustedException.- See Also:
-
DEFAULT_INITIAL_BACKOFF
Default number of milliseconds to initially wait for the retry backoff.
-
-
Method Details
-
execute
Executes the wrapped function with default maximum number of retries in the event of an exception.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to execute- Returns:
- result of the wrapped function
- Throws:
RetriesExhaustedException- if the maximum number of retries has been reached
-
execute
public static <T> T execute(RetryableFunction<T> fn, int maxRetries) throws RetriesExhaustedException Executes the wrapped function with the specified maximum number of retries in the event of an exception.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to executemaxRetries- maximum number of times to retry the wrapped function in the event of an exception- Returns:
- result of the wrapped function
- Throws:
RetriesExhaustedException- if the maximum number of retries has been reached
-
execute
public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff) throws RetriesExhaustedException Executes the wrapped function with the specified maximum number of retries, and an initial backoff, in the event of an exception.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to executemaxRetries- maximum number of times to retry the wrapped function in the event of an exceptioninitialBackoff- number of milliseconds to initially wait for backoff- Returns:
- result of the wrapped function
- Throws:
RetriesExhaustedException- if the maximum number of retries has been reached
-
execute
public static <T> T execute(RetryableFunction<T> fn, Collection<Class<? extends Throwable>> expectedErrors) throws RetriesExhaustedException Executes the wrapped function, terminating immediately if one of the expected errors is encountered.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to executeexpectedErrors- a collection of errors that when encountered will cause the function to fail immediately without exhausting the maximum number of retries- Returns:
- result of the wrapped function
- Throws:
RetriesExhaustedException- if the maximum number of retries has been reached
-
execute
public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Collection<Class<? extends Throwable>> expectedErrors) throws RetriesExhaustedException Executes the wrapped function, terminating immediately if one of the expected errors is encountered, otherwise the function will be retried up to the specified maximum number of retries.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to executemaxRetries- maximum number of times to retry the wrapped function in the event of an exception not specified in the expectedErrorsexpectedErrors- a collection of errors that when encountered will cause the function to fail immediately without exhausting the maximum number of retries- Returns:
- result of the wrapped function
- Throws:
RetriesExhaustedException- if the maximum number of retries has been reached
-
execute
public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Collection<Class<? extends Throwable>> expectedErrors) throws RetriesExhaustedException Executes the wrapped function, terminating immediately if one of the expected errors is encountered, otherwise the function will be retried up to the specified maximum number of retries.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to executemaxRetries- maximum number of times to retry the wrapped function in the event of an exception not specified in the expectedErrorsinitialBackoff- number of milliseconds to initially wait for backoffexpectedErrors- a collection of errors that when encountered will cause the function to fail immediately without exhausting the maximum number of retries- Returns:
- result of the wrapped function
- Throws:
RetriesExhaustedException- if the maximum number of retries has been reachedIllegalArgumentException- if maxRetries is negative or initialBackoff is negative
-
execute
Executes the wrapped function. In the event that all retries are exhausted, executes the fallback function.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to executefallback- fallback Supplier to use if retries are exhausted- Returns:
- result of the wrapped function
-
execute
Executes the wrapped function. In the event that all retries are exhausted, executes the fallback function.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to executemaxRetries- the maximum number of times to retry the RetryableFunction in the event of an exceptionfallback- fallback Supplier to use if retries are exhausted- Returns:
- result of the wrapped function
- Throws:
IllegalArgumentException- if maxRetries is negative or fallback is null
-
execute
public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Supplier<T> fallback) Executes the wrapped function. In the event that all retries are exhausted, executes the fallback function.- Type Parameters:
T- return type of the wrapped function- Parameters:
fn- wrapped function to executemaxRetries- the maximum number of times to retry the RetryableFunction in the event of an exceptioninitialBackoff- number of milliseconds to initially wait for backofffallback- fallback Supplier to use if retries are exhausted- Returns:
- result of the wrapped function
- Throws:
IllegalArgumentException- if maxRetries is negative, initialBackoff is negative, or fallback is null
-
execute
public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Supplier<T> fallback, Collection<Class<? extends Throwable>> expectedErrors) Executes the provided RetryableFunction with the given parameters.- Type Parameters:
T- the return type of the wrapped function- Parameters:
fn- the RetryableFunction to executemaxRetries- the maximum number of times to retry the wrapped function in the event of an exceptioninitialBackoff- number of milliseconds to initially wait for backofffallback- the fallback Supplier to use if retries are exhaustedexpectedErrors- a collection of errors that will cause the function to fail immediately without exhausting the maximum number of retries- Returns:
- the result of the wrapped function, or the result of the fallback Supplier if retries are exhausted
- Throws:
IllegalArgumentException- if maxRetries is negative, initialBackoff is negative, or fallback is null
-