Class TimingUtils

java.lang.Object
io.fluxzero.common.TimingUtils

public class TimingUtils extends Object
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 Details

    • TimingUtils

      public TimingUtils()
  • Method Details

    • time

      public static void time(Runnable task, Consumer<Long> callback)
      Executes a task and measures its execution time in milliseconds.
      Parameters:
      task - the task to run
      callback - the callback to report the elapsed time
    • time

      public static void time(Runnable task, Consumer<Long> callback, TemporalUnit timeUnit)
      Executes a task and measures its execution time in the specified TemporalUnit.
      Parameters:
      task - the task to run
      callback - the callback to report the elapsed time
      timeUnit - the unit of time to report
    • time

      public static <T> T time(Callable<T> task, Consumer<Long> callback)
      Executes a Callable and measures its execution time in milliseconds.
      Type Parameters:
      T - the return type of the task
      Parameters:
      task - the task to run
      callback - the callback to report the elapsed time
      Returns:
      the result of the callable
    • time

      public static <T> T time(Callable<T> task, Consumer<Long> callback, TemporalUnit timeUnit)
      Executes a Callable and measures its execution time in the given TemporalUnit.
      Type Parameters:
      T - the return type of the task
      Parameters:
      task - the task to run
      callback - the callback to report the elapsed time
      timeUnit - the time unit to use
      Returns:
      the result of the callable
    • retryOnFailure

      public static void retryOnFailure(Runnable task, Duration delay)
      Retries a task indefinitely if it fails, using a fixed delay.
      Parameters:
      task - the task to retry
      delay - the delay between retries
    • retryOnFailure

      public static void retryOnFailure(Runnable task, Duration delay, Predicate<Throwable> predicate)
      Retries a task indefinitely if it fails and matches the given exception predicate.
      Parameters:
      task - the task to retry
      delay - the delay between retries
      predicate - predicate to determine which exceptions are retryable
    • retryOnFailure

      public static <T> T retryOnFailure(Callable<T> task, Duration delay)
      Retries a Callable task indefinitely with a fixed delay.
      Type Parameters:
      T - the result type
      Parameters:
      task - the task to retry
      delay - the delay between retries
      Returns:
      the successful result
    • retryOnFailure

      public static <T> T retryOnFailure(Callable<T> task, Duration delay, Predicate<Throwable> errorTest)
      Retries a Callable task with a delay and a predicate to filter retryable exceptions.
      Type Parameters:
      T - the result type
      Parameters:
      task - the task to execute
      delay - the delay between retries
      errorTest - predicate to test whether an exception is retryable
      Returns:
      the successful result, or null if not retryable and throwOnFailingErrorTest is false
    • retryOnFailure

      public static <T> T retryOnFailure(Callable<T> task, RetryConfiguration configuration)
      Retries a Callable task using a full RetryConfiguration. Supports optional logging, error test logic, retry limits, and fail-fast behavior.
      Type Parameters:
      T - the result type
      Parameters:
      task - the task to run
      configuration - 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

      public static boolean isMissedDeadline(Clock clock, long deadline)
      Returns whether the given deadline has passed using the specified Clock.
      Parameters:
      clock - the clock to use
      deadline - the deadline to compare
      Returns:
      true if the current time is after the deadline
    • runAndWaitSafely

      public static boolean runAndWaitSafely(ThrowingRunnable task, Duration maxDuration)
      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 execute
      maxDuration - the maximum duration to wait
      Returns:
      true if the task completed in time, false if 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 execute
      maxDuration - the maximum duration to wait
      Returns:
      true if the task completed in time, false if it timed out
    • runAndWait

      public static boolean runAndWait(ThrowingRunnable task, Duration maxDuration)
      Executes the given task and waits for completion up to the given maximum duration.
      Parameters:
      task - the task to execute
      maxDuration - the maximum duration to wait
      Returns:
      true if the task completed in time, false if 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 execute
      maxDuration - the maximum duration to wait
      fallback - 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 execute
      maxDuration - the maximum duration to wait
      Returns:
      the task result
      Throws:
      TimeoutException - if the task does not complete within the given duration