Class RetryTimeout

java.lang.Object
org.refcodes.controlflow.RetryTimeout
All Implemented Interfaces:
Retryable, org.refcodes.mixin.Abortable, org.refcodes.mixin.ReadTimeoutMillisAccessor, org.refcodes.mixin.Restartable

public class RetryTimeout extends Object implements Retryable, org.refcodes.mixin.ReadTimeoutMillisAccessor
The RetryTimeout can be used in loops to test whether a timeout has been reached, if the timeout has not been reached, then a given period of time (delay) is waited. In case no more timeout is "left" the business logic may act accordingly such as throwing an exception.

The actual timeout being waited is the effective time elapsed from the first call to nextRetry() till the last call to nextRetry(). Any delay inside the iteration loop is not added up to the delay.

  • Nested Class Summary

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

    org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisBuilder<B extends org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisBuilder<B>>, org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisMutator, org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisProperty
  • Constructor Summary

    Constructors
    Constructor
    Description
    RetryTimeout(long aTimeoutMillis, int aRetryLoops)
    Constructs the RetryTimeout with the given timeout and the given number of retry loops.
    RetryTimeout(long aTimeoutMillis, long aRetryDelayInMs)
    Constructs the RetryTimeout with the given timeout and the given retry delay.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    long
    Returns the timeout altogether the RetryTimeout is waiting while iterating through the RetryTimeout via hasNextRetry() and nextRetry().
    int
    The current state regarding the retires.
    long
    Returns retry to wait in milliseconds upon calling nextRetry()).
    boolean
    Returns true in case not all retires have been used up.
    boolean
    Tests whether a next retry is possible.
    boolean
    nextRetry(Object aMonitor)
    Tests whether a next retry is possible.
    void

    Methods inherited from class java.lang.Object

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

    • RetryTimeout

      public RetryTimeout(long aTimeoutMillis, long aRetryDelayInMs)
      Constructs the RetryTimeout with the given timeout and the given retry delay. The retry number is round about: ------------------------------------------------------------------------- "aTimeoutMillis / aRetryDelayInMs" ------------------------------------------------------------------------- CAUTION: The above mentioned is not necessarily so, in case your business logic inside the loop iterations is slow, then less retries fit in the given timeout.
      Parameters:
      aTimeoutMillis - The total time in milliseconds all iterations together will delay.
      aRetryDelayInMs - The delay before each retry to wait in milliseconds.
    • RetryTimeout

      public RetryTimeout(long aTimeoutMillis, int aRetryLoops)
      Constructs the RetryTimeout with the given timeout and the given number of retry loops. The retry delay is round about: ------------------------------------------------------------------------- "aTimeoutMillis / aRetryLoops" ------------------------------------------------------------------------- CAUTION: The above mentioned is not necessarily so, in case your business logic inside the loop iterations is slow, then less retries fit in the given timeout.
      Parameters:
      aTimeoutMillis - The total time in milliseconds all iterations together will delay.
      aRetryLoops - The number of retries, each retry delay before each retry to wait in milliseconds is about the timeout divided by the retry number.
  • Method Details

    • nextRetry

      public boolean nextRetry()
      Tests whether a next retry is possible. In case this is the case, then the thread is delayed by the configured (implementation specific) delay time or until the provided monitor is notified via Object.notify() or Object.notifyAll(). Similar to the nextRetry() with the difference of accepting a dedicated monitor which is used for aborting the dellay.
      Specified by:
      nextRetry in interface Retryable
      Returns:
      True in case there is a next retry as of hasNextRetry().
    • nextRetry

      public boolean nextRetry(Object aMonitor)
      Tests whether a next retry is possible. In case this is the case, then the thread is delayed by the configured (implementation specific) delay time or until the provided monitor is notified via Object.notify() or Object.notifyAll(). Similar to the nextRetry() with the difference of accepting a dedicated monitor which is used for aborting the dellay.
      Parameters:
      aMonitor - the monitor
      Returns:
      True in case there is a next retry as of hasNextRetry().
    • 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.
    • getRetryCount

      public int getRetryCount()
      The current state regarding the retires. It specifies at which retry we currently are.
      Specified by:
      getRetryCount in interface Retryable
      Returns:
      The number of retries used up so far.
    • abort

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

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

      public long getReadTimeoutMillis()
      Returns the timeout altogether the RetryTimeout is waiting while iterating through the RetryTimeout via hasNextRetry() and nextRetry(). The actual timeout being waited is the effective time elapsed from the first call to nextRetry() till the last call to nextRetry(). Any delay inside the iteration loop is not added up to the delay.
      Specified by:
      getReadTimeoutMillis in interface org.refcodes.mixin.ReadTimeoutMillisAccessor
      Returns:
      The timeout in milliseconds.
    • getRetryDelayInMs

      public long getRetryDelayInMs()
      Returns retry to wait in milliseconds upon calling nextRetry()).
      Returns:
      The delay being set in milliseconds.