Class RetryStrategy

  • All Implemented Interfaces:

    
    public class RetryStrategy
    
                        

    A retry strategy for doing some job. It allows to specify initial delay before the task is started as well as retry delay. It will be executed as long as the task Callable returns true. It is also possible to configure whether the retries should be continued after unexpected exception or not. If we decide to not continue retries from outside the task cancel method will prevent from scheduling future retries(but it will not interrupt currently executing one). Check with isCancelled to stop the operation in progress. See "RetryStrategyTest" for usage samples.

    Author:

    Pawel Domas

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      RetryStrategy() Creates new RetryStrategy instance that will use ScheduledExecutorService with pool size of 1 thread to schedule retry attempts.
      RetryStrategy(ScheduledExecutorService retryExecutor) Creates new instance of RetryStrategy that will use given ScheduledExecutorService to schedule retry attempts.
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      synchronized void cancel() Cancels any future retry attempts.
      synchronized boolean isCancelled() Returns true if this retry strategy has been cancelled or false otherwise.
      synchronized void runRetryingTask(RetryTask task) Start given RetryTask that will be executed for the first time after getInitialDelay.
      • Methods inherited from class java.lang.Object

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

      • RetryStrategy

        RetryStrategy()
        Creates new RetryStrategy instance that will use ScheduledExecutorService with pool size of 1 thread to schedule retry attempts.
      • RetryStrategy

        RetryStrategy(ScheduledExecutorService retryExecutor)
        Creates new instance of RetryStrategy that will use given ScheduledExecutorService to schedule retry attempts.
        Parameters:
        retryExecutor - ScheduledExecutorService that will be used for scheduling retry attempts.
    • Method Detail

      • cancel

         synchronized void cancel()

        Cancels any future retry attempts. Currently running tasks are not interrupted.

      • isCancelled

         synchronized boolean isCancelled()

        Returns true if this retry strategy has been cancelled or false otherwise.

      • runRetryingTask

         synchronized void runRetryingTask(RetryTask task)

        Start given RetryTask that will be executed for the first time after getInitialDelay. After first execution next retry attempts will be rescheduled as long as it's callable method returns true or until (cancel is called.

        Parameters:
        task - the retry task to be employed by this retry strategy instance