Class CircuitBreaker
- java.lang.Object
-
- akka.pattern.CircuitBreaker
-
public class CircuitBreaker extends java.lang.Object
TheresetTimeout
will be increased exponentially for each failed attempt to close the circuit. The default exponential backoff factor is 2.param: maxResetTimeout the upper bound of resetTimeout
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
CircuitBreaker.State
Add a listener function which is invoked on state entry
-
Field Summary
Fields Modifier and Type Field Description protected static long
resetTimeoutOffset
protected static long
stateOffset
-
Constructor Summary
Constructors Constructor Description 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)
CircuitBreaker(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout, scala.concurrent.ExecutionContext executor)
CircuitBreaker(scala.concurrent.ExecutionContext executor, Scheduler scheduler, int maxFailures, java.time.Duration callTimeout, java.time.Duration resetTimeout)
CircuitBreaker(scala.concurrent.ExecutionContext executor, Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
Deprecated.Use the overloaded one which accepts java.time.Duration instead.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description CircuitBreaker
addOnCallBreakerOpenListener(java.lang.Runnable callback)
JavaAPI for onCallBreakerOpen.CircuitBreaker
addOnCallFailureListener(java.util.function.Consumer<java.lang.Object> callback)
JavaAPI for onCallFailureCircuitBreaker
addOnCallSuccessListener(java.util.function.Consumer<java.lang.Object> callback)
JavaAPI for onCallSuccessCircuitBreaker
addOnCallTimeoutListener(java.util.function.Consumer<java.lang.Object> callback)
JavaAPI for onCallTimeoutCircuitBreaker
addOnCloseListener(java.lang.Runnable callback)
JavaAPI for onCloseCircuitBreaker
addOnHalfOpenListener(java.lang.Runnable callback)
JavaAPI for onHalfOpenCircuitBreaker
addOnOpenListener(java.lang.Runnable callback)
Java API for onOpenstatic CircuitBreaker
apply(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
<T> scala.concurrent.Future<T>
callWithCircuitBreaker(java.util.concurrent.Callable<scala.concurrent.Future<T>> body)
<T> scala.concurrent.Future<T>
callWithCircuitBreaker(java.util.concurrent.Callable<scala.concurrent.Future<T>> body, java.util.function.BiFunction<java.util.Optional<T>,java.util.Optional<java.lang.Throwable>,java.lang.Boolean> defineFailureFn)
<T> java.util.concurrent.CompletionStage<T>
callWithCircuitBreakerCS(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<T>> body)
<T> java.util.concurrent.CompletionStage<T>
callWithCircuitBreakerCS(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<T>> body, java.util.function.BiFunction<java.util.Optional<T>,java.util.Optional<java.lang.Throwable>,java.lang.Boolean> defineFailureFn)
<T> T
callWithSyncCircuitBreaker(java.util.concurrent.Callable<T> body)
Java API forwithSyncCircuitBreaker(scala.Function0<T>)
.<T> T
callWithSyncCircuitBreaker(java.util.concurrent.Callable<T> body, java.util.function.BiFunction<java.util.Optional<T>,java.util.Optional<java.lang.Throwable>,java.lang.Boolean> defineFailureFn)
Java API forwithSyncCircuitBreaker(scala.Function0<T>)
.protected static <T> scala.Function1<scala.util.Try<T>,java.lang.Object>
convertJavaFailureFnToScala(java.util.function.BiFunction<java.util.Optional<T>,java.util.Optional<java.lang.Throwable>,java.lang.Boolean> javaFn)
static CircuitBreaker
create(Scheduler scheduler, int maxFailures, java.time.Duration callTimeout, java.time.Duration resetTimeout)
Java API: Create a new CircuitBreaker.static CircuitBreaker
create(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
Deprecated.Use the overloaded one which accepts java.time.Duration instead.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
onCallBreakerOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when call was failed due to open breaker.CircuitBreaker
onCallFailure(scala.Function1<java.lang.Object,scala.runtime.BoxedUnit> callback)
Adds a callback to execute when call finished with failure.CircuitBreaker
onCallSuccess(scala.Function1<java.lang.Object,scala.runtime.BoxedUnit> callback)
Adds a callback to execute when call finished with success.CircuitBreaker
onCallTimeout(scala.Function1<java.lang.Object,scala.runtime.BoxedUnit> callback)
Adds a callback to execute when call finished with timeout.CircuitBreaker
onClose(java.lang.Runnable callback)
Deprecated.Use addOnCloseListener instead.CircuitBreaker
onClose(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when circuit breaker state closesCircuitBreaker
onHalfOpen(java.lang.Runnable callback)
Deprecated.Use addOnHalfOpenListener instead.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 theExecutionContext
supplied in the constructor.CircuitBreaker
onOpen(java.lang.Runnable callback)
Deprecated.Use addOnOpenListener instead.CircuitBreaker
onOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when circuit breaker opensscala.concurrent.duration.FiniteDuration
resetTimeout()
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<T> scala.concurrent.Future<T>
withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>> body, scala.Function1<scala.util.Try<T>,java.lang.Object> defineFailureFn)
Wraps invocations of asynchronous calls that need to be protectedCircuitBreaker
withExponentialBackoff(java.time.Duration maxResetTimeout)
TheresetTimeout
will be increased exponentially for each failed attempt to close the circuit.CircuitBreaker
withExponentialBackoff(scala.concurrent.duration.FiniteDuration maxResetTimeout)
TheresetTimeout
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<T> T
withSyncCircuitBreaker(scala.Function0<T> body, scala.Function1<scala.util.Try<T>,java.lang.Object> defineFailureFn)
Wraps invocations of synchronous calls that need to be protected
-
-
-
Constructor Detail
-
CircuitBreaker
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)
-
CircuitBreaker
public CircuitBreaker(scala.concurrent.ExecutionContext executor, Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
Deprecated.Use the overloaded one which accepts java.time.Duration instead. Since 2.5.12.
-
CircuitBreaker
public CircuitBreaker(scala.concurrent.ExecutionContext executor, Scheduler scheduler, int maxFailures, java.time.Duration callTimeout, java.time.Duration resetTimeout)
-
CircuitBreaker
public CircuitBreaker(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout, scala.concurrent.ExecutionContext executor)
-
-
Method Detail
-
apply
public static CircuitBreaker apply(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
-
create
public static CircuitBreaker create(Scheduler scheduler, int maxFailures, scala.concurrent.duration.FiniteDuration callTimeout, scala.concurrent.duration.FiniteDuration resetTimeout)
Deprecated.Use the overloaded one which accepts java.time.Duration instead. Since 2.5.12.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 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 circuit- Returns:
- (undocumented)
-
create
public static CircuitBreaker create(Scheduler scheduler, int maxFailures, java.time.Duration callTimeout, java.time.Duration 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 schedulermaxFailures
- Maximum number of failures before opening the circuitcallTimeout
-Duration
of time after which to consider a call a failureresetTimeout
-Duration
of time after which to attempt to close the circuit- Returns:
- (undocumented)
-
convertJavaFailureFnToScala
protected static <T> scala.Function1<scala.util.Try<T>,java.lang.Object> convertJavaFailureFnToScala(java.util.function.BiFunction<java.util.Optional<T>,java.util.Optional<java.lang.Throwable>,java.lang.Boolean> javaFn)
-
resetTimeout
public scala.concurrent.duration.FiniteDuration resetTimeout()
-
withExponentialBackoff
public CircuitBreaker withExponentialBackoff(scala.concurrent.duration.FiniteDuration maxResetTimeout)
TheresetTimeout
will be increased exponentially for each failed attempt to close the circuit. The default exponential backoff factor is 2.- Parameters:
maxResetTimeout
- the upper bound of resetTimeout- Returns:
- (undocumented)
-
withExponentialBackoff
public CircuitBreaker withExponentialBackoff(java.time.Duration maxResetTimeout)
TheresetTimeout
will be increased exponentially for each failed attempt to close the circuit. The default exponential backoff factor is 2.- Parameters:
maxResetTimeout
- the upper bound of resetTimeout- Returns:
- (undocumented)
-
withCircuitBreaker
public <T> scala.concurrent.Future<T> withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>> body, scala.Function1<scala.util.Try<T>,java.lang.Object> defineFailureFn)
Wraps invocations of asynchronous calls that need to be protected- Parameters:
body
- Call needing protecteddefineFailureFn
- function that define what should be consider failure and thus increase failure count- Returns:
Future
containing the call result or ascala.concurrent.TimeoutException
if the call timed out
-
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 or ascala.concurrent.TimeoutException
if the call timed out
-
callWithCircuitBreaker
public <T> scala.concurrent.Future<T> callWithCircuitBreaker(java.util.concurrent.Callable<scala.concurrent.Future<T>> body)
Java API forwithCircuitBreaker(scala.Function0<scala.concurrent.Future<T>>, scala.Function1<scala.util.Try<T>, java.lang.Object>)
- Parameters:
body
- Call needing protected- Returns:
Future
containing the call result or ascala.concurrent.TimeoutException
if the call timed out
-
callWithCircuitBreaker
public <T> scala.concurrent.Future<T> callWithCircuitBreaker(java.util.concurrent.Callable<scala.concurrent.Future<T>> body, java.util.function.BiFunction<java.util.Optional<T>,java.util.Optional<java.lang.Throwable>,java.lang.Boolean> defineFailureFn)
Java API forwithCircuitBreaker(scala.Function0<scala.concurrent.Future<T>>, scala.Function1<scala.util.Try<T>, java.lang.Object>)
- Parameters:
body
- Call needing protecteddefineFailureFn
- function that define what should be consider failure and thus increase failure count- Returns:
Future
containing the call result or ascala.concurrent.TimeoutException
if the call timed out
-
callWithCircuitBreakerCS
public <T> java.util.concurrent.CompletionStage<T> callWithCircuitBreakerCS(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<T>> body)
Java API (8) forwithCircuitBreaker(scala.Function0<scala.concurrent.Future<T>>, scala.Function1<scala.util.Try<T>, java.lang.Object>)
- Parameters:
body
- Call needing protected- Returns:
CompletionStage
containing the call result or ascala.concurrent.TimeoutException
if the call timed out
-
callWithCircuitBreakerCS
public <T> java.util.concurrent.CompletionStage<T> callWithCircuitBreakerCS(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<T>> body, java.util.function.BiFunction<java.util.Optional<T>,java.util.Optional<java.lang.Throwable>,java.lang.Boolean> defineFailureFn)
Java API (8) forwithCircuitBreaker(scala.Function0<scala.concurrent.Future<T>>, scala.Function1<scala.util.Try<T>, java.lang.Object>)
- Parameters:
body
- Call needing protecteddefineFailureFn
- function that define what should be consider failure and thus increase failure count- Returns:
CompletionStage
containing the call result or ascala.concurrent.TimeoutException
if the call timed out
-
withSyncCircuitBreaker
public <T> T withSyncCircuitBreaker(scala.Function0<T> body)
Wraps invocations of synchronous calls that need to be protectedCalls 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.
- Parameters:
body
- Call needing protected- Returns:
- The result of the call
-
withSyncCircuitBreaker
public <T> T withSyncCircuitBreaker(scala.Function0<T> body, scala.Function1<scala.util.Try<T>,java.lang.Object> defineFailureFn)
Wraps invocations of synchronous calls that need to be protectedCalls 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.
- Parameters:
body
- Call needing protecteddefineFailureFn
- function that define what should be consider failure and thus increase failure count- Returns:
- The result of the call
-
callWithSyncCircuitBreaker
public <T> T callWithSyncCircuitBreaker(java.util.concurrent.Callable<T> body)
Java API forwithSyncCircuitBreaker(scala.Function0<T>)
. ThrowsTimeoutException
if the call timed out.- Parameters:
body
- Call needing protected- Returns:
- The result of the call
-
callWithSyncCircuitBreaker
public <T> T callWithSyncCircuitBreaker(java.util.concurrent.Callable<T> body, java.util.function.BiFunction<java.util.Optional<T>,java.util.Optional<java.lang.Throwable>,java.lang.Boolean> defineFailureFn)
Java API forwithSyncCircuitBreaker(scala.Function0<T>)
. ThrowsTimeoutException
if the call timed out.- Parameters:
body
- Call needing protecteddefineFailureFn
- function that define what should be consider failure and thus increase failure count- Returns:
- The result of the call
-
succeed
public void succeed()
Mark a successful call through CircuitBreaker. Sometimes the callee of CircuitBreaker sends back a message to the caller Actor. In such a case, it is convenient to mark a successful call instead of using Future via<T>withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>>,scala.Function1<scala.util.Try<T>,java.lang.Object>)
-
fail
public void fail()
Mark a failed call through CircuitBreaker. Sometimes the callee of CircuitBreaker sends back a message to the caller Actor. In such a case, it is convenient to mark a failed call instead of using Future via<T>withCircuitBreaker(scala.Function0<scala.concurrent.Future<T>>,scala.Function1<scala.util.Try<T>,java.lang.Object>)
-
isClosed
public boolean isClosed()
Return true if the internal state is Closed. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in withCircuitBreaker. So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.- Returns:
- (undocumented)
-
isOpen
public boolean isOpen()
Return true if the internal state is Open. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in withCircuitBreaker. So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.- Returns:
- (undocumented)
-
isHalfOpen
public boolean isHalfOpen()
Return true if the internal state is HalfOpen. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in withCircuitBreaker. So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.- Returns:
- (undocumented)
-
onOpen
public CircuitBreaker onOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when circuit breaker opensThe 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)
Deprecated.Use addOnOpenListener instead. Since 2.5.0.Java API for onOpen- Parameters:
callback
- Handler to be invoked on state change- Returns:
- CircuitBreaker for fluent usage
-
addOnOpenListener
public CircuitBreaker addOnOpenListener(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 theExecutionContext
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)
Deprecated.Use addOnHalfOpenListener instead. Since 2.5.0.JavaAPI for onHalfOpen- Parameters:
callback
- Handler to be invoked on state change- Returns:
- CircuitBreaker for fluent usage
-
addOnHalfOpenListener
public CircuitBreaker addOnHalfOpenListener(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 closesThe 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)
Deprecated.Use addOnCloseListener instead. Since 2.5.0.JavaAPI for onClose- Parameters:
callback
- Handler to be invoked on state change- Returns:
- CircuitBreaker for fluent usage
-
addOnCloseListener
public CircuitBreaker addOnCloseListener(java.lang.Runnable callback)
JavaAPI for onClose- Parameters:
callback
- Handler to be invoked on state change- Returns:
- CircuitBreaker for fluent usage
-
onCallSuccess
public CircuitBreaker onCallSuccess(scala.Function1<java.lang.Object,scala.runtime.BoxedUnit> callback)
Adds a callback to execute when call finished with success.The callback is run in the
ExecutionContext
supplied in the constructor.- Parameters:
callback
- Handler to be invoked on successful call, where passed value is elapsed time in nanoseconds.- Returns:
- CircuitBreaker for fluent usage
-
addOnCallSuccessListener
public CircuitBreaker addOnCallSuccessListener(java.util.function.Consumer<java.lang.Object> callback)
JavaAPI for onCallSuccess- Parameters:
callback
- Handler to be invoked on successful call, where passed value is elapsed time in nanoseconds.- Returns:
- CircuitBreaker for fluent usage
-
onCallFailure
public CircuitBreaker onCallFailure(scala.Function1<java.lang.Object,scala.runtime.BoxedUnit> callback)
Adds a callback to execute when call finished with failure.The callback is run in the
ExecutionContext
supplied in the constructor.- Parameters:
callback
- Handler to be invoked on failed call, where passed value is elapsed time in nanoseconds.- Returns:
- CircuitBreaker for fluent usage
-
addOnCallFailureListener
public CircuitBreaker addOnCallFailureListener(java.util.function.Consumer<java.lang.Object> callback)
JavaAPI for onCallFailure- Parameters:
callback
- Handler to be invoked on failed call, where passed value is elapsed time in nanoseconds.- Returns:
- CircuitBreaker for fluent usage
-
onCallTimeout
public CircuitBreaker onCallTimeout(scala.Function1<java.lang.Object,scala.runtime.BoxedUnit> callback)
Adds a callback to execute when call finished with timeout.The callback is run in the
ExecutionContext
supplied in the constructor.- Parameters:
callback
- Handler to be invoked on call finished with timeout, where passed value is elapsed time in nanoseconds.- Returns:
- CircuitBreaker for fluent usage
-
addOnCallTimeoutListener
public CircuitBreaker addOnCallTimeoutListener(java.util.function.Consumer<java.lang.Object> callback)
JavaAPI for onCallTimeout- Parameters:
callback
- Handler to be invoked on call finished with timeout, where passed value is elapsed time in nanoseconds.- Returns:
- CircuitBreaker for fluent usage
-
onCallBreakerOpen
public CircuitBreaker onCallBreakerOpen(scala.Function0<scala.runtime.BoxedUnit> callback)
Adds a callback to execute when call was failed due to open breaker.The callback is run in the
ExecutionContext
supplied in the constructor.- Parameters:
callback
- Handler to be invoked on call failed due to open breaker.- Returns:
- CircuitBreaker for fluent usage
-
addOnCallBreakerOpenListener
public CircuitBreaker addOnCallBreakerOpenListener(java.lang.Runnable callback)
JavaAPI for onCallBreakerOpen.- Parameters:
callback
- Handler to be invoked on call failed due to open breaker.- Returns:
- CircuitBreaker for fluent usage
-
currentFailureCount
public int currentFailureCount()
Retrieves current failure count.- Returns:
- count
-
-