Class Retry<E extends java.lang.Exception>

  • Type Parameters:
    E - the class of exceptions that may be thrown

    public class Retry<E extends java.lang.Exception>
    extends java.lang.Object
    Retries an action until it succeeds, or has retried too often and failed. By default the action will be run up to 5 times. The action is deemed successful if it runs to completion without throwing an exception, and returns true.
    • Exceptions are caught and, if deemed retryable then the action will be re-attempted. By default, any exception is considered retryable.
    • The retry instance can be configured to sleep between retries.
    • The maximum retry count is configurable (5 times by default).
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Retry.Action<E extends java.lang.Exception>
      A runnable action that may throw an exception of type E.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <E extends java.lang.Exception>
      Retry<E>
      action​(Retry.Action<E> action)
      Create a retryable action.
      Retry<E> maximumRetries​(int maximumRetries)
      Configure the maximum number of retries.
      Retry<E> retryOnException​(java.util.function.Predicate<java.lang.Exception> retryOnException)
      Provide a predicate to determine if a thrown exception can be retried.
      boolean run()
      Run the action until it runs successfully, to a maximum number of retries (default: 5).
      Retry<E> sleep​(long duration, java.util.concurrent.TimeUnit unit)
      Set the sleep time between retries.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • action

        public static <E extends java.lang.Exception> Retry<E> action​(Retry.Action<E> action)
        Create a retryable action.
        Type Parameters:
        E - the class of exceptions that may be thrown
        Parameters:
        action - the action to be run
        Returns:
        the instance
      • maximumRetries

        public Retry<E> maximumRetries​(int maximumRetries)
        Configure the maximum number of retries.
        Parameters:
        maximumRetries - the number of retries, must be zero or more
        Returns:
        this Retry instance
      • retryOnException

        public Retry<E> retryOnException​(java.util.function.Predicate<java.lang.Exception> retryOnException)
        Provide a predicate to determine if a thrown exception can be retried.
        Parameters:
        retryOnException - determine if provided exception is retryable.
        Returns:
        the instance for further configuration
      • sleep

        public Retry<E> sleep​(long duration,
                              java.util.concurrent.TimeUnit unit)
        Set the sleep time between retries.
        Parameters:
        duration - the time to sleep
        unit - the unit of time of duration
        Returns:
        the instance for further configuration
      • run

        public boolean run()
                    throws E extends java.lang.Exception
        Run the action until it runs successfully, to a maximum number of retries (default: 5). If an exception occurs then the action will be retried providing the exception is retryable.
        Returns:
        true if the action was run successfully, or false if the action was unable to complete
        Throws:
        E - exception thrown during the action
        E extends java.lang.Exception