Package com.tnsai.spi
Interface ResilienceStrategy
public interface ResilienceStrategy
Strategy interface for resilience patterns.
This interface provides a unified abstraction for various resilience patterns including retry, circuit breaker, timeout, and fallback. Implementations can be composed to create complex resilience strategies.
Built-in Implementations
RetryStrategy- Retry with exponential backoffCircuitBreakerStrategy- Fail-fast when errors exceed thresholdTimeoutStrategy- Abort operations exceeding time limitFallbackStrategy- Provide alternative result on failureCompositeStrategy- Combine multiple strategies
Usage
ResilienceStrategy strategy = ResilienceStrategy.builder()
.retry(3, Duration.ofMillis(500))
.circuitBreaker(5, Duration.ofSeconds(30))
.timeout(Duration.ofSeconds(10))
.fallback(() -> "default")
.build();
String result = strategy.execute(() -> riskyOperation());
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classBuilder for creating composite resilience strategies. -
Method Summary
Modifier and TypeMethodDescriptiondefault ResilienceStrategyandThen(ResilienceStrategy after) Returns a composed strategy that first applies this strategy, then applies the given strategy.static ResilienceStrategy.Builderbuilder()Returns a builder for creating resilience strategies.default voidExecutes the given runnable with resilience protection.<T> TExecutes the given operation with resilience protection.default <T> TexecuteUnchecked(Supplier<T> operation) Executes the given operation with resilience protection.default booleanChecks if this strategy is currently healthy/available.default Stringname()Returns the name of this strategy for logging/monitoring.static ResilienceStrategynoOp()Returns a no-op strategy that executes operations directly.default voidreset()Resets the strategy state.
-
Method Details
-
execute
Executes the given operation with resilience protection.- Type Parameters:
T- result type- Parameters:
operation- the operation to execute- Returns:
- the result of the operation
- Throws:
Exception- if the operation fails after all resilience attempts
-
executeUnchecked
Executes the given operation with resilience protection.- Type Parameters:
T- result type- Parameters:
operation- the operation to execute- Returns:
- the result of the operation
- Throws:
RuntimeException- if the operation fails after all resilience attempts
-
execute
Executes the given runnable with resilience protection.- Parameters:
operation- the operation to execute- Throws:
Exception- if the operation fails after all resilience attempts
-
andThen
Returns a composed strategy that first applies this strategy, then applies the given strategy.- Parameters:
after- the strategy to apply after this one- Returns:
- composed strategy
-
name
Returns the name of this strategy for logging/monitoring.- Returns:
- strategy name
-
isHealthy
default boolean isHealthy()Checks if this strategy is currently healthy/available.For circuit breakers, this returns false when the circuit is open.
- Returns:
- true if the strategy allows execution
-
reset
default void reset()Resets the strategy state.For circuit breakers, this closes the circuit. For retry strategies, this resets attempt counters.
-
builder
Returns a builder for creating resilience strategies.- Returns:
- new builder instance
-
noOp
Returns a no-op strategy that executes operations directly.- Returns:
- no-op strategy
-