Class MultipleAttemptsExecutor

java.lang.Object
uk.ac.ebi.utils.runcontrol.MultipleAttemptsExecutor
All Implemented Interfaces:
Executor

public class MultipleAttemptsExecutor extends Object implements Executor
An executor to attempt an operation multiple times, knowing it can fail from time to time (e.g. getting data from a REST API).
Author:
brandizi
Date:
9 Oct 2015
  • Constructor Details

    • MultipleAttemptsExecutor

      @SafeVarargs public MultipleAttemptsExecutor(int maxAttempts, long minPauseTimeMs, long maxPauseTimeMs, Class<? extends Exception>... interceptedExceptions)
    • MultipleAttemptsExecutor

      @SafeVarargs public MultipleAttemptsExecutor(Class<? extends Exception>... interceptedExceptions)
  • Method Details

    • execute

      public void execute(Runnable action)
      Overrides Executor.execute(Runnable), by calling #executeChecked(Runnable). and intercepting the thrown checked exception.
      Specified by:
      execute in interface Executor
    • executeChecked

      public void executeChecked(com.machinezoo.noexception.throwing.ThrowingRunnable action) throws Exception
      Tries to run the action and, if it fails with one of getInterceptedExceptions(), re-run it up to getMaxAttempts(). A pause with a random time between getMinPauseTime() and getMaxPauseTime() is inserted between attempts, so that, in case of race conditions between parallel threads, they don't re-attempt the same conflicting operation at the same time. This version is based on the possibility that a checked exception occurs, use execute(Runnable) when you have only unchecked exceptions.
      Throws:
      Exception
    • getMaxAttempts

      public int getMaxAttempts()
      If the operation run by execute(Runnable) fails even after this number of times, the exception it raises is re-thrown to the caller. Default is 3.
    • setMaxAttempts

      public void setMaxAttempts(int maxAttempts)
    • getMinPauseTime

      public long getMinPauseTime()
      After a failed attempt, execute(Runnable) pauses for a random time between this and getMaxPauseTime() ms. This may be useful for those operations that have concurrent access problems (it's a brutal way to cope with them, but might be reasonable sometime). Default is 0. If these two values are both 0, no pause occurs between attempts.
    • setMinPauseTime

      public void setMinPauseTime(long minPauseTimeMs)
    • getMaxPauseTime

      public long getMaxPauseTime()
      See Also:
    • setMaxPauseTime

      public void setMaxPauseTime(long maxPauseTimeMs)
    • getInterceptedExceptions

      public Class<Exception>[] getInterceptedExceptions()
      execute(Runnable) considers only these exceptions (or their subclasses), when evaluating if a failed operation has to be re-attempted. Any other exception is re-thrown to the caller and the operation is not re-attempted.
    • setInterceptedExceptions

      public void setInterceptedExceptions(Class<? extends Exception>[] interceptedExceptions)
    • getAttemptMsgLogLevel

      public uk.org.lidalia.slf4jext.Level getAttemptMsgLogLevel()
      Failed attempts are logged with this logging level. Default is INFO.
    • setAttemptMsgLogLevel

      public void setAttemptMsgLogLevel(uk.org.lidalia.slf4jext.Level attemptMsgLogLevel)