Class RetryUtils

java.lang.Object
com.tnsai.utils.RetryUtils

public class RetryUtils extends Object
Utility for retrying operations with exponential backoff.
  • 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 retry
      maxRetries - Maximum number of retry attempts
      initialDelay - 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 retry
      maxRetries - Maximum number of retry attempts
      initialDelay - Initial delay duration
      retryPredicate - 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 a RetryPolicy.
      Parameters:
      operation - Operation to run
      retryPolicy - Policy definition
      operationName - Human-readable name for logging
      Throws:
      Exception