Package com.tnsai.utils
Class RetryUtils
java.lang.Object
com.tnsai.utils.RetryUtils
Utility for retrying operations with exponential backoff.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TexecuteWithRetry(Callable<T> operation, int maxRetries, Duration initialDelay) Retries an operation with exponential backoff.static <T> TexecuteWithRetry(Callable<T> operation, int maxRetries, Duration initialDelay, Predicate<Exception> retryPredicate, String operationName) Retries an operation with exponential backoff.static <T> TexecuteWithRetry(Callable<T> operation, RetryPolicy retryPolicy, String operationName) Retries an operation using aRetryPolicy.
-
Constructor Details
-
RetryUtils
public RetryUtils()
-
-
Method Details
-
executeWithRetry
public static <T> T executeWithRetry(Callable<T> operation, int maxRetries, Duration initialDelay) throws Exception Retries an operation with exponential backoff. Uses default retry predicate (retries on any exception) and default operation name.- Type Parameters:
T- Return type of the operation- Parameters:
operation- The operation to retrymaxRetries- Maximum number of retry attemptsinitialDelay- Initial delay duration- Returns:
- The result of the operation
- Throws:
Exception- If retries are exhausted
-
executeWithRetry
public static <T> T executeWithRetry(Callable<T> operation, int maxRetries, Duration initialDelay, Predicate<Exception> retryPredicate, String operationName) throws Exception Retries an operation with exponential backoff.- Type Parameters:
T- Return type of the operation- Parameters:
operation- The operation to retrymaxRetries- Maximum number of retry attemptsinitialDelay- Initial delay durationretryPredicate- Condition to determine if retry should happen based on exception- Returns:
- The result of the operation
- Throws:
Exception- If retries are exhausted or a non-retriable exception occurs
-
executeWithRetry
public static <T> T executeWithRetry(Callable<T> operation, RetryPolicy retryPolicy, String operationName) throws Exception Retries an operation using aRetryPolicy.- Parameters:
operation- Operation to runretryPolicy- Policy definitionoperationName- Human-readable name for logging- Throws:
Exception
-