E
- The type parameter of the Exception
instance signifying
terminal failure of the RetryPolicy
execution.public abstract class RetryPolicy<E extends Exception> extends Object implements Serializable
The retryOn(Exception)
method specifies the exception conditions of
when a retry should occur.
The run(Retryable)
method is the entrypoint for a Retryable
object to be executed.
Constructor and Description |
---|
RetryPolicy(int maxRetries,
double jitter)
Creates a new
RetryPolicy with the specified maxRetries
value. |
Modifier and Type | Method and Description |
---|---|
protected abstract long |
getDelayMs(int attemptNo)
Returns the delay in milliseconds for the specified attempt number.
|
int |
getMaxRetries()
Returns the number of retry attempts allowed by this
RetryPolicy . |
protected void |
onRetry(int attemptNo)
Method that is called before each execution of
Retryable.retry(RetryPolicy,int) . |
protected E |
onRetryFailure(Exception e,
int attemptNo,
long delayMs)
Callback to return the
Exception instance of type <E> to be
thrown in the event of terminal failure of the RetryPolicy
execution. |
protected abstract boolean |
retryOn(Exception e)
Specifies the exception conditions of when a retry should occur.
|
<T> T |
run(Retryable<T,E> retryable)
The entrypoint for a
Retryable object to be executed. |
<T> T |
run(Retryable<T,E> retryable,
long timeout)
The entrypoint for a
Retryable object to be executed. |
public RetryPolicy(int maxRetries, double jitter)
RetryPolicy
with the specified maxRetries
value.maxRetries
- A positive value representing the number of retry
attempts allowed by the RetryPolicy
.jitter
- The factor multiplier to be applied to
getDelayMs(int)
to thereafter be added to the delay for
each retry.IllegalArgumentException
- If maxRetries
or jitter
is
negative.protected abstract boolean retryOn(Exception e)
e
- The exception that occurred during execution of a
Retryable
object.true
if a retry should occur, otherwise false
.protected E onRetryFailure(Exception e, int attemptNo, long delayMs)
Exception
instance of type <E>
to be
thrown in the event of terminal failure of the RetryPolicy
execution.
If this method is not overridden, or if it returns null
, a
RetryFailureException
will be thrown instead.
e
- The exception that occurred during execution of a
Retryable
object.attemptNo
- The attempt number on which the exception was thrown.delayMs
- The delay (in milliseconds) from the previous invocation
attempt.Exception
instance signifying the terminal failure of
the RetryPolicy
execution.public final <T> T run(Retryable<T,E> retryable) throws E extends Exception
Retryable
object to be executed. Exceptions in
Retryable.retry(RetryPolicy,int)
will be considered for retry if
the number of maxRetries
has not been exceeded, and
retryOn(Exception)
returns true
.T
- The type of the result object.retryable
- The Retryable
object to run.Retryable.retry(RetryPolicy,int)
.RetryFailureException
- If retry attempts have exceeded
maxRetries
, or if retryOn(Exception)
returns
false
.NullPointerException
- If retryable
is null.E extends Exception
public final <T> T run(Retryable<T,E> retryable, long timeout) throws E extends Exception
Retryable
object to be executed. Exceptions in
Retryable.retry(RetryPolicy,int)
will be considered for retry if
the number of maxRetries
has not been exceeded, and
retryOn(Exception)
returns true
.T
- The type of the result object.retryable
- The Retryable
object to run.timeout
- The maximum time to retry in milliseconds.Retryable.retry(RetryPolicy,int)
.RetryFailureException
- If retry attempts have exceeded
maxRetries
, if retryOn(Exception)
returns
false
, or if timeout
is exceeded.IllegalArgumentException
- If timeout
is negative.NullPointerException
- If the value of timeout
is negative.E extends Exception
protected void onRetry(int attemptNo)
Retryable.retry(RetryPolicy,int)
.
Runtime exceptions thrown from this method will result in the termination
of the RetryPolicy
execution.
attemptNo
- The next attempt number.public int getMaxRetries()
RetryPolicy
.RetryPolicy
.protected abstract long getDelayMs(int attemptNo)
attemptNo
- The attempt number, starting with 1
.Copyright © 2020 LibJ. All rights reserved.