Package org.refcodes.controlflow
Class RetryCounterImpl
- java.lang.Object
-
- org.refcodes.controlflow.RetryCounterImpl
-
- All Implemented Interfaces:
Retryable
,RetryCounter
,org.refcodes.mixin.Abortable
,org.refcodes.mixin.Restartable
public class RetryCounterImpl extends java.lang.Object implements RetryCounter
Implementation of theRetryCounter
interface.
-
-
Constructor Summary
Constructors Constructor Description RetryCounterImpl(int aRetryNumber)
Constructs a retry retry counter by providing the number of retries with no delay before each retry.RetryCounterImpl(int aRetryNumber, long aRetryDelayInMs)
Constructs a retry timeout counter by providing the number of retries and the delay before each retry.RetryCounterImpl(int aRetryNumber, long aRetryDelayInMs, long aExpRetryDelayExtensionInMs)
Constructs a retry timeout counter by providing the number of retries and the delay before each retry and taking an exponential delay extension into calculation: ------------------------------------------------------------------------- "(getRetryDelayInMs() + (Math.pow( 4, getRetryCount() - 1 ) * getExpRetryDelayExtensionInMs())" ------------------------------------------------------------------------- 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.RetryCounterImpl(long aTotalDelayInMs, long aRetryDelayInMs)
Constructs a retry timeout counter by providing the total delay time and the delay before each retry.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort()
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
hasNextRetry()
Returns true in case not all retires have been used up.boolean
nextRetry()
Tests whether a next retry is possible.void
restart()
void
setRetryDelyInMs(long aRetryDelayInMs)
Sets the delay before each retry to wait in milliseconds.
-
-
-
Constructor Detail
-
RetryCounterImpl
public RetryCounterImpl(int aRetryNumber)
Constructs a retry retry counter by providing the number of retries with no delay before each retry.- Parameters:
aRetryNumber
- Sets the number of retires this instance allows altogether
-
RetryCounterImpl
public RetryCounterImpl(long aTotalDelayInMs, long aRetryDelayInMs)
Constructs a retry timeout counter by providing the total delay time and the delay before each retry. The total delay time is the total time thisRetryCounter
will delay till the last call of thenextRetry()
. It does not take the time "lost" into account lost by business logic calling theRetryCounter
. The actual caclultated number of retires may be a rounded one, depending whether the total delay divided by the retry delay has a reminder. Basically speaking this is a convenience constructor callingRetryCounterImpl(int, long, long)
calculating the number of retires by: ------------------------------------------------------------------------- "aRetryNumber = (aTotalDelayInMs / aRetryDelayInMs)" -------------------------------------------------------------------------- Parameters:
aTotalDelayInMs
- Sets the total delay time this instance allows altogether. From this number divided by the actual retry delay results in the number of retires.aRetryDelayInMs
- The delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped.
-
RetryCounterImpl
public RetryCounterImpl(int aRetryNumber, long aRetryDelayInMs)
Constructs a retry timeout counter by providing the number of retries and the delay before each retry. 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.- Parameters:
aRetryNumber
- Sets the number of retires this instance allows altogetheraRetryDelayInMs
- The delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped.
-
RetryCounterImpl
public RetryCounterImpl(int aRetryNumber, long aRetryDelayInMs, long aExpRetryDelayExtensionInMs)
Constructs a retry timeout counter by providing the number of retries and the delay before each retry and taking an exponential delay extension into calculation: ------------------------------------------------------------------------- "(getRetryDelayInMs() + (Math.pow( 4, getRetryCount() - 1 ) * getExpRetryDelayExtensionInMs())" ------------------------------------------------------------------------- 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. In case an exponential delay extension is being set greater than 0, then the delay is extended accordingly. A delay of 0 or smaller skips delaying altogether.- Parameters:
aRetryNumber
- Sets the number of retires this instance allows altogetheraRetryDelayInMs
- The delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped.aExpRetryDelayExtensionInMs
- the exp retry delay extension in ms
-
-
Method Detail
-
nextRetry
public 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 theRetryCounter.nextRetry()
increments the retry counterRetryCounter.getRetryCount()
to be 1 and not 0, so the retry counter is actually a "try" counter, having the first "try" upon invokingRetryCounter.nextRetry()
. -------------------------------------------------------------------------- Specified by:
nextRetry
in interfaceRetryable
- Specified by:
nextRetry
in interfaceRetryCounter
- Returns:
- True in case there is a next retry as of
Retryable.hasNextRetry()
.
-
getNextRetryDelayInMs
public long getNextRetryDelayInMs()
Calculates the next retry delay in milliseconds. This is the time theRetryCounter.nextRetry()
method will wait in case its rules match for doing a sleep.- Specified by:
getNextRetryDelayInMs
in interfaceRetryCounter
- Returns:
- The next retry delay in milliseconds.
-
hasNextRetry
public boolean hasNextRetry()
Returns true in case not all retires have been used up.- Specified by:
hasNextRetry
in interfaceRetryable
- Returns:
- True in case there are retries left true in case all retries elapsed.
-
restart
public void restart()
- Specified by:
restart
in interfaceorg.refcodes.mixin.Restartable
-
abort
public void abort()
- Specified by:
abort
in interfaceorg.refcodes.mixin.Abortable
-
getRetryNumber
public int getRetryNumber()
Returns the number of retires this instance allows altogether.- Specified by:
getRetryNumber
in interfaceRetryCounter
- Returns:
- The number of altogether retries.
-
getInitialRetryDelayInMs
public long getInitialRetryDelayInMs()
Returns the initial delay before each retry to wait in milliseconds. TheRetryCounter.getCurrentRetryDelayInMs()
is calculated from the initial delay and the exponential retry delay added for each retry (not added for the first call toRetryCounter.nextRetry()
).- Specified by:
getInitialRetryDelayInMs
in interfaceRetryCounter
- Returns:
- The delay to being set in milliseconds.
-
getCurrentRetryDelayInMs
public 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 theRetryCounter.getInitialRetryDelayInMs()
with theRetryCounter.getExpRetryDelayExtensionInMs()
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. -------------------------------------------------------------------------- Specified by:
getCurrentRetryDelayInMs
in interfaceRetryCounter
- Returns:
- The delay to being set in milliseconds.
-
getExpRetryDelayExtensionInMs
public 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 callingRetryCounter.nextRetry()
is extended by that exponential delay (being increased for each retry): ------------------------------------------------------------------------- "(getRetryDelayInMs() + (Math.pow( 4, getRetryCount() - 1 ) * getExpRetryDelayExtensionInMs())" -------------------------------------------------------------------------- Specified by:
getExpRetryDelayExtensionInMs
in interfaceRetryCounter
- Returns:
- The exponential retry delay extension time in milliseconds.
-
getRetryCount
public int getRetryCount()
The current state regarding the retires. It specifies at which retry we currently are. ------------------------------------------------------------------------- ATTENTION: The first call theRetryCounter.nextRetry()
increments the retry counterRetryCounter.getRetryCount()
to be 1 and not 0, so the retry counter is actually a "try" counter, having the first "try" upon invokingRetryCounter.nextRetry()
. -------------------------------------------------------------------------- Specified by:
getRetryCount
in interfaceRetryable
- Specified by:
getRetryCount
in interfaceRetryCounter
- Returns:
- The number of retries used up so far
-
setRetryDelyInMs
public void setRetryDelyInMs(long aRetryDelayInMs)
Sets the delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped.- Parameters:
aRetryDelayInMs
- The delay to be set in milliseconds.
-
-