Class RetryContext

java.lang.Object
express.mvp.myra.transport.error.RetryContext

public final class RetryContext extends Object
Tracks the state of retry attempts for an operation.

This class maintains information about retry attempts including counts, timing, and the last error encountered. It's used by RetryPolicy to make retry decisions.

Usage Example

RetryContext context = new RetryContext("send-operation", maxRetries);

while (context.hasAttemptsRemaining()) {
    try {
        performOperation();
        break; // Success
    } catch (Exception e) {
        context.recordFailure(e);
        if (context.hasAttemptsRemaining()) {
            Thread.sleep(context.getNextDelayMillis());
        } else {
            throw new OperationFailedException(
                "Operation failed after " + context.getAttemptCount() + " attempts", e);
        }
    }
}

Thread Safety

This class is not thread-safe. Each retry sequence should use its own context instance.

See Also:
  • Constructor Details

    • RetryContext

      public RetryContext(String operationId, int maxAttempts)
      Creates a new retry context.
      Parameters:
      operationId - identifier for the operation
      maxAttempts - maximum number of attempts
    • RetryContext

      public RetryContext(int maxAttempts)
      Creates a context with default operation ID.
      Parameters:
      maxAttempts - maximum number of attempts
  • Method Details

    • getOperationId

      public String getOperationId()
      Returns the operation identifier.
      Returns:
      the operation ID
    • getMaxAttempts

      public int getMaxAttempts()
      Returns the maximum number of attempts allowed.
      Returns:
      max attempts
    • getAttemptCount

      public int getAttemptCount()
      Returns the current attempt count.
      Returns:
      number of attempts made (0 before first attempt)
    • getRetryCount

      public int getRetryCount()
      Returns the number of retries (attempts minus 1).
      Returns:
      number of retries
    • hasAttemptsRemaining

      public boolean hasAttemptsRemaining()
      Checks if there are attempts remaining.
      Returns:
      true if more attempts are allowed
    • startAttempt

      public int startAttempt()
      Records that an attempt is starting.
      Returns:
      the new attempt number
    • recordFailure

      public void recordFailure(Throwable error)
      Records a failed attempt.
      Parameters:
      error - the error that caused the failure
    • recordFailure

      public void recordFailure(Throwable error, ErrorCategory category)
      Records a failed attempt with explicit category.
      Parameters:
      error - the error that caused the failure
      category - the error category
    • getLastError

      public Throwable getLastError()
      Returns the last error encountered.
      Returns:
      the last error, or null if no failures yet
    • getLastErrorCategory

      public ErrorCategory getLastErrorCategory()
      Returns the category of the last error.
      Returns:
      the error category, or null if no failures yet
    • getStartTime

      public Instant getStartTime()
      Returns the time when retries started.
      Returns:
      the start time
    • getLastAttemptTime

      public Instant getLastAttemptTime()
      Returns the time of the last attempt.
      Returns:
      the last attempt time
    • getElapsedTime

      public Duration getElapsedTime()
      Returns the total elapsed time since start.
      Returns:
      elapsed duration
    • getTotalDelayMillis

      public long getTotalDelayMillis()
      Returns the total time spent waiting in delays.
      Returns:
      total delay time in milliseconds
    • setNextDelay

      public void setNextDelay(long delayMillis)
      Sets the delay for the next retry.

      This is typically called by the retry policy.

      Parameters:
      delayMillis - delay in milliseconds
    • getNextDelayMillis

      public long getNextDelayMillis()
      Returns the configured delay for the next retry.
      Returns:
      delay in milliseconds
    • recordDelay

      public void recordDelay(long delayMillis)
      Records that a delay was executed.
      Parameters:
      delayMillis - the delay that was applied
    • isFirstAttempt

      public boolean isFirstAttempt()
      Checks if this is the first attempt.
      Returns:
      true if attemptCount is 0 or 1
    • isLastAttempt

      public boolean isLastAttempt()
      Checks if this is the last allowed attempt.
      Returns:
      true if no more retries are allowed after this attempt
    • reset

      public void reset()
      Resets the context for reuse.
    • toString

      public String toString()
      Overrides:
      toString in class Object