java.lang.Object
com.broadleafcommerce.dataexchange.domain.util.Retryable<X>
Type Parameters:
X -
All Implemented Interfaces:
Runnable, Callable<X>, Supplier<X>

public class Retryable<X> extends Object implements Callable<X>, Supplier<X>, Runnable
Implementation of Callable, Runnable, and Supplier interfaces that wraps another delegate Callable in retry logic. This is often used to schedule an asynchronous task. For example, Executor.execute(Runnable) or AsyncTaskExecutor.submit(Callable) or CompletableFuture.supplyAsync(Supplier, Executor). However, this can just as easily be used synchronously to ensure that retry semantics are applied to a given execution.
Author:
Kelly Tisdell (ktisdell)
  • Constructor Details

    • Retryable

      public Retryable(@NonNull Callable<X> delegate, @NonNull org.springframework.retry.support.RetryTemplate retryTemplate, @NonNull Consumer<Exception> exceptionConsumer)
      Constructor for Retryable. Consider using the Retryable.RetryableBuilder: builder().
      Parameters:
      delegate - the Callable function implementation to execute
      retryTemplate - the RetryTemplate to use for retry semantics
      exceptionConsumer - the Consumer function implementation to receive any exceptions
  • Method Details

    • defaultRetryPredicate

      @NonNull public static Predicate<Throwable> defaultRetryPredicate(@Nullable Predicate<Throwable> retryPredicate)
      Takes the provided Predicate and adds default logic. By default, the following throwables will not be retried: Also, a number of WebClientResponseException will not be retried, except for the following:
      • WebClientResponseException.InternalServerError
      • WebClientResponseException.ServiceUnavailable
      • WebClientResponseException.BadGateway
      • WebClientResponseException.GatewayTimeout
      • WebClientResponseException.TooManyRequests
      • WebClientResponseException.Conflict
      If the provided Predicate is null it will be treated as returning true by default.
      Parameters:
      retryPredicate - indicating if a Throwable should trigger a retry (maybe null)
      Returns:
      predicate indicating if a Throwable should trigger a retry (never null)
      See Also:
      • org.springframework.retry.support.RetryTemplateBuilder#retryOn(Predicate)
    • builder

      public static <X> Retryable.RetryableBuilder<X> builder()
    • call

      public X call() throws Exception
      Executes the Callable delegate in the context of retry semantics. This method will throw any exceptions after the retry logic has been tried and the exception exceptionConsumer has received the exception. Note that the exceptionConsumer may re-throw the exception as a RuntimeException. This is exactly the same as the get() method except that it will throw an exception if an exception is caught after retry logic. The exceptionConsumer might also throw a RuntimeException, in which case that will be the exception thrown out of this method.
      Specified by:
      call in interface Callable<X>
      Returns:
      the result of Callable.call() with retry semantics in case of an error
      Throws:
      Exception - if an exception is thrown
    • get

      public X get()
      Executes the Callable delegate in the context of retry semantics. This method will not normally throw any exceptions after the retry logic has been tried and the exception exceptionConsumer has received the exception. This is exactly the same as the call() method except that it will NOT normally throw an exception. Rather if an exception is encountered after retries, then this will call the exceptionConsumer and return null. Note that the exceptionConsumer may re-throw the exception as a RuntimeException. So, while this method will catch the exception and pass it to the exceptionConsumer, it will not, by default, throw that exception. But the exceptionConsumer might, depending on its implementation.
      Specified by:
      get in interface Supplier<X>
      Returns:
      the result of the delegate Callable.call() with retry semantics in case of an error
    • run

      public void run()
      Executes the Callable delegate in the context of retry semantics. This method will not throw any exceptions after the retry logic has been tried and the exception exceptionConsumer has received the exception. This is exactly the same as the call() and get() methods except that it will NOT return a value and will NOT throw an exception. Rather if an exception is encountered after retries, then this will call the exceptionConsumer and complete normally. Note that the exceptionConsumer may re-throw the exception as a RuntimeException. So, while this method will catch the exception and pass it to the exceptionConsumer, it will not, by default, throw that exception. But the exceptionConsumer might, depending on its implementation.
      Specified by:
      run in interface Runnable