public class CircuitBreaker extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
CircuitBreaker.State
The state of the circuit.
|
| Constructor and Description |
|---|
CircuitBreaker()
Creates a Circuit that opens after a single failure, closes after a single success, and has no delay by default.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
allowsExecution()
Returns whether the circuit allows execution, possibly triggering a state transition.
|
void |
close()
Closes the circuit.
|
<T> CircuitBreaker |
failIf(BiPredicate<T,? extends Throwable> completionPredicate)
Specifies that a failure should be recorded if the
completionPredicate matches the completion result. |
<T> CircuitBreaker |
failIf(Predicate<T> resultPredicate)
Specifies that a failure should be recorded if the
resultPredicate matches the result. |
CircuitBreaker |
failOn(Class<? extends Throwable>... failures)
Specifies the types to fail on.
|
CircuitBreaker |
failOn(List<Class<? extends Throwable>> failures)
Specifies the types to fail on.
|
CircuitBreaker |
failOn(Predicate<? extends Throwable> failurePredicate)
Specifies that a failure should be recorded if the
failurePredicate matches the failure. |
CircuitBreaker |
failWhen(Object result)
Specifies that a failure should be recorded if the execution result matches the
result. |
Duration |
getDelay()
Returns the delay before allowing another execution on the circuit.
|
Ratio |
getFailureThreshold()
Gets the ratio of successive failures that must occur when in a closed state in order to open the circuit else
null if none has been configured. |
CircuitBreaker.State |
getState()
Gets the state of the circuit.
|
Ratio |
getSuccessThreshold()
Gets the ratio of successive successful executions that must occur when in a half-open state in order to close the
circuit else
null if none has been configured. |
Duration |
getTimeout()
Returns timeout for executions else
null if none has been configured. |
void |
halfOpen()
Half-opens the circuit.
|
boolean |
isClosed()
Returns whether the circuit is closed.
|
boolean |
isFailure(Object result,
Throwable failure)
Returns whether the circuit breaker considers the
result or throwable a failure based on the
configured conditions, or if failure is not null it is not checked by any configured condition. |
boolean |
isHalfOpen()
Returns whether the circuit is half open.
|
boolean |
isOpen()
Returns whether the circuit is open.
|
void |
onClose(CheckedRunnable runnable)
Calls the
runnable when the circuit is closed. |
void |
onHalfOpen(CheckedRunnable runnable)
Calls the
runnable when the circuit is half-opened. |
void |
onOpen(CheckedRunnable runnable)
Calls the
runnable when the circuit is opened. |
void |
open()
Opens the circuit.
|
void |
recordFailure(Throwable failure)
Records an execution
failure as a success or failure based on the failure configuration as determined by
isFailure(Object, Throwable). |
void |
recordResult(Object result)
Records an execution
result as a success or failure based on the failure configuration as determined by
isFailure(Object, Throwable). |
void |
recordSuccess()
Records an execution success.
|
String |
toString() |
CircuitBreaker |
withDelay(long delay,
TimeUnit timeUnit)
Sets the
delay to wait in open state before transitioning to half-open. |
CircuitBreaker |
withFailureThreshold(int failureThreshold)
Sets the number of successive failures that must occur when in a closed state in order to open the circuit.
|
CircuitBreaker |
withFailureThreshold(int failures,
int executions)
Sets the ratio of successive failures that must occur when in a closed state in order to open the circuit.
|
CircuitBreaker |
withSuccessThreshold(int successThreshold)
Sets the number of successive successful executions that must occur when in a half-open state in order to close the
circuit, else the circuit is re-opened when a failure occurs.
|
CircuitBreaker |
withSuccessThreshold(int successes,
int executions)
Sets the ratio of successive successful executions that must occur when in a half-open state in order to close the
circuit.
|
CircuitBreaker |
withTimeout(long timeout,
TimeUnit timeUnit)
Sets the
timeout for executions. |
public CircuitBreaker()
public boolean allowsExecution()
public void close()
public <T> CircuitBreaker failIf(BiPredicate<T,? extends Throwable> completionPredicate)
completionPredicate matches the completion result.NullPointerException - if completionPredicate is nullpublic <T> CircuitBreaker failIf(Predicate<T> resultPredicate)
resultPredicate matches the result.NullPointerException - if resultPredicate is nullpublic CircuitBreaker failOn(Class<? extends Throwable>... failures)
failures.NullPointerException - if failures is nullIllegalArgumentException - if failures is emptypublic CircuitBreaker failOn(List<Class<? extends Throwable>> failures)
failures.NullPointerException - if failures is nullIllegalArgumentException - if failures is emptypublic CircuitBreaker failOn(Predicate<? extends Throwable> failurePredicate)
failurePredicate matches the failure.NullPointerException - if failurePredicate is nullpublic CircuitBreaker failWhen(Object result)
result.public Duration getDelay()
Duration.NONE.withDelay(long, TimeUnit)public Ratio getFailureThreshold()
null if none has been configured.public CircuitBreaker.State getState()
public Ratio getSuccessThreshold()
null if none has been configured.public Duration getTimeout()
null if none has been configured.withTimeout(long, TimeUnit)public void halfOpen()
public boolean isClosed()
public boolean isFailure(Object result, Throwable failure)
result or throwable a failure based on the
configured conditions, or if failure is not null it is not checked by any configured condition.public boolean isHalfOpen()
public boolean isOpen()
public void onClose(CheckedRunnable runnable)
runnable when the circuit is closed.public void onHalfOpen(CheckedRunnable runnable)
runnable when the circuit is half-opened.public void onOpen(CheckedRunnable runnable)
runnable when the circuit is opened.public void open()
public void recordFailure(Throwable failure)
failure as a success or failure based on the failure configuration as determined by
isFailure(Object, Throwable).isFailure(Object, Throwable)public void recordResult(Object result)
result as a success or failure based on the failure configuration as determined by
isFailure(Object, Throwable).isFailure(Object, Throwable)public void recordSuccess()
public CircuitBreaker withDelay(long delay, TimeUnit timeUnit)
delay to wait in open state before transitioning to half-open.NullPointerException - if timeUnit is nullIllegalArgumentException - if delay <= 0public CircuitBreaker withFailureThreshold(int failureThreshold)
IllegalArgumentException - if failureThresh < 1public CircuitBreaker withFailureThreshold(int failures, int executions)
executions executions have taken place.failures - The number of failures that must occur in order to open the circuitexecutions - The number of executions to measure the failures againstIllegalArgumentException - if failures < 1, executions < 1, or failures is <
executionspublic CircuitBreaker withSuccessThreshold(int successThreshold)
IllegalArgumentException - if successThreshold < 1public CircuitBreaker withSuccessThreshold(int successes, int executions)
executions executions have taken place.successes - The number of successful executions that must occur in order to open the circuitexecutions - The number of executions to measure the successes againstIllegalArgumentException - if successes < 1, executions < 1, or successes is <
executionspublic CircuitBreaker withTimeout(long timeout, TimeUnit timeUnit)
timeout for executions. Executions that exceed this timeout are not interrupted, but are recorded
as failures once they naturally complete.NullPointerException - if timeUnit is nullIllegalArgumentException - if timeout <= 0Copyright © 2017. All Rights Reserved.