Class Policy<T>
java.lang.Object
io.github.teceli.resiliencia.compose.Policy<T>
- All Implemented Interfaces:
Resilient<T>
Fluent composition of multiple resilience patterns.
The first pattern passed to
compose(io.github.teceli.resiliencia.core.api.Resilient<T>) is the outermost layer, invoked first.
Each pattern added via and(io.github.teceli.resiliencia.core.api.Resilient<T>) becomes the new innermost layer, closer to the operation
than every pattern added before it.
Example: Policy.compose(retry).call(op) executes op with retry protection.
Example: Policy.compose(circuitBreaker).and(retry).call(op) checks the circuit breaker first;
retry sits inside it, closest to the operation.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.teceli.resiliencia.core.api.Resilient
Resilient.Operation<T> -
Method Summary
Modifier and TypeMethodDescriptionAdd another pattern to this policy.call(Resilient.Operation<T> operation) Execute the operation through the pattern chain on the calling thread, blocking until complete.static <T> Policy<T> Create a policy with a single pattern.booleanTrue if any pattern in this chain has its own deadline, checked recursively through nestedPolicypatterns.outcome(Resilient.Operation<T> operation) Execute and capture outcome (never throws).static <T> Policy<T> useOptimumOrder(Resilient<T>... patterns) Create a policy from the given patterns, composed in the library's optimum order regardless of the order they are passed in — outermost to innermost: RateLimiter, CircuitBreaker, Bulkhead, Retry, Timeout.withListener(ResilienceEvent.Listener listener) Add a listener notified of everyPolicyValidationWarningemitted by this instance.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.github.teceli.resiliencia.core.api.Resilient
callAsync, patternKind, patternName
-
Method Details
-
compose
Create a policy with a single pattern. This pattern becomes the outermost layer. -
and
Add another pattern to this policy. The new pattern becomes the innermost layer, closest to the operation; patterns added earlier stay further out. The new pattern is checked against every pattern already in the chain (transitively, not just the adjacent one) for known-bad orderings: Retry wrapping CircuitBreaker is rejected withInvalidPolicyException; Timeout wrapping Retry logs a WARN but proceeds, also emitting aPolicyValidationWarningto any listener already attached viawithListener(ResilienceEvent.Listener).- Throws:
InvalidPolicyException- if the resulting ordering is known to be broken at runtime
-
withListener
Add a listener notified of everyPolicyValidationWarningemitted by this instance. Only observes warnings raised byand(Resilient)calls made after this listener was attached —useOptimumOrder(Resilient[])builds its entire chain in one call with no opportunity to attach a listener mid-build, so any warning it triggers is only visible via the SLF4J log, which always fires regardless. Listener exceptions are logged and otherwise ignored — a broken listener never affects the outcome. -
useOptimumOrder
Create a policy from the given patterns, composed in the library's optimum order regardless of the order they are passed in — outermost to innermost: RateLimiter, CircuitBreaker, Bulkhead, Retry, Timeout. The result is a plainPolicy, identical to what the equivalentcompose(...).and(...)chain produces, and goes through the same ordering guardrail. The sort is stable: patterns of the same kind keep the relative order they were passed in. -
hasOwnDeadline
public boolean hasOwnDeadline()True if any pattern in this chain has its own deadline, checked recursively through nestedPolicypatterns. Without this override, a Retry configured withwithOverallDeadline(...)but nested inside a sub-Policywould reportfalsehere (theResilientdefault), silently defeating the Timeout-wraps-Retry WARN suppression that the same Retry would get in a flat chain.- Specified by:
hasOwnDeadlinein interfaceResilient<T>
-
call
Execute the operation through the pattern chain on the calling thread, blocking until complete. Returns the result, or throws whichever exception the innermost failing pattern throws. Policy propagates RuntimeExceptions (including all ResilientException subtypes) as-is, without wrapping. For Throwable types that are not RuntimeException (e.g., Error), wraps in ResilientException as a safety net.- Specified by:
callin interfaceResilient<T>- Throws:
ResilientException- if the operation fails after passing through the pattern chain
-
outcome
Execute and capture outcome (never throws). Chains all patterns so each wraps the operation before execution.
-