Class RetrySupplier<T>

  • Type Parameters:
    T - The type of the result the Supplier yields upon application
    All Implemented Interfaces:
    java.util.function.Supplier<T>

    public class RetrySupplier<T>
    extends java.lang.Object
    implements java.util.function.Supplier<T>
    A Supplier which wraps the apply method of another Supplier and attempts it up to a fixed number of times. This class can be used when a task is known to be prone to occasional failure and other workarounds are not known.
    See Also:
    for a Function implementation, for some factory methods, for some predefined handlers
    • Constructor Summary

      Constructors 
      Constructor Description
      RetrySupplier​(java.util.function.Supplier<T> supplier, int tries)
      An instance that does nothing before retrying and ignores exceptions that occur.
      RetrySupplier​(java.util.function.Supplier<T> supplier, int tries, ExceptionHandler handler)
      An instance that does nothing before retrying.
      RetrySupplier​(java.util.function.Supplier<T> supplier, int tries, ExceptionHandler handler, java.lang.Runnable beforeRetry)
      Constructor for RetrySupplier.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T get()
      Attempt to get a result tries number of times.
      • Methods inherited from class java.lang.Object

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

      • supplier

        private final java.util.function.Supplier<T> supplier
      • tries

        private final int tries
      • beforeRetry

        private final java.lang.Runnable beforeRetry
    • Constructor Detail

      • RetrySupplier

        public RetrySupplier​(java.util.function.Supplier<T> supplier,
                             int tries)
        An instance that does nothing before retrying and ignores exceptions that occur.
        Parameters:
        supplier - which fetches the result, must not be null
        tries - the number of times to attempt to get a result, must be positive
      • RetrySupplier

        public RetrySupplier​(java.util.function.Supplier<T> supplier,
                             int tries,
                             ExceptionHandler handler)
        An instance that does nothing before retrying.
        Parameters:
        supplier - which fetches the result, must not be null
        tries - the number of times to attempt to get a result, must be positive
        handler - reacts to exceptions thrown by the supplier, must not be null
      • RetrySupplier

        public RetrySupplier​(java.util.function.Supplier<T> supplier,
                             int tries,
                             ExceptionHandler handler,
                             java.lang.Runnable beforeRetry)

        Constructor for RetrySupplier.

        Parameters:
        supplier - which fetches the result, must not be null
        tries - the number of times to attempt to get a result, must be positive
        handler - reacts to exceptions thrown by the supplier, must not be null
        beforeRetry - a task which will run at the end of any
    • Method Detail

      • get

        public T get()
        Attempt to get a result tries number of times. Any exceptions thrown will be ignored until the number of attempts is reached. If the number of attempts is reached without a successful result, the most recent exception to be thrown will be rethrown.
        Specified by:
        get in interface java.util.function.Supplier<T>