Class RetryUtils
java.lang.Object
dev.langchain4j.internal.RetryUtils
Utility class for retrying actions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThis class encapsulates a retry policy. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final RetryUtils.RetryPolicyDefault retry policy used bywithRetry(Callable). -
Method Summary
Modifier and TypeMethodDescriptionThis method returns a RetryPolicy.Builder.static voidThis method attempts to execute a given action up to a specified number of times with an exponential backoff.static <T> TThis method attempts to execute a given action up to 3 times with an exponential backoff.static <T> TThis method attempts to execute a given action up to a specified number of times with an exponential backoff.static <T> TwithRetryMappingExceptions(Callable<T> action) This method attempts to execute a given action up to 3 times with an exponential backoff.static <T> TwithRetryMappingExceptions(Callable<T> action, int maxRetries) This method attempts to execute a given action up to a specified number of times with an exponential backoff.static <T> TwithRetryMappingExceptions(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.static <T> CompletableFuture<T> withRetryMappingExceptionsAsync(Supplier<CompletableFuture<T>> action, int maxRetries) Non-blocking counterpart ofwithRetryMappingExceptions(Callable, int): retries aCompletableFuture-returning action with exponential backoff + jitter, mapping failures with the defaultExceptionMapper, without ever blocking a thread during the backoff.static <T> CompletableFuture<T> withRetryMappingExceptionsAsync(Supplier<CompletableFuture<T>> action, int maxRetries, ExceptionMapper exceptionMapper) Non-blocking counterpart ofwithRetryMappingExceptions(Callable, int, ExceptionMapper).
-
Field Details
-
DEFAULT_RETRY_POLICY
Default retry policy used bywithRetry(Callable).
-
-
Method Details
-
retryPolicyBuilder
This method returns a RetryPolicy.Builder.- Returns:
- A RetryPolicy.Builder.
-
withRetry
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
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 tomaxRetries + 1times.- Returns:
- The result of the action if it is successful.
- Throws:
RuntimeException- if the action fails on all attempts.
-
withRetry
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 tomaxRetries + 1times.- Throws:
RuntimeException- if the action fails on all attempts.
-
withRetryMappingExceptions
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 defaultExceptionMapper.- 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
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 defaultExceptionMapper.- 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 tomaxRetries + 1times.- 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 providedExceptionMapper.- 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 tomaxRetries + 1times.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 ofwithRetryMappingExceptions(Callable, int): retries aCompletableFuture-returning action with exponential backoff + jitter, mapping failures with the defaultExceptionMapper, without ever blocking a thread during the backoff. SeeRetryUtils.RetryPolicy.withRetryAsync(Supplier, int, ExceptionMapper).- Since:
- 1.20.0
-
withRetryMappingExceptionsAsync
public static <T> CompletableFuture<T> withRetryMappingExceptionsAsync(Supplier<CompletableFuture<T>> action, int maxRetries, ExceptionMapper exceptionMapper) Non-blocking counterpart ofwithRetryMappingExceptions(Callable, int, ExceptionMapper).- Since:
- 1.20.0
-