Class TimingUtils
java.lang.Object
io.fluxzero.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 booleanisMissedDeadline(long deadline) Returns whether the given deadline has passed using the system UTC clock.static booleanisMissedDeadline(Clock clock, long deadline) Returns whether the given deadline has passed using the specifiedClock.static voidretryOnFailure(Runnable task, Duration delay) Retries a task indefinitely if it fails, using a fixed delay.static voidretryOnFailure(Runnable task, Duration delay, Predicate<Throwable> predicate) Retries a task indefinitely if it fails and matches the given exception predicate.static <T> TretryOnFailure(Callable<T> task, RetryConfiguration configuration) Retries aCallabletask using a fullRetryConfiguration.static <T> TretryOnFailure(Callable<T> task, Duration delay) Retries aCallabletask indefinitely with a fixed delay.static <T> TretryOnFailure(Callable<T> task, Duration delay, Predicate<Throwable> errorTest) Retries aCallabletask with a delay and a predicate to filter retryable exceptions.static voidExecutes a task and measures its execution time in milliseconds.static voidtime(Runnable task, Consumer<Long> callback, TemporalUnit timeUnit) Executes a task and measures its execution time in the specifiedTemporalUnit.static <T> TExecutes aCallableand measures its execution time in milliseconds.static <T> Ttime(Callable<T> task, Consumer<Long> callback, TemporalUnit timeUnit) Executes aCallableand measures its execution time in the givenTemporalUnit.
-
Constructor Details
-
TimingUtils
public TimingUtils()
-
-
Method Details
-
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 aCallableand 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 aCallableand 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
-
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
-
retryOnFailure
public static <T> T retryOnFailure(Callable<T> task, Duration delay, Predicate<Throwable> errorTest) Retries aCallabletask 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
nullif not retryable and throwOnFailingErrorTest is false
-
retryOnFailure
Retries aCallabletask 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
-