Class TimingUtils

java.lang.Object
io.fluxcapacitor.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