Class BackoffCallback

java.lang.Object
com.tccc.kos.commons.util.concurrent.BaseCallback
com.tccc.kos.commons.util.concurrent.BackoffCallback

public class BackoffCallback extends BaseCallback
Class used to schedule callbacks based on a backoff algorithm. This leverages the Backoff class to compute backoff intervals and can be controlled by the supplied BackoffConfig object. Please see Backoff and BackoffConfig for details around configuring the backoff algorithm to use and how to configure it. This class supports changing the backoff configuration on the fly.

It is common for the BackoffConfig to be part of a config bean of the object utilizing the callback. For example, a service may use a BackoffCallback internally but the associated BackoffConfig is part of the service ConfigBean. This allows the configuration of the timer to be adjusted on the fly using standard configuration tools.

Starting this callback will reset the backoff count to zero and start the timer. This facilitates the use case where the callback is only started when reaching a failure state where retries are required. When the failure state is complete, use cancel() to stop the timer.

This implementation guarantees that the callback is never called concurrently, even if the timer is rebuilt and triggered during an existing callback, or even if the timer if flushed.

Since:
1.0
Version:
2024-11-07
  • Constructor Details

    • BackoffCallback

      public BackoffCallback(Runnable callback)
      Creates a callback that utilizes a Backoff with a default BackoffConfig object. The config object can be changed at any time using setConfig() . The timer is not started until the start() method is called.
      Parameters:
      callback - the callback to call when the timer fires
    • BackoffCallback

      public BackoffCallback(BackoffConfig config, Runnable callback)
      Creates a callback that utilizes a Backoff with the supplied configuration. The timer is not started until the start() method is called.
      Parameters:
      config - the config to use for the backoff, set to default if null
      callback - the callback to call when the timer fires
  • Method Details

    • getConfig

      public BackoffConfig getConfig()
      Return the BackoffConfig associated with this callback.
    • setConfig

      public void setConfig(BackoffConfig config)
      Set the BackoffConfig associated with this callback. This can be changed at any time, even while the callback is running.
    • start

      public void start()
      Clears the count in the associated backoff and starts the timer. If the timer is already running, it is first cancelled and then restarted. Each time the timer fires the current backoff delay will be computed and then the backoff will be incremented. will be incremented and the resulting time value will be used for the nextThe timer will continue to trigger callbacks based on the configuration of the associated backoff .
    • stop

      public void stop()
      Stops the callback. Calls cancel() and exists for api symmetry.
    • getBackoff

      public Backoff getBackoff()