Package com.guinetik.rr.interceptor
Class RetryInterceptor
- java.lang.Object
-
- com.guinetik.rr.interceptor.RetryInterceptor
-
- All Implemented Interfaces:
RequestInterceptor
public class RetryInterceptor extends Object implements RequestInterceptor
Interceptor that retries failed requests with configurable backoff.This interceptor catches exceptions during request execution and retries them based on configurable criteria. It supports exponential backoff, maximum retry limits, and custom retry predicates.
Default Retry Behavior
By default, retries are attempted for:
- 5xx Server Errors (500-599)
- 408 Request Timeout
- 429 Too Many Requests
- Network/Connection errors (status code 0 or -1)
The following are NOT retried:
- 4xx Client Errors (except 408, 429)
CircuitBreakerOpenException(circuit is open)
Basic Usage
// Retry up to 3 times with 1 second initial delay RetryInterceptor retry = new RetryInterceptor(3, 1000); RocketClient client = RocketClientFactory.builder("https://api.example.com") .withInterceptor(retry) .build();Exponential Backoff
// Retry with exponential backoff: 1s, 2s, 4s RetryInterceptor retry = new RetryInterceptor(3, 1000, 2.0);Custom Retry Predicate
// Only retry on specific status codes RetryInterceptor retry = RetryInterceptor.builder() .maxRetries(3) .initialDelayMs(500) .retryOn(e -> e.getStatusCode() == 503) .build();- Since:
- 1.1.0
- Author:
- guinetik <guinetik@gmail.com>
- See Also:
RequestInterceptor,InterceptorChain
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRetryInterceptor.BuilderBuilder for creating customized RetryInterceptor instances.
-
Constructor Summary
Constructors Constructor Description RetryInterceptor()Creates a retry interceptor with default settings.RetryInterceptor(int maxRetries, long initialDelayMs)Creates a retry interceptor with custom retry count and delay.RetryInterceptor(int maxRetries, long initialDelayMs, double backoffMultiplier)Creates a retry interceptor with exponential backoff.RetryInterceptor(int maxRetries, long initialDelayMs, double backoffMultiplier, long maxDelayMs, Predicate<RocketRestException> retryPredicate)Creates a fully customized retry interceptor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static RetryInterceptor.Builderbuilder()Creates a builder for custom retry configuration.intgetOrder()Returns the order of this interceptor in the chain.<Req,Res>
ResonError(RocketRestException e, RequestSpec<Req,Res> request, InterceptorChain chain)Called when an exception occurs during request execution.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.guinetik.rr.interceptor.RequestInterceptor
afterResponse, beforeRequest
-
-
-
-
Constructor Detail
-
RetryInterceptor
public RetryInterceptor()
Creates a retry interceptor with default settings. Uses 3 retries, 1 second initial delay, 2x backoff, 30 second max delay.
-
RetryInterceptor
public RetryInterceptor(int maxRetries, long initialDelayMs)Creates a retry interceptor with custom retry count and delay.- Parameters:
maxRetries- Maximum number of retries (must be positive)initialDelayMs- Initial delay between retries in milliseconds
-
RetryInterceptor
public RetryInterceptor(int maxRetries, long initialDelayMs, double backoffMultiplier)Creates a retry interceptor with exponential backoff.- Parameters:
maxRetries- Maximum number of retriesinitialDelayMs- Initial delay in millisecondsbackoffMultiplier- Multiplier for each subsequent retry (e.g., 2.0 for doubling)
-
RetryInterceptor
public RetryInterceptor(int maxRetries, long initialDelayMs, double backoffMultiplier, long maxDelayMs, Predicate<RocketRestException> retryPredicate)Creates a fully customized retry interceptor.- Parameters:
maxRetries- Maximum number of retriesinitialDelayMs- Initial delay in millisecondsbackoffMultiplier- Multiplier for exponential backoffmaxDelayMs- Maximum delay cap in millisecondsretryPredicate- Custom predicate to determine if exception is retryable (null for default)
-
-
Method Detail
-
onError
public <Req,Res> Res onError(RocketRestException e, RequestSpec<Req,Res> request, InterceptorChain chain) throws RocketRestException
Description copied from interface:RequestInterceptorCalled when an exception occurs during request execution.This method can:
- Retry the request using
InterceptorChain.retry(RequestSpec) - Transform the exception into a different one
- Recover and return a fallback response
- Rethrow the exception (default behavior)
- Specified by:
onErrorin interfaceRequestInterceptor- Type Parameters:
Req- The request body typeRes- The response type- Parameters:
e- The exception that occurredrequest- The request that failedchain- The interceptor chain (use for retry)- Returns:
- A recovered response, or throws an exception
- Throws:
RocketRestException- If the error cannot be handled
- Retry the request using
-
getOrder
public int getOrder()
Description copied from interface:RequestInterceptorReturns the order of this interceptor in the chain.Lower values execute first. Use negative values for interceptors that must run early (e.g., authentication), and positive values for interceptors that should run late (e.g., logging).
Suggested ordering:
- -100: Authentication/Authorization
- 0: Default (most interceptors)
- 100: Retry logic
- 200: Logging/Metrics
- Specified by:
getOrderin interfaceRequestInterceptor- Returns:
- The order value (lower = earlier)
-
builder
public static RetryInterceptor.Builder builder()
Creates a builder for custom retry configuration.- Returns:
- A new builder instance
-
-