Package uk.ac.ebi.utils.runcontrol
Class MultipleAttemptsExecutor
java.lang.Object
uk.ac.ebi.utils.runcontrol.MultipleAttemptsExecutor
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionMultipleAttemptsExecutor(int maxAttempts, long minPauseTimeMs, long maxPauseTimeMs, Class<? extends Exception>... interceptedExceptions) MultipleAttemptsExecutor(Class<? extends Exception>... interceptedExceptions) -
Method Summary
Modifier and TypeMethodDescriptionvoidOverridesExecutor.execute(Runnable), by calling.invalid reference
#executeChecked(Runnable)voidexecuteChecked(com.machinezoo.noexception.throwing.ThrowingRunnable action) Tries to run the action and, if it fails with one ofgetInterceptedExceptions(), re-run it up togetMaxAttempts().uk.org.lidalia.slf4jext.LevelFailed attempts are logged with this logging level.execute(Runnable)considers only these exceptions (or their subclasses), when evaluating if a failed operation has to be re-attempted.intIf the operation run byexecute(Runnable)fails even after this number of times, the exception it raises is re-thrown to the caller.longlongAfter a failed attempt,execute(Runnable)pauses for a random time between this andgetMaxPauseTime()ms.voidsetAttemptMsgLogLevel(uk.org.lidalia.slf4jext.Level attemptMsgLogLevel) voidsetInterceptedExceptions(Class<? extends Exception>[] interceptedExceptions) voidsetMaxAttempts(int maxAttempts) voidsetMaxPauseTime(long maxPauseTimeMs) voidsetMinPauseTime(long minPauseTimeMs)
-
Constructor Details
-
MultipleAttemptsExecutor
@SafeVarargs public MultipleAttemptsExecutor(int maxAttempts, long minPauseTimeMs, long maxPauseTimeMs, Class<? extends Exception>... interceptedExceptions) -
MultipleAttemptsExecutor
-
-
Method Details
-
execute
OverridesExecutor.execute(Runnable), by calling. and intercepting the thrown checked exception.invalid reference
#executeChecked(Runnable) -
executeChecked
public void executeChecked(com.machinezoo.noexception.throwing.ThrowingRunnable action) throws Exception Tries to run the action and, if it fails with one ofgetInterceptedExceptions(), re-run it up togetMaxAttempts(). A pause with a random time betweengetMinPauseTime()andgetMaxPauseTime()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, useexecute(Runnable)when you have only unchecked exceptions.- Throws:
Exception
-
getMaxAttempts
public int getMaxAttempts()If the operation run byexecute(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 andgetMaxPauseTime()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
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
-
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)
-