Class BackoffCallback
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 Summary
ConstructorsConstructorDescriptionBackoffCallback(BackoffConfig config, Runnable callback) Creates a callback that utilizes aBackoffwith the supplied configuration.BackoffCallback(Runnable callback) Creates a callback that utilizes aBackoffwith a defaultBackoffConfigobject. -
Method Summary
Modifier and TypeMethodDescriptionReturn theBackoffConfigassociated with this callback.voidsetConfig(BackoffConfig config) Set theBackoffConfigassociated with this callback.voidstart()Clears the count in the associatedbackoffand starts the timer.voidstop()Stops the callback.Methods inherited from class com.tccc.kos.commons.util.concurrent.BaseCallback
cancel, flush, flushInline, force, forceInline, getCallback, getWaitTime, isRunning, isStopped
-
Constructor Details
-
BackoffCallback
Creates a callback that utilizes aBackoffwith a defaultBackoffConfigobject. The config object can be changed at any time usingsetConfig(). The timer is not started until thestart()method is called.- Parameters:
callback- the callback to call when the timer fires
-
BackoffCallback
Creates a callback that utilizes aBackoffwith the supplied configuration. The timer is not started until thestart()method is called.- Parameters:
config- the config to use for the backoff, set to default if nullcallback- the callback to call when the timer fires
-
-
Method Details
-
getConfig
Return theBackoffConfigassociated with this callback. -
setConfig
Set theBackoffConfigassociated 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 associatedbackoffand starts the timer. If the timer is already running, it is first cancelled and then restarted. Each time the timer fires the currentbackoffdelay 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 associatedbackoff. -
stop
public void stop()Stops the callback. Callscancel()and exists for api symmetry. -
getBackoff
-