public class CircuitBreaker
extends java.lang.Object
Transitions through three states:
- In *Closed* state, calls pass through until the maxFailures
count is reached. This causes the circuit breaker
to open. Both exceptions and calls exceeding callTimeout
are considered failures.
- In *Open* state, calls fail-fast with an exception. After resetTimeout
, circuit breaker transitions to
half-open state.
- In *Half-Open* state, the first call will be allowed through, if it succeeds the circuit breaker will reset to
closed state. If it fails, the circuit breaker will re-open to open state. All calls beyond the first that
execute while the first is running will fail-fast with an exception.
param: scheduler Reference to Akka scheduler
param: maxFailures Maximum number of failures before opening the circuit
param: callTimeout FiniteDuration
of time after which to consider a call a failure
param: resetTimeout FiniteDuration
of time after which to attempt to close the circuit
param: executor ExecutionContext
used for execution of state transition listeners
Modifier and Type | Field and Description |
---|---|
protected static long |
resetTimeoutOffset |
protected static long |
stateOffset |
Constructor and Description |
---|
CircuitBreaker(scala.concurrent.ExecutionContext executor,
Scheduler scheduler,
int maxFailures,
scala.concurrent.duration.FiniteDuration callTimeout,
scala.concurrent.duration.FiniteDuration resetTimeout) |
CircuitBreaker(Scheduler scheduler,
int maxFailures,
scala.concurrent.duration.FiniteDuration callTimeout,
scala.concurrent.duration.FiniteDuration resetTimeout,
scala.concurrent.ExecutionContext executor) |
CircuitBreaker(Scheduler scheduler,
int maxFailures,
scala.concurrent.duration.FiniteDuration callTimeout,
scala.concurrent.duration.FiniteDuration resetTimeout,
scala.concurrent.duration.FiniteDuration maxResetTimeout,
double exponentialBackoffFactor,
scala.concurrent.ExecutionContext executor) |
Modifier and Type | Method and Description |
---|---|
static CircuitBreaker |
apply(Scheduler scheduler,
int maxFailures,
scala.concurrent.duration.FiniteDuration callTimeout,
scala.concurrent.duration.FiniteDuration resetTimeout)
Create a new CircuitBreaker.
|
<T> scala.concurrent.Future<T> |
callWithCircuitBreaker(java.util.concurrent.Callable<scala.concurrent.Future<T>> body)
|
<T> java.util.concurrent.CompletionStage<T> |
callWithCircuitBreakerCS(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<T>> body)
Java API (8) for
withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>>) |
<T> T |
callWithSyncCircuitBreaker(java.util.concurrent.Callable<T> body)
Java API for
withSyncCircuitBreaker(scala.Function0<T>) . |
static CircuitBreaker |
create(Scheduler scheduler,
int maxFailures,
scala.concurrent.duration.FiniteDuration callTimeout,
scala.concurrent.duration.FiniteDuration resetTimeout)
Java API: Create a new CircuitBreaker.
|
int |
currentFailureCount()
Retrieves current failure count.
|
void |
fail()
Mark a failed call through CircuitBreaker.
|
boolean |
isClosed()
Return true if the internal state is Closed.
|
boolean |
isHalfOpen()
Return true if the internal state is HalfOpen.
|
boolean |
isOpen()
Return true if the internal state is Open.
|
CircuitBreaker |
onClose(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when circuit breaker state closes
|
CircuitBreaker |
onClose(java.lang.Runnable callback)
JavaAPI for onClose
|
CircuitBreaker |
onHalfOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when circuit breaker transitions to half-open
The callback is run in the
ExecutionContext supplied in the constructor. |
CircuitBreaker |
onHalfOpen(java.lang.Runnable callback)
JavaAPI for onHalfOpen
|
CircuitBreaker |
onOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when circuit breaker opens
|
CircuitBreaker |
onOpen(java.lang.Runnable callback)
Java API for onOpen
|
void |
succeed()
Mark a successful call through CircuitBreaker.
|
<T> scala.concurrent.Future<T> |
withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>> body)
Wraps invocations of asynchronous calls that need to be protected
|
CircuitBreaker |
withExponentialBackoff(scala.concurrent.duration.FiniteDuration maxResetTimeout)
The
resetTimeout will be increased exponentially for each failed attempt to close the circuit. |
<T> T |
withSyncCircuitBreaker(scala.Function0<T> body)
Wraps invocations of synchronous calls that need to be protected
|
protected static final long stateOffset
protected static final long resetTimeoutOffset
public CircuitBreaker(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout, scala.concurrent.duration.FiniteDuration maxResetTimeout, double exponentialBackoffFactor, scala.concurrent.ExecutionContext executor)
public CircuitBreaker(scala.concurrent.ExecutionContext executor, Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
public CircuitBreaker(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout, scala.concurrent.ExecutionContext executor)
public static CircuitBreaker apply(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
Callbacks run in caller's thread when using withSyncCircuitBreaker, and in same ExecutionContext as the passed in Future when using withCircuitBreaker. To use another ExecutionContext for the callbacks you can specify the executor in the constructor.
scheduler
- Reference to Akka schedulermaxFailures
- Maximum number of failures before opening the circuitcallTimeout
- FiniteDuration
of time after which to consider a call a failureresetTimeout
- FiniteDuration
of time after which to attempt to close the circuitpublic static CircuitBreaker create(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
Callbacks run in caller's thread when using withSyncCircuitBreaker, and in same ExecutionContext as the passed in Future when using withCircuitBreaker. To use another ExecutionContext for the callbacks you can specify the executor in the constructor.
scheduler
- Reference to Akka schedulermaxFailures
- Maximum number of failures before opening the circuitcallTimeout
- FiniteDuration
of time after which to consider a call a failureresetTimeout
- FiniteDuration
of time after which to attempt to close the circuitpublic CircuitBreaker withExponentialBackoff(scala.concurrent.duration.FiniteDuration maxResetTimeout)
resetTimeout
will be increased exponentially for each failed attempt to close the circuit.
The default exponential backoff factor is 2.
maxResetTimeout
- the upper bound of resetTimeoutpublic <T> scala.concurrent.Future<T> withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>> body)
body
- Call needing protectedFuture
containing the call result or a
scala.concurrent.TimeoutException
if the call timed out
public <T> scala.concurrent.Future<T> callWithCircuitBreaker(java.util.concurrent.Callable<scala.concurrent.Future<T>> body)
body
- Call needing protectedFuture
containing the call result or a
scala.concurrent.TimeoutException
if the call timed outpublic <T> java.util.concurrent.CompletionStage<T> callWithCircuitBreakerCS(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<T>> body)
withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>>)
body
- Call needing protectedCompletionStage
containing the call result or a
scala.concurrent.TimeoutException
if the call timed outpublic <T> T withSyncCircuitBreaker(scala.Function0<T> body)
Calls are run in caller's thread. Because of the synchronous nature of
this call the scala.concurrent.TimeoutException
will only be thrown
after the body has completed.
Throws java.util.concurrent.TimeoutException if the call timed out.
body
- Call needing protectedpublic <T> T callWithSyncCircuitBreaker(java.util.concurrent.Callable<T> body)
withSyncCircuitBreaker(scala.Function0<T>)
. Throws TimeoutException
if the call timed out.
body
- Call needing protectedpublic void succeed()
withCircuitBreaker
public void fail()
withCircuitBreaker
public boolean isClosed()
public boolean isOpen()
public boolean isHalfOpen()
public CircuitBreaker onOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
The callback is run in the ExecutionContext
supplied in the constructor.
callback
- Handler to be invoked on state changepublic CircuitBreaker onOpen(java.lang.Runnable callback)
callback
- Handler to be invoked on state changepublic CircuitBreaker onHalfOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
ExecutionContext
supplied in the constructor.
callback
- Handler to be invoked on state changepublic CircuitBreaker onHalfOpen(java.lang.Runnable callback)
callback
- Handler to be invoked on state changepublic CircuitBreaker onClose(scala.Function0<scala.runtime.BoxedUnit> callback)
The callback is run in the ExecutionContext
supplied in the constructor.
callback
- Handler to be invoked on state changepublic CircuitBreaker onClose(java.lang.Runnable callback)
callback
- Handler to be invoked on state changepublic int currentFailureCount()