Package dev.failsafe
Class Failsafe
- java.lang.Object
-
- dev.failsafe.Failsafe
-
public class Failsafe extends java.lang.ObjectSimple, sophisticated failure handling.
-
-
Constructor Summary
Constructors Constructor Description Failsafe()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <R> FailsafeExecutor<R>none()Creates and returns a noopFailsafeExecutorinstance that treats any exception as a failure for the purposes of calling event listeners, and provides no additional failure handling.static <R> FailsafeExecutor<R>with(java.util.List<? extends Policy<R>> policies)Creates and returns a newFailsafeExecutorinstance that will handle failures according to the givenpolicies.static <R,P extends Policy<R>>
FailsafeExecutor<R>with(P outerPolicy, P... policies)Creates and returns a newFailsafeExecutorinstance that will handle failures according to the givenouterPolicyandpolicies.
-
-
-
Method Detail
-
with
@SafeVarargs public static <R,P extends Policy<R>> FailsafeExecutor<R> with(P outerPolicy, P... policies)
Creates and returns a newFailsafeExecutorinstance that will handle failures according to the givenouterPolicyandpolicies. The policies are composed around an execution and will handle execution results in reverse, with the last policy being applied first. For example, consider:Failsafe.with(fallback, retryPolicy, circuitBreaker).get(supplier);
This is equivalent to composition using the the
composemethod:Failsafe.with(fallback).compose(retryPolicy).compose(circuitBreaker).get(supplier);
These result in the following internal composition when executing arunnableorsupplierand handling its result:Fallback(RetryPolicy(CircuitBreaker(Supplier)))
This means theCircuitBreakeris first to evaluate theSupplier's result, then theRetryPolicy, then theFallback. Each policy makes its own determination as to whether the result represents a failure. This allows different policies to be used for handling different types of failures.- Type Parameters:
R- result typeP- policy type- Throws:
java.lang.NullPointerException- ifouterPolicyis null
-
with
public static <R> FailsafeExecutor<R> with(java.util.List<? extends Policy<R>> policies)
Creates and returns a newFailsafeExecutorinstance that will handle failures according to the givenpolicies. Thepoliciesare composed around an execution and will handle execution results in reverse, with the last policy being applied first. For example, consider:Failsafe.with(Arrays.asList(fallback, retryPolicy, circuitBreaker)).get(supplier);
This results in the following internal composition when executing arunnableorsupplierand handling its result:Fallback(RetryPolicy(CircuitBreaker(Supplier)))
This means theCircuitBreakeris first to evaluate theSupplier's result, then theRetryPolicy, then theFallback. Each policy makes its own determination as to whether the result represents a failure. This allows different policies to be used for handling different types of failures.- Type Parameters:
R- result type- Throws:
java.lang.NullPointerException- ifpoliciesis nulljava.lang.IllegalArgumentException- ifpoliciesis empty
-
none
public static <R> FailsafeExecutor<R> none()
Creates and returns a noopFailsafeExecutorinstance that treats any exception as a failure for the purposes of calling event listeners, and provides no additional failure handling.- Type Parameters:
R- result type- Throws:
java.lang.NullPointerException- ifpoliciesis nulljava.lang.IllegalArgumentException- ifpoliciesis empty
-
-