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 backoff
  • CircuitBreakerStrategy - Fail-fast when errors exceed threshold
  • TimeoutStrategy - Abort operations exceeding time limit
  • FallbackStrategy - Provide alternative result on failure
  • CompositeStrategy - 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 Classes
    Modifier and Type
    Interface
    Description
    static class 
    Builder for creating composite resilience strategies.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a composed strategy that first applies this strategy, then applies the given strategy.
    Returns a builder for creating resilience strategies.
    default void
    execute(Runnable operation)
    Executes the given runnable with resilience protection.
    <T> T
    execute(Callable<T> operation)
    Executes the given operation with resilience protection.
    default <T> T
    executeUnchecked(Supplier<T> operation)
    Executes the given operation with resilience protection.
    default boolean
    Checks if this strategy is currently healthy/available.
    default String
    Returns the name of this strategy for logging/monitoring.
    Returns a no-op strategy that executes operations directly.
    default void
    Resets the strategy state.
  • Method Details

    • execute

      <T> T execute(Callable<T> operation) throws Exception
      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

      default <T> T executeUnchecked(Supplier<T> operation)
      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

      default void execute(Runnable operation) throws Exception
      Executes the given runnable with resilience protection.
      Parameters:
      operation - the operation to execute
      Throws:
      Exception - if the operation fails after all resilience attempts
    • andThen

      default ResilienceStrategy andThen(ResilienceStrategy after)
      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

      default String 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

      static ResilienceStrategy.Builder builder()
      Returns a builder for creating resilience strategies.
      Returns:
      new builder instance
    • noOp

      static ResilienceStrategy noOp()
      Returns a no-op strategy that executes operations directly.
      Returns:
      no-op strategy