Class RetryPolicy

java.lang.Object
com.tnsai.resilience.RetryPolicy

public final class RetryPolicy extends Object
Immutable retry policy definition used by the resilience layer. Supports exponential backoff, jitter and fine-grained retry conditions.
  • Method Details

    • getMaxAttempts

      public int getMaxAttempts()
    • getInitialBackoff

      public Duration getInitialBackoff()
    • getMaxBackoff

      public Duration getMaxBackoff()
    • getMultiplier

      public double getMultiplier()
    • getJitterFactor

      public double getJitterFactor()
    • getExtraCondition

      public Predicate<Exception> getExtraCondition()
    • shouldRetry

      public boolean shouldRetry(Exception exception, int attempt)
      Returns true if the operation should be retried for the given exception and attempt number.
      Parameters:
      exception - The raised exception
      attempt - The current attempt count (1 = first retry)
    • nextDelay

      public Duration nextDelay(Duration currentDelay)
      Calculates the next backoff duration using exponential growth with jitter. Handles potential overflow by capping at maxBackoff.
      Parameters:
      currentDelay - Current delay
      Returns:
      Next delay duration
    • builder

      public static RetryPolicy.Builder builder()
    • defaultPolicy

      public static RetryPolicy defaultPolicy()
    • from

      public static RetryPolicy from(Resilience.Retry retry)
      Builds a RetryPolicy from a Resilience.Retry annotation, mapping its millisecond fields to Durations and its jitter flag to a jitter factor. Only exception-typed entries in retryOn/noRetryOn are honoured (the annotation permits Throwable, but the policy operates on Exception).

      The annotation's maxAttempts default of 0 means "no retry"; callers should consult retry.maxAttempts() > 0 before wiring a policy. This factory floors maxAttempts at 1 so the resulting policy is always constructible.

      Parameters:
      retry - the annotation's retry configuration
      Returns:
      an equivalent immutable retry policy