Class ExponentialTaskExecutionBackOff

java.lang.Object
com.github.sonus21.rqueue.utils.backoff.ExponentialTaskExecutionBackOff
All Implemented Interfaces:
TaskExecutionBackOff

public class ExponentialTaskExecutionBackOff extends Object implements TaskExecutionBackOff
Implementation of the TaskExecutionBackOff class, that increases the delay exponentially at the specified rate.

The delay is defined as Math.pow(multiplier, failureCount) * initialInterval.

Default delay for the task is 1.5 seconds, that increases at the rate of 1.5 on each failure. After a hundred plus failures, it will reach the limit of long value 9223372036854775807L, and no more retry would be possible, by default any task can be delay upto 3 hours 10800000L.

Using multiplier 1 would mean using the fixed execution back off FixedTaskExecutionBackOff

  • Field Details

    • DEFAULT_INITIAL_INTERVAL

      public static final long DEFAULT_INITIAL_INTERVAL
      The default initial delay for the any task: 1500 ms (1.5 seconds)
      See Also:
    • DEFAULT_MULTIPLIER

      public static final double DEFAULT_MULTIPLIER
      The default multiplier (increases the delay by 50%).
      See Also:
    • DEFAULT_MAX_INTERVAL

      public static final long DEFAULT_MAX_INTERVAL
      The default maximum delay for any task (3 hours)
      See Also:
  • Constructor Details

    • ExponentialTaskExecutionBackOff

      public ExponentialTaskExecutionBackOff()
      Create an instance with an initial interval of 1500L ms, maximum interval of 10800000L, multiplier of 1.5 and default unlimited retries.
    • ExponentialTaskExecutionBackOff

      public ExponentialTaskExecutionBackOff(long initialInterval, long maxInterval, double multiplier, int maxRetries)
      Create an instance.
      Parameters:
      initialInterval - the initial delay for the message
      maxInterval - the maximum delay for the message.
      multiplier - the factor at which delay would be increased.
      maxRetries - the maximum retry count for any message.
  • Method Details

    • getInitialInterval

      public long getInitialInterval()
      Return the initial interval in milliseconds.
      Returns:
      configured initial interval.
    • setInitialInterval

      public void setInitialInterval(long initialInterval)
      Set the initial interval in milliseconds.
      Parameters:
      initialInterval - initial interval in milliseconds.
    • getMaxRetries

      public int getMaxRetries()
      Return the maximum number of retries.
      Returns:
      maximum retries that will be performed.
    • setMaxRetries

      public void setMaxRetries(int maxRetries)
      Set the maximum number of retries
      Parameters:
      maxRetries - maximum retries
    • getMultiplier

      public double getMultiplier()
      get the multiplier
      Returns:
      multiplier for this back off.
    • setMultiplier

      public void setMultiplier(double multiplier)
      Set multiplier
      Parameters:
      multiplier - value of the multiplier
    • getMaxInterval

      public long getMaxInterval()
      get max interval
      Returns:
      long value
    • setMaxInterval

      public void setMaxInterval(long maxInterval)
      Set the max interval in milliseconds
      Parameters:
      maxInterval - value in milliseconds
    • nextBackOff

      public long nextBackOff(Object message, RqueueMessage rqueueMessage, int failureCount)
      Return the number of milliseconds to wait for the same message to be consumed by other consumers. (-1L) to indicate that no further enqueue should be made for the message.
      Specified by:
      nextBackOff in interface TaskExecutionBackOff
      Parameters:
      message - message that's fetched
      rqueueMessage - raw message
      failureCount - number of times this message has failed.
      Returns:
      backoff in the millisecond.
    • getMaxRetries

      protected int getMaxRetries(Object message, RqueueMessage rqueueMessage, int failureCount)
    • getDelay

      protected long getDelay(Object message, RqueueMessage rqueueMessage, int failureCount)
    • getDelay

      protected long getDelay(int failureCount)