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 <T> TcallAndWait(@NonNull Callable<T> task, Duration maxDuration) Executes the given task within the specified maximum duration.static <T> TcallAndWait(Callable<T> task, Duration maxDuration, Supplier<? extends T> fallback) Executes the given task within the specified maximum duration.static 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 booleanrunAndWait(ThrowingRunnable task, Duration maxDuration) Executes the given task and waits for completion up to the given maximum duration.static booleanrunAndWaitSafely(ThrowingRunnable task, Duration maxDuration) Executes the given task and waits for completion up to the given maximum duration.static booleanrunAndWaitSafely(ThrowingRunnable task, Duration maxDuration, boolean logFailure) Executes the given task and waits for completion up to the given maximum duration.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
-
runAndWaitSafely
Executes the given task and waits for completion up to the given maximum duration.If the task fails, false is returned without logging the failure.
- Parameters:
task- the task to executemaxDuration- the maximum duration to wait- Returns:
trueif the task completed in time,falseif it timed out
-
runAndWaitSafely
public static boolean runAndWaitSafely(ThrowingRunnable task, Duration maxDuration, boolean logFailure) Executes the given task and waits for completion up to the given maximum duration.If the task fails, a warning is optionally logged and false is returned.
- Parameters:
task- the task to executemaxDuration- the maximum duration to wait- Returns:
trueif the task completed in time,falseif it timed out
-
runAndWait
Executes the given task and waits for completion up to the given maximum duration.- Parameters:
task- the task to executemaxDuration- the maximum duration to wait- Returns:
trueif the task completed in time,falseif it timed out
-
callAndWait
public static <T> T callAndWait(Callable<T> task, Duration maxDuration, Supplier<? extends T> fallback) Executes the given task within the specified maximum duration. If the task times out, the fallback value is returned.- Type Parameters:
T- the task result type- Parameters:
task- the task to executemaxDuration- the maximum duration to waitfallback- supplies the fallback value in case of timeout- Returns:
- the task result, or the fallback result if the task timed out
-
callAndWait
public static <T> T callAndWait(@NonNull @NonNull Callable<T> task, Duration maxDuration) throws TimeoutException Executes the given task within the specified maximum duration.- Type Parameters:
T- the task result type- Parameters:
task- the task to executemaxDuration- the maximum duration to wait- Returns:
- the task result
- Throws:
TimeoutException- if the task does not complete within the given duration
-