Class RetryCounter

java.lang.Object
org.refcodes.controlflow.RetryCounter
All Implemented Interfaces:
Retryable, org.refcodes.mixin.Abortable, org.refcodes.mixin.Restartable, org.refcodes.mixin.RetryCountAccessor, org.refcodes.mixin.RetryNumberAccessor

public class RetryCounter extends Object implements Retryable, org.refcodes.mixin.RetryNumberAccessor, org.refcodes.mixin.RetryCountAccessor
The RetryCounter 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 calling nextRetry() is extended by that exponential delay (being increased for each retry).

ATTENTION: The first call the nextRetry() increments the retry counter getRetryCount() to be 1 and not 0, so the retry counter is actually a "try" counter, having the first "try" upon invoking nextRetry().

  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.refcodes.mixin.RetryCountAccessor

    org.refcodes.mixin.RetryCountAccessor.RetryCountBuilder<B extends org.refcodes.mixin.RetryCountAccessor.RetryCountBuilder<B>>, org.refcodes.mixin.RetryCountAccessor.RetryCountMutator, org.refcodes.mixin.RetryCountAccessor.RetryCountProperty

    Nested classes/interfaces inherited from interface org.refcodes.mixin.RetryNumberAccessor

    org.refcodes.mixin.RetryNumberAccessor.RetryNumberBuilder<B extends org.refcodes.mixin.RetryNumberAccessor.RetryNumberBuilder<B>>, org.refcodes.mixin.RetryNumberAccessor.RetryNumberMutator, org.refcodes.mixin.RetryNumberAccessor.RetryNumberProperty
  • Constructor Summary

    Constructors
    Constructor
    Description
    RetryCounter(int aRetryNumber)
    Constructs a retry retry counter by providing the number of retries with no delay before each retry.
    RetryCounter(int aRetryNumber, long aRetryDelayMillis)
    Constructs a retry timeout counter by providing the number of retries and the delay before each retry.
    RetryCounter(int aRetryNumber, long aRetryDelayMillis, long aExpRetryDelayExtensionMillis)
    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: ------------------------------------------------------------------------- "(getRetryDelayMillis() + (Math.pow( 4, getRetryCount() - 1 ) * getExpRetryDelayExtensionMillis())" ------------------------------------------------------------------------- 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.
    RetryCounter(long aTotalDelayMillis, long aRetryDelayMillis)
    Constructs a retry timeout counter by providing the total delay time and the delay before each retry.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    long
    Returns the current delay before a retry to wait in milliseconds.
    long
    Returns the exponential retry delay extension time in milliseconds.
    long
    Returns the initial delay before each retry to wait in milliseconds.
    long
    Calculates the next retry delay in milliseconds.
    int
    The current state regarding the retires.
    int
    Returns the number of retires this instance allows altogether.
    long
    Returns the total retry delay already accumulated as of calling the nextRetry() method since the initial construction or the last restart().
    boolean
    Returns true in case not all retires have been used up.
    boolean
    Tests whether a next retry is possible.
    void
    void
    setRetryDelayMillis(long aRetryDelayMillis)
    Sets the delay before each retry to wait in milliseconds.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RetryCounter

      public RetryCounter(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
    • RetryCounter

      public RetryCounter(long aTotalDelayMillis, long aRetryDelayMillis)
      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 this RetryCounter will delay till the last call of the nextRetry(). It does not take the time "lost" into account lost by business logic calling the RetryCounter. The actual calculated 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 calling RetryCounter(int, long, long) calculating the number of retires by: ------------------------------------------------------------------------- "aRetryNumber = (aTotalDelayMillis / aRetryDelayMillis)" -------------------------------------------------------------------------
      Parameters:
      aTotalDelayMillis - Sets the total delay time this instance allows altogether. From this number divided by the actual retry delay results in the number of retires.
      aRetryDelayMillis - The delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped.
    • RetryCounter

      public RetryCounter(int aRetryNumber, long aRetryDelayMillis)
      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 altogether (value of -1 represents an infinite number which can only be aborted via abort()).
      aRetryDelayMillis - The delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped.
    • RetryCounter

      public RetryCounter(int aRetryNumber, long aRetryDelayMillis, long aExpRetryDelayExtensionMillis)
      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: ------------------------------------------------------------------------- "(getRetryDelayMillis() + (Math.pow( 4, getRetryCount() - 1 ) * getExpRetryDelayExtensionMillis())" ------------------------------------------------------------------------- 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 altogether
      aRetryDelayMillis - The delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped.
      aExpRetryDelayExtensionMillis - the exponential retry delay extension in milliseconds
  • Method Details

    • 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 the nextRetry() increments the retry counter getRetryCount() to be 1 and not 0, so the retry counter is actually a "try" counter, having the first "try" upon invoking nextRetry(). -------------------------------------------------------------------------
      Specified by:
      nextRetry in interface Retryable
      Returns:
      True in case there is a next retry as of hasNextRetry().
    • getNextRetryDelayMillis

      public long getNextRetryDelayMillis()
      Calculates the next retry delay in milliseconds. This is the time the nextRetry() method will wait in case its rules match for doing a sleep.
      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 interface Retryable
      Returns:
      True in case there are retries left true in case all retries elapsed.
    • restart

      public void restart()
      Specified by:
      restart in interface org.refcodes.mixin.Restartable
    • abort

      public void abort()
      Specified by:
      abort in interface org.refcodes.mixin.Abortable
    • getRetryNumber

      public int getRetryNumber()
      Returns the number of retires this instance allows altogether.
      Specified by:
      getRetryNumber in interface org.refcodes.mixin.RetryNumberAccessor
      Returns:
      The number of altogether retries.
    • getInitialRetryDelayMillis

      public long getInitialRetryDelayMillis()
      Returns the initial delay before each retry to wait in milliseconds. The getCurrentRetryDelayMillis() is calculated from the initial delay and the exponential retry delay added for each retry (not added for the first call to nextRetry()).
      Returns:
      The delay to being set in milliseconds.
    • getCurrentRetryDelayMillis

      public long getCurrentRetryDelayMillis()
      Returns the current delay before a retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped. The current delay is calculated by extending the getInitialRetryDelayMillis() with the getExponentialRetryDelayExtensionMillis() 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.
    • getExponentialRetryDelayExtensionMillis

      public long getExponentialRetryDelayExtensionMillis()
      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 calling nextRetry() is extended by that exponential delay (being increased for each retry): ------------------------------------------------------------------------- "(getRetryDelayMillis() + (Math.pow( 4, getRetryCount() - 1 ) * getExpRetryDelayExtensionMillis())" -------------------------------------------------------------------------
      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 the nextRetry() increments the retry counter getRetryCount() to be 1 and not 0, so the retry counter is actually a "try" counter, having the first "try" upon invoking nextRetry(). -------------------------------------------------------------------------
      Specified by:
      getRetryCount in interface Retryable
      Specified by:
      getRetryCount in interface org.refcodes.mixin.RetryCountAccessor
      Returns:
      The number of retries used up so far
    • getTotalRetryDelayMillis

      public long getTotalRetryDelayMillis()
      Returns the total retry delay already accumulated as of calling the nextRetry() method since the initial construction or the last restart().
      Returns:
      The accumulated total delay already taken effect.
    • setRetryDelayMillis

      public void setRetryDelayMillis(long aRetryDelayMillis)
      Sets the delay before each retry to wait in milliseconds. When less than or equal to 0, then the delay is skipped.
      Parameters:
      aRetryDelayMillis - The delay to be set in milliseconds.