Package com.norconex.commons.lang.exec
Class Retrier
java.lang.Object
com.norconex.commons.lang.exec.Retrier
This class is responsible for executing
IRetriable
instances. Upon reaching the maximum number of retries allowed, it
will throw a RetriableException, wrapping the last exceptions
encountered, if any, to a configurable maximum.- Since:
- 1.13.0
- Author:
- Pascal Essiembre
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum number of exception causes kept.static final intDefault maximum number of retries.static final longDefault wait time (milliseconds) before making a new attempt. -
Constructor Summary
ConstructorsConstructorDescriptionRetrier()Creates a new instance with the default maximum retries and default retry delay (no delay).Retrier(int maxRetries) Creates a new instance with the default retry delay (no delay).Retrier(IExceptionFilter exceptionFilter) Creates a new instance which will retry execution only if the exception thrown by an attempt is accepted by theIExceptionFilter(up tomaxRetries).Retrier(IExceptionFilter exceptionFilter, int maxRetries) Creates a new instance which will retry execution only if the exception thrown by an attempt is accepted by theIExceptionFilter(up tomaxRetries). -
Method Summary
Modifier and TypeMethodDescriptionboolean<T> Texecute(IRetriable<T> retriable) Runs theIRetriableinstance.Sets an exception filter that limits the exceptions eligible for retry.intGets the maximum number of exception causes to keep when all attempts were made and aRetriableExceptionis thrown.intGets the maximum number of retries (the initial run does not count as a retry).longGets the delay in milliseconds before attempting to execute again.inthashCode()setExceptionFilter(IExceptionFilter exceptionFilter) Sets an exception filter that limits the exceptions eligible for retry.setMaxCauses(int maxCauses) Sets the maximum number of exception causes to keep when all attempts were made and aRetriableExceptionis thrown.setMaxRetries(int maxRetries) Sets the maximum number of retries (the initial run does not count as a retry).setRetryDelay(long retryDelay) Sets the delay in milliseconds before attempting to execute again.toString()
-
Field Details
-
DEFAULT_MAX_RETRIES
public static final int DEFAULT_MAX_RETRIESDefault maximum number of retries.- See Also:
-
DEFAULT_RETRY_DELAY
public static final long DEFAULT_RETRY_DELAYDefault wait time (milliseconds) before making a new attempt.- See Also:
-
DEFAULT_MAX_CAUSES_KEPT
public static final int DEFAULT_MAX_CAUSES_KEPTDefault maximum number of exception causes kept.- See Also:
-
-
Constructor Details
-
Retrier
public Retrier()Creates a new instance with the default maximum retries and default retry delay (no delay). -
Retrier
public Retrier(int maxRetries) Creates a new instance with the default retry delay (no delay).- Parameters:
maxRetries- maximum number of execution retries
-
Retrier
Creates a new instance which will retry execution only if the exception thrown by an attempt is accepted by theIExceptionFilter(up tomaxRetries). Uses the default maximum retries and default retry delay.- Parameters:
exceptionFilter- exception filter
-
Retrier
Creates a new instance which will retry execution only if the exception thrown by an attempt is accepted by theIExceptionFilter(up tomaxRetries).- Parameters:
exceptionFilter- exception filtermaxRetries- maximum number of retries
-
-
Method Details
-
getMaxRetries
public int getMaxRetries()Gets the maximum number of retries (the initial run does not count as a retry).- Returns:
- maximum number of retries
-
setMaxRetries
Sets the maximum number of retries (the initial run does not count as a retry).- Parameters:
maxRetries- maximum number of retries- Returns:
- this instance
-
getRetryDelay
public long getRetryDelay()Gets the delay in milliseconds before attempting to execute again.- Returns:
- delay in milliseconds
-
setRetryDelay
Sets the delay in milliseconds before attempting to execute again.- Parameters:
retryDelay- delay in milliseconds- Returns:
- this instance
-
getExceptionFilter
Sets an exception filter that limits the exceptions eligible for retry.- Returns:
- exception filter
-
setExceptionFilter
Sets an exception filter that limits the exceptions eligible for retry.- Parameters:
exceptionFilter- exception filter- Returns:
- this instance
-
getMaxCauses
public int getMaxCauses()Gets the maximum number of exception causes to keep when all attempts were made and aRetriableExceptionis thrown.- Returns:
- max number of causes
-
setMaxCauses
Sets the maximum number of exception causes to keep when all attempts were made and aRetriableExceptionis thrown.- Parameters:
maxCauses- max number of causes- Returns:
- this instance
-
execute
Runs theIRetriableinstance. This method is only thread-safe if not modified after being used for the first time, and the exception filter is thread-safe (ornull).- Type Parameters:
T- type of optional return value- Parameters:
retriable- the code to run- Returns:
- execution output if any, or null
- Throws:
RetriableException- wrapper around last exception encountered or exception thrown when max rerun attempts is reached.
-
equals
-
hashCode
public int hashCode() -
toString
-