Retry
Enables retry mechanism for annotated functions with configurable retry policies.
This annotation provides resilient error handling by automatically retrying failed operations according to specified policies. It's essential for handling transient failures in distributed systems, network operations, and external service calls.
The retry mechanism supports:
Exponential backoff with jitter
Configurable retry counts and timeouts
Selective exception handling (recoverable vs unrecoverable)
Circuit breaker patterns integration
Example usage:
class PaymentService {
@Retry(
maxRetries = 3,
minBackoff = 1,
recoverable = [IOException::class, TimeoutException::class],
unrecoverable = [IllegalArgumentException::class]
)
fun processPayment(request: PaymentRequest): PaymentResult {
// Network call that might fail temporarily
return paymentGateway.charge(request)
}
@Retry(enabled = false) // Disable retries for this operation
fun validatePayment(request: PaymentRequest) {
// Validation that should fail fast
require(request.amount > 0) { "Amount must be positive" }
}
}Parameters
Whether retry functionality is enabled. Defaults to true.
Maximum number of retry attempts. Defaults to DEFAULT_MAX_RETRIES.
Minimum backoff duration in seconds before first retry. Uses exponential backoff for subsequent retries. See java.time.temporal.ChronoUnit.SECONDS for time units.
Maximum timeout in seconds for each execution attempt. Prevents hanging operations. See java.time.temporal.ChronoUnit.SECONDS.
Array of exception types that should trigger retries when encountered. Only these exceptions will cause retry attempts.
Array of exception types that should NOT trigger retries. These exceptions fail immediately without retry.
See also
for the default retry limit
for the default backoff duration
for the default execution timeout