Class RetryUtils

java.lang.Object
dev.langchain4j.internal.RetryUtils

public final class RetryUtils extends Object
Utility class for retrying actions.
  • Field Details

  • Method Details

    • retryPolicyBuilder

      public static RetryUtils.RetryPolicy.Builder retryPolicyBuilder()
      This method returns a RetryPolicy.Builder.
      Returns:
      A RetryPolicy.Builder.
    • withRetry

      public static <T> T withRetry(Callable<T> action)
      This method attempts to execute a given action up to 3 times with an exponential backoff. If the action fails on all attempts, it throws a RuntimeException.
      Type Parameters:
      T - The type of the result of the action.
      Parameters:
      action - The action to be executed.
      Returns:
      The result of the action if it is successful.
      Throws:
      RuntimeException - if the action fails on all attempts.
    • withRetry

      public static <T> T withRetry(Callable<T> action, int maxRetries)
      This method attempts to execute a given action up to a specified number of times with an exponential backoff. If the action fails on all attempts, it throws a RuntimeException.
      Type Parameters:
      T - The type of the result of the action.
      Parameters:
      action - The action to be executed.
      maxRetries - The maximum number of retries. The action can be executed up to maxRetries + 1 times.
      Returns:
      The result of the action if it is successful.
      Throws:
      RuntimeException - if the action fails on all attempts.
    • withRetry

      public static void withRetry(Runnable action, int maxRetries)
      This method attempts to execute a given action up to a specified number of times with an exponential backoff. If the action fails on all attempts, it throws a RuntimeException.
      Parameters:
      action - The action to be executed.
      maxRetries - The maximum number of retries. The action can be executed up to maxRetries + 1 times.
      Throws:
      RuntimeException - if the action fails on all attempts.
    • withRetryMappingExceptions

      public static <T> T withRetryMappingExceptions(Callable<T> action)
      This method attempts to execute a given action up to 3 times with an exponential backoff. If the action fails, the Exception causing the failure will be mapped with the default ExceptionMapper.
      Type Parameters:
      T - The type of the result of the action.
      Parameters:
      action - The action to be executed.
      Returns:
      The result of the action if it is successful.
      Throws:
      RuntimeException - if the action fails on all attempts.
    • withRetryMappingExceptions

      public static <T> T withRetryMappingExceptions(Callable<T> action, int maxRetries)
      This method attempts to execute a given action up to a specified number of times with an exponential backoff. If the action fails, the Exception causing the failure will be mapped with the default ExceptionMapper.
      Type Parameters:
      T - The type of the result of the action.
      Parameters:
      action - The action to be executed.
      maxRetries - The maximum number of retries. The action can be executed up to maxRetries + 1 times.
      Returns:
      The result of the action if it is successful.
      Throws:
      RuntimeException - if the action fails on all attempts.
    • withRetryMappingExceptions

      public static <T> T withRetryMappingExceptions(Callable<T> action, int maxRetries, ExceptionMapper exceptionMapper)
      This method attempts to execute a given action up to a specified number of times with an exponential backoff. If the action fails, the Exception causing the failure will be mapped with the provided ExceptionMapper.
      Type Parameters:
      T - The type of the result of the action.
      Parameters:
      action - The action to be executed.
      maxRetries - The maximum number of retries. The action can be executed up to maxRetries + 1 times.
      exceptionMapper - The ExceptionMapper used to translate the exception that caused the failure of the action invocation.
      Returns:
      The result of the action if it is successful.
      Throws:
      RuntimeException - if the action fails on all attempts.
    • withRetryMappingExceptionsAsync

      public static <T> CompletableFuture<T> withRetryMappingExceptionsAsync(Supplier<CompletableFuture<T>> action, int maxRetries)
      Non-blocking counterpart of withRetryMappingExceptions(Callable, int): retries a CompletableFuture-returning action with exponential backoff + jitter, mapping failures with the default ExceptionMapper, without ever blocking a thread during the backoff. See RetryUtils.RetryPolicy.withRetryAsync(Supplier, int, ExceptionMapper).
      Since:
      1.20.0
    • withRetryMappingExceptionsAsync

      public static <T> CompletableFuture<T> withRetryMappingExceptionsAsync(Supplier<CompletableFuture<T>> action, int maxRetries, ExceptionMapper exceptionMapper)
      Since:
      1.20.0