Package org.refcodes.controlflow
Interface RetryCounter
-
- All Superinterfaces:
org.refcodes.mixin.Abortable
,org.refcodes.mixin.Restartable
,Retryable
- All Known Implementing Classes:
RetryCounterImpl
public interface RetryCounter extends Retryable
TheRetryCounter
can be used in loops to test whether a retry should take place and in case a retry is to take place, then a given period of time (delay) is waited and the internal retry counter is decremented. In case no more retries are "available" the business logic may act accordingly such as throwing an exception.In case a delay greater than 0 is set, then each retry is being delayed by the according amount of milliseconds, in case no retries are left, then delaying is skipped. A delay of 0 or smaller skips delaying altogether.
The first call to
nextRetry()
does not delay as the first call is not considered to be a retry. In case an exponential retry delay extension time has been provided, then the retry delay upon callingnextRetry()
is extended by that exponential delay (being increased for each retry).ATTENTION: The first call the
nextRetry()
increments the retry countergetRetryCount()
to be 1 and not 0, so the retry counter is actually a "try" counter, having the first "try" upon invokingnextRetry()
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description long
getCurrentRetryDelayInMs()
Returns the current delay before each retry to wait in milliseconds.long
getExpRetryDelayExtensionInMs()
Returns the exponential retry delay extension time in milliseconds.long
getInitialRetryDelayInMs()
Returns the initial delay before each retry to wait in milliseconds.long
getNextRetryDelayInMs()
Calculates the next retry delay in milliseconds.int
getRetryCount()
The current state regarding the retires.int
getRetryNumber()
Returns the number of retires this instance allows altogether.boolean
nextRetry()
Tests whether a next retry is possible.-
Methods inherited from interface org.refcodes.controlflow.Retryable
hasNextRetry
-
-
-
-
Method Detail
-
getNextRetryDelayInMs
long getNextRetryDelayInMs()
Calculates the next retry delay in milliseconds. This is the time thenextRetry()
method will wait in case its rules match for doing a sleep.- Returns:
- The next retry delay in milliseconds.
-
getInitialRetryDelayInMs
long getInitialRetryDelayInMs()
Returns the initial delay before each retry to wait in milliseconds. ThegetCurrentRetryDelayInMs()
is calculated from the initial delay and the exponential retry delay added for each retry (not added for the first call tonextRetry()
).- Returns:
- The delay to being set in milliseconds.
-
getCurrentRetryDelayInMs
long getCurrentRetryDelayInMs()
Returns the current delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped. The current delay is calculated by extending thegetInitialRetryDelayInMs()
with thegetExpRetryDelayExtensionInMs()
per retry. ------------------------------------------------------------------------- The current retry delay gets incremented for each retry by an exponential retry delay extension time calculated from in case it is greater than 0. -------------------------------------------------------------------------- Returns:
- The delay to being set in milliseconds.
-
getExpRetryDelayExtensionInMs
long getExpRetryDelayExtensionInMs()
Returns the exponential retry delay extension time in milliseconds. In case an exponential retry delay extension time has been provided, then the retry delay upon callingnextRetry()
is extended by that exponential delay (being increased for each retry): ------------------------------------------------------------------------- "(getRetryDelayInMs() + (Math.pow( 4, getRetryCount() - 1 ) * getExpRetryDelayExtensionInMs())" -------------------------------------------------------------------------- Returns:
- The exponential retry delay extension time in milliseconds.
-
getRetryNumber
int getRetryNumber()
Returns the number of retires this instance allows altogether.- Returns:
- The number of altogether retries.
-
nextRetry
boolean nextRetry()
Tests whether a next retry is possible. In case this is true, then the retry count is incremented by one. In case this is not the first call to the retry counter, then the thread is delayed by the configured delay time. In case ------------------------------------------------------------------------- The first call does not delay as the first call is not considered to be a retry. In case an exponential retry delay extension time has been provided, then the retry delay is extended by that exponential delay being increased for each retry. ------------------------------------------------------------------------- ATTENTION: The first call thenextRetry()
increments the retry countergetRetryCount()
to be 1 and not 0, so the retry counter is actually a "try" counter, having the first "try" upon invokingnextRetry()
. -------------------------------------------------------------------------- Specified by:
nextRetry
in interfaceRetryable
- Returns:
- True in case there is a next retry as of
Retryable.hasNextRetry()
.
-
getRetryCount
int getRetryCount()
The current state regarding the retires. It specifies at which retry we currently are. ------------------------------------------------------------------------- ATTENTION: The first call thenextRetry()
increments the retry countergetRetryCount()
to be 1 and not 0, so the retry counter is actually a "try" counter, having the first "try" upon invokingnextRetry()
. -------------------------------------------------------------------------- Specified by:
getRetryCount
in interfaceRetryable
- Returns:
- The number of retries used up so far
-
-