Class Retry


  • public final class Retry
    extends java.lang.Object
    Retry helper: retries given Callable as long as it returns null (interpreted as "no answer yet") or given time passes. This helper implements similar semantics regarding caller threads as Lock.tryLock(long, TimeUnit) method does: blocks the caller thread until operation return non-null value within the given waiting time and the current thread has not been interrupted.
    Since:
    1.7.3
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Retry.DoNotRetry
      Marker interface to apply onto exceptions to make them "never retried" when thrown.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <R> R retry​(int attempts, long sleepMillis, java.util.concurrent.Callable<R> operation, java.util.function.Predicate<java.lang.Exception> retryPredicate, R defaultResult)
      Retries attempting max given times the passed in operation, sleeping given sleepMills between retries.
      static <R> R retry​(long time, java.util.concurrent.TimeUnit unit, long sleepMillis, java.util.concurrent.Callable<R> operation, java.util.function.Predicate<java.lang.Exception> retryPredicate, R defaultResult)
      Retries for given amount of time (time, unit) the passed in operation, sleeping given sleepMills between retries.
      • Methods inherited from class java.lang.Object

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

      • retry

        public static <R> R retry​(long time,
                                  java.util.concurrent.TimeUnit unit,
                                  long sleepMillis,
                                  java.util.concurrent.Callable<R> operation,
                                  java.util.function.Predicate<java.lang.Exception> retryPredicate,
                                  R defaultResult)
                           throws java.lang.InterruptedException
        Retries for given amount of time (time, unit) the passed in operation, sleeping given sleepMills between retries. In case operation returns null, it is assumed "is not done yet" state, so retry will happen (if time barrier allows). If time barrier passes, and still null ("is not done yet") is returned from operation, the defaultResult is returned.
        Throws:
        java.lang.InterruptedException
      • retry

        public static <R> R retry​(int attempts,
                                  long sleepMillis,
                                  java.util.concurrent.Callable<R> operation,
                                  java.util.function.Predicate<java.lang.Exception> retryPredicate,
                                  R defaultResult)
                           throws java.lang.InterruptedException
        Retries attempting max given times the passed in operation, sleeping given sleepMills between retries. In case operation returns null, it is assumed "is not done yet" state, so retry will happen (if attempt count allows). If all attempts used, and still null ("is not done yet") is returned from operation, the defaultResult is returned.

        Just to clear things up: 5 attempts is really 4 retries (once do it and retry 4 times). 0 attempts means "do not even try it", and this method returns without doing anything.

        Throws:
        java.lang.InterruptedException