Class RetryPolicy
java.lang.Object
com.loomcache.client.retry.RetryPolicy
Configurable retry policy with exponential backoff, jitter, and statistics tracking.
Supports configurable max retries, exponential backoff with base delay, maximum delay cap, and random jitter to prevent thundering herd.
Usage:
var policy = RetryPolicy.builder()
.maxRetries(3)
.baseDelayMs(100)
.maxDelayMs(5000)
.jitterFactor(0.25)
.build();
String result = policy.execute(() -> client.get("map", "key"));
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic interfacestatic final recordStatistics snapshot for retry operations. -
Method Summary
Modifier and TypeMethodDescriptionstatic RetryPolicy.Builderbuilder()longcalculateDelay(int attempt) Calculate the delay for the given attempt using exponential backoff with jitter.static RetryPolicy<T> @Nullable Texecute(RetryPolicy.RetryableOperation<T> operation) Executes the given operation with retry logic.Get a snapshot of retry statistics.booleanCheck if circuit breaker is currently open.static RetryPolicynoRetry()voidManually reset circuit breaker.withCircuitBreaker(int failureThreshold, long resetTimeoutMs) Create a new RetryPolicy with circuit breaker configuration.withJitter(double jitterFactor) Create a new RetryPolicy with updated jitter factor.withRetryPredicate(Predicate<Exception> retryPredicate) Create a new RetryPolicy with custom retry predicate.
-
Method Details
-
execute
Executes the given operation with retry logic.- Type Parameters:
T- the return type of the operation- Parameters:
operation- the operation to execute (must not be null)- Returns:
- the result of the operation
- Throws:
IllegalStateException- if the circuit breaker is openException- if the operation fails after all retriesNullPointerException- if operation is null
-
calculateDelay
public long calculateDelay(int attempt) Calculate the delay for the given attempt using exponential backoff with jitter.- Parameters:
attempt- the attempt number (0-indexed)- Returns:
- the calculated delay in milliseconds
-
getRetryStats
Get a snapshot of retry statistics.- Returns:
- a snapshot of current retry statistics (never null)
-
isCircuitOpen
public boolean isCircuitOpen()Check if circuit breaker is currently open.- Returns:
- true if the circuit breaker is open, false otherwise
-
resetCircuit
public void resetCircuit()Manually reset circuit breaker. -
withCircuitBreaker
Create a new RetryPolicy with circuit breaker configuration.- Parameters:
failureThreshold- the failure threshold for circuit breakerresetTimeoutMs- the timeout for circuit breaker reset in milliseconds- Returns:
- a new RetryPolicy with circuit breaker configuration
-
withJitter
Create a new RetryPolicy with updated jitter factor.- Parameters:
jitterFactor- the new jitter factor (0-1)- Returns:
- a new RetryPolicy with updated jitter factor
-
withRetryPredicate
Create a new RetryPolicy with custom retry predicate.- Parameters:
retryPredicate- the custom retry predicate (must not be null)- Returns:
- a new RetryPolicy with custom retry predicate
-
noRetry
-
defaultPolicy
-
builder
-