Package io.fluxcapacitor.common
Class TimingUtils
java.lang.Object
io.fluxcapacitor.common.TimingUtils
Utility class for measuring execution time and retrying operations with configurable backoff and error handling.
Provides static methods to:
- Measure the execution duration of tasks with optional reporting callbacks.
- Retry failing tasks with custom retry conditions and backoff strategies.
- Check if a deadline has passed.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
isMissedDeadline
(long deadline) Returns whether the given deadline has passed using the system UTC clock.static boolean
isMissedDeadline
(Clock clock, long deadline) Returns whether the given deadline has passed using the specifiedClock
.static void
retryOnFailure
(Runnable task, Duration delay) Retries a task indefinitely if it fails, using a fixed delay.static void
retryOnFailure
(Runnable task, Duration delay, Predicate<Throwable> predicate) Retries a task indefinitely if it fails and matches the given exception predicate.static <T> T
retryOnFailure
(Callable<T> task, RetryConfiguration configuration) Retries aCallable
task using a fullRetryConfiguration
.static <T> T
retryOnFailure
(Callable<T> task, Duration delay) Retries aCallable
task indefinitely with a fixed delay.static <T> T
retryOnFailure
(Callable<T> task, Duration delay, Predicate<Throwable> errorTest) Retries aCallable
task with a delay and a predicate to filter retryable exceptions.static void
Executes a task and measures its execution time in milliseconds.static void
time
(Runnable task, Consumer<Long> callback, TemporalUnit timeUnit) Executes a task and measures its execution time in the specifiedTemporalUnit
.static <T> T
Executes aCallable
and measures its execution time in milliseconds.static <T> T
time
(Callable<T> task, Consumer<Long> callback, TemporalUnit timeUnit) Executes aCallable
and measures its execution time in the givenTemporalUnit
.
-
Constructor Details
-
TimingUtils
public TimingUtils()
-
-
Method Details
-
time
Executes a task and measures its execution time in milliseconds.- Parameters:
task
- the task to runcallback
- the callback to report the elapsed time
-
time
Executes a task and measures its execution time in the specifiedTemporalUnit
.- Parameters:
task
- the task to runcallback
- the callback to report the elapsed timetimeUnit
- the unit of time to report
-
time
Executes aCallable
and measures its execution time in milliseconds.- Type Parameters:
T
- the return type of the task- Parameters:
task
- the task to runcallback
- the callback to report the elapsed time- Returns:
- the result of the callable
-
time
Executes aCallable
and measures its execution time in the givenTemporalUnit
.- Type Parameters:
T
- the return type of the task- Parameters:
task
- the task to runcallback
- the callback to report the elapsed timetimeUnit
- the time unit to use- Returns:
- the result of the callable
-
retryOnFailure
Retries a task indefinitely if it fails, using a fixed delay.- Parameters:
task
- the task to retrydelay
- the delay between retries
-
retryOnFailure
Retries a task indefinitely if it fails and matches the given exception predicate.- Parameters:
task
- the task to retrydelay
- the delay between retriespredicate
- predicate to determine which exceptions are retryable
-
retryOnFailure
Retries aCallable
task indefinitely with a fixed delay.- Type Parameters:
T
- the result type- Parameters:
task
- the task to retrydelay
- the delay between retries- Returns:
- the successful result
-
retryOnFailure
public static <T> T retryOnFailure(Callable<T> task, Duration delay, Predicate<Throwable> errorTest) Retries aCallable
task with a delay and a predicate to filter retryable exceptions.- Type Parameters:
T
- the result type- Parameters:
task
- the task to executedelay
- the delay between retrieserrorTest
- predicate to test whether an exception is retryable- Returns:
- the successful result, or
null
if not retryable and throwOnFailingErrorTest is false
-
retryOnFailure
Retries aCallable
task using a fullRetryConfiguration
. Supports optional logging, error test logic, retry limits, and fail-fast behavior.- Type Parameters:
T
- the result type- Parameters:
task
- the task to runconfiguration
- the retry configuration- Returns:
- the result of the task
-
isMissedDeadline
public static boolean isMissedDeadline(long deadline) Returns whether the given deadline has passed using the system UTC clock.- Parameters:
deadline
- the deadline to compare- Returns:
- true if the current time is after the deadline
-
isMissedDeadline
Returns whether the given deadline has passed using the specifiedClock
.- Parameters:
clock
- the clock to usedeadline
- the deadline to compare- Returns:
- true if the current time is after the deadline
-