Package com.norconex.commons.lang.exec
Class Retrier
- java.lang.Object
-
- com.norconex.commons.lang.exec.Retrier
-
public class Retrier extends Object
This class is responsible for executingIRetriableinstances. Upon reaching the maximum number of retries allowed, it will throw aRetriableException, wrapping the last exceptions encountered, if any, to a configurable maximum.- Since:
- 1.13.0
- Author:
- Pascal Essiembre
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_MAX_CAUSES_KEPTDefault maximum number of exception causes kept.static intDEFAULT_MAX_RETRIESDefault maximum number of retries.static longDEFAULT_RETRY_DELAYDefault wait time (milliseconds) before making a new attempt.
-
Constructor Summary
Constructors Constructor Description Retrier()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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object other)<T> Texecute(IRetriable<T> retriable)Runs theIRetriableinstance.IExceptionFiltergetExceptionFilter()Sets an exception filter that limits the exceptions eligible for retry.intgetMaxCauses()Gets the maximum number of exception causes to keep when all attempts were made and aRetriableExceptionis thrown.intgetMaxRetries()Gets the maximum number of retries (the initial run does not count as a retry).longgetRetryDelay()Gets the delay in milliseconds before attempting to execute again.inthashCode()RetriersetExceptionFilter(IExceptionFilter exceptionFilter)Sets an exception filter that limits the exceptions eligible for retry.RetriersetMaxCauses(int maxCauses)Sets the maximum number of exception causes to keep when all attempts were made and aRetriableExceptionis thrown.RetriersetMaxRetries(int maxRetries)Sets the maximum number of retries (the initial run does not count as a retry).RetriersetRetryDelay(long retryDelay)Sets the delay in milliseconds before attempting to execute again.StringtoString()
-
-
-
Field Detail
-
DEFAULT_MAX_RETRIES
public static final int DEFAULT_MAX_RETRIES
Default maximum number of retries.- See Also:
- Constant Field Values
-
DEFAULT_RETRY_DELAY
public static final long DEFAULT_RETRY_DELAY
Default wait time (milliseconds) before making a new attempt.- See Also:
- Constant Field Values
-
DEFAULT_MAX_CAUSES_KEPT
public static final int DEFAULT_MAX_CAUSES_KEPT
Default maximum number of exception causes kept.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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
public 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). Uses the default maximum retries and default retry delay.- Parameters:
exceptionFilter- exception filter
-
Retrier
public 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).- Parameters:
exceptionFilter- exception filtermaxRetries- maximum number of retries
-
-
Method Detail
-
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
public Retrier setMaxRetries(int maxRetries)
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
public Retrier setRetryDelay(long retryDelay)
Sets the delay in milliseconds before attempting to execute again.- Parameters:
retryDelay- delay in milliseconds- Returns:
- this instance
-
getExceptionFilter
public IExceptionFilter getExceptionFilter()
Sets an exception filter that limits the exceptions eligible for retry.- Returns:
- exception filter
-
setExceptionFilter
public Retrier setExceptionFilter(IExceptionFilter exceptionFilter)
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
public Retrier setMaxCauses(int maxCauses)
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
public <T> T execute(IRetriable<T> retriable) throws RetriableException
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.
-
-