Class RetryContext
java.lang.Object
express.mvp.myra.transport.error.RetryContext
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 Summary
ConstructorsConstructorDescriptionRetryContext(int maxAttempts) Creates a context with default operation ID.RetryContext(String operationId, int maxAttempts) Creates a new retry context. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the current attempt count.Returns the total elapsed time since start.Returns the time of the last attempt.Returns the last error encountered.Returns the category of the last error.intReturns the maximum number of attempts allowed.longReturns the configured delay for the next retry.Returns the operation identifier.intReturns the number of retries (attempts minus 1).Returns the time when retries started.longReturns the total time spent waiting in delays.booleanChecks if there are attempts remaining.booleanChecks if this is the first attempt.booleanChecks if this is the last allowed attempt.voidrecordDelay(long delayMillis) Records that a delay was executed.voidrecordFailure(Throwable error) Records a failed attempt.voidrecordFailure(Throwable error, ErrorCategory category) Records a failed attempt with explicit category.voidreset()Resets the context for reuse.voidsetNextDelay(long delayMillis) Sets the delay for the next retry.intRecords that an attempt is starting.toString()
-
Constructor Details
-
RetryContext
Creates a new retry context.- Parameters:
operationId- identifier for the operationmaxAttempts- 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
-
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
Records a failed attempt.- Parameters:
error- the error that caused the failure
-
recordFailure
Records a failed attempt with explicit category.- Parameters:
error- the error that caused the failurecategory- the error category
-
getLastError
Returns the last error encountered.- Returns:
- the last error, or null if no failures yet
-
getLastErrorCategory
Returns the category of the last error.- Returns:
- the error category, or null if no failures yet
-
getStartTime
-
getLastAttemptTime
Returns the time of the last attempt.- Returns:
- the last attempt time
-
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
-