akka.pattern
Class CircuitBreaker

java.lang.Object
  extended by akka.pattern.CircuitBreaker

public class CircuitBreaker
extends java.lang.Object

Provides circuit breaker functionality to provide stability when working with "dangerous" operations, e.g. calls to remote systems

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


Field Summary
protected static long stateOffset
           
 
Constructor Summary
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)
           
 
Method Summary
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)
          Java API for withCircuitBreaker(scala.Function0>)
<T> T
callWithSyncCircuitBreaker(java.util.concurrent.Callable<T> body)
          Java API for withSyncCircuitBreaker(scala.Function0)
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.
 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
 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
<T> scala.concurrent.Future<T>
withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>> body)
          Wraps invocations of asynchronous calls that need to be protected
<T> T
withSyncCircuitBreaker(scala.Function0<T> body)
          Wraps invocations of synchronous calls that need to be protected
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stateOffset

protected static final long stateOffset
Constructor Detail

CircuitBreaker

public CircuitBreaker(Scheduler scheduler,
                      int maxFailures,
                      scala.concurrent.duration.FiniteDuration callTimeout,
                      scala.concurrent.duration.FiniteDuration resetTimeout,
                      scala.concurrent.ExecutionContext executor)

CircuitBreaker

public CircuitBreaker(scala.concurrent.ExecutionContext executor,
                      Scheduler scheduler,
                      int maxFailures,
                      scala.concurrent.duration.FiniteDuration callTimeout,
                      scala.concurrent.duration.FiniteDuration resetTimeout)
Method Detail

apply

public static CircuitBreaker apply(Scheduler scheduler,
                                   int maxFailures,
                                   scala.concurrent.duration.FiniteDuration callTimeout,
                                   scala.concurrent.duration.FiniteDuration resetTimeout)
Create a new CircuitBreaker.

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.

Parameters:
scheduler - Reference to Akka scheduler
maxFailures - Maximum number of failures before opening the circuit
callTimeout - FiniteDuration of time after which to consider a call a failure
resetTimeout - FiniteDuration of time after which to attempt to close the circuit
Returns:
(undocumented)

create

public static CircuitBreaker create(Scheduler scheduler,
                                    int maxFailures,
                                    scala.concurrent.duration.FiniteDuration callTimeout,
                                    scala.concurrent.duration.FiniteDuration resetTimeout)
Java API: Create a new CircuitBreaker.

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.

Parameters:
scheduler - Reference to Akka scheduler
maxFailures - Maximum number of failures before opening the circuit
callTimeout - FiniteDuration of time after which to consider a call a failure
resetTimeout - FiniteDuration of time after which to attempt to close the circuit
Returns:
(undocumented)

withCircuitBreaker

public <T> scala.concurrent.Future<T> withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>> body)
Wraps invocations of asynchronous calls that need to be protected

Parameters:
body - Call needing protected
Returns:
Future containing the call result

callWithCircuitBreaker

public <T> scala.concurrent.Future<T> callWithCircuitBreaker(java.util.concurrent.Callable<scala.concurrent.Future<T>> body)
Java API for withCircuitBreaker(scala.Function0>)

Parameters:
body - Call needing protected
Returns:
Future containing the call result

withSyncCircuitBreaker

public <T> T withSyncCircuitBreaker(scala.Function0<T> body)
Wraps invocations of synchronous calls that need to be protected

Calls are run in caller's thread

Parameters:
body - Call needing protected
Returns:
The result of the call

callWithSyncCircuitBreaker

public <T> T callWithSyncCircuitBreaker(java.util.concurrent.Callable<T> body)
Java API for withSyncCircuitBreaker(scala.Function0)

Parameters:
body - Call needing protected
Returns:
The result of the call

onOpen

public CircuitBreaker onOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when circuit breaker opens

The callback is run in the ExecutionContext supplied in the constructor.

Parameters:
callback - Handler to be invoked on state change
Returns:
CircuitBreaker for fluent usage

onOpen

public CircuitBreaker onOpen(java.lang.Runnable callback)
Java API for onOpen

Parameters:
callback - Handler to be invoked on state change
Returns:
CircuitBreaker for fluent usage

onHalfOpen

public 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.

Parameters:
callback - Handler to be invoked on state change
Returns:
CircuitBreaker for fluent usage

onHalfOpen

public CircuitBreaker onHalfOpen(java.lang.Runnable callback)
JavaAPI for onHalfOpen

Parameters:
callback - Handler to be invoked on state change
Returns:
CircuitBreaker for fluent usage

onClose

public CircuitBreaker onClose(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when circuit breaker state closes

The callback is run in the ExecutionContext supplied in the constructor.

Parameters:
callback - Handler to be invoked on state change
Returns:
CircuitBreaker for fluent usage

onClose

public CircuitBreaker onClose(java.lang.Runnable callback)
JavaAPI for onClose

Parameters:
callback - Handler to be invoked on state change
Returns:
CircuitBreaker for fluent usage

currentFailureCount

public int currentFailureCount()
Retrieves current failure count.

Returns:
count