中文 | English
An elegant(useless😉) and lightweight retry framework for JVM which supports:
- Highly customizable retry and backoff policies.
- Both synchronous and asynchronous execution.
- Apply pre-defined retry template on existing object using dynamic proxy.
Sakura Retry does NOT attempt to support more features than other mainstream retry frameworks, nor does it offer better performance. In the early years, I started writing Sakura Retry due to a lack of information, and I have kept using it because I like it more and more in terms of aesthetics.
Available on Maven Central.
Example:
fun main() {
val retry = Retry.Builder()
.setCondition(maxAttempts(3))
.setBackoffPolicy(fixedDelayInSeconds(10) + randomDelayInSeconds(0, 1))
.addFailureListener(logging())
.build()
retry.call {
println("maybe fail")
}
}Basic Concepts:
- Condition: Determines whether to trigger or terminate retries.
- BackoffPolicy: Defines the waiting interval between retries.
- FailureListener: Handles (e.g., logging, alerting) upon execution failures.
RetryPolicy supports logical composition (e.g., maxAttempts(10) and !runtimeException()) to express complex retry strategies through a single concept. BackoffPolicy allows combinable configurations (e.g., fixedDelayInSeconds(10) + randomDelayInSeconds(0, 1)), which represents a 10-second fixed delay with an additional 0–1 second random jitter.
Find more examples here.