Interface CircuitBreaker
-
public interface CircuitBreakerAn implementation of the circuit breaker pattern for Vert.x- Author:
- Clement Escoffier
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description CircuitBreakerclose()Closes the circuit breaker.CircuitBreakercloseHandler(Handler<Void> handler)Sets aHandlerto be invoked when the circuit breaker state switches to closed.static CircuitBreakercreate(String name, Vertx vertx)Creates a new instance ofCircuitBreaker, with default options.static CircuitBreakercreate(String name, Vertx vertx, CircuitBreakerOptions options)Creates a new instance ofCircuitBreaker.<T> Future<T>execute(Handler<Promise<T>> command)Same asexecuteWithFallback(Handler, Function)but using the circuit breaker default fallback.<T> Future<T>execute(Supplier<Future<T>> command)Same asexecuteWithFallback(Supplier, Function)but using the circuit breaker default fallback.<T> CircuitBreakerexecuteAndReport(Promise<T> resultPromise, Handler<Promise<T>> command)Same asexecuteAndReportWithFallback(Promise, Handler, Function)but using the circuit breaker default fallback.<T> CircuitBreakerexecuteAndReportWithFallback(Promise<T> resultPromise, Handler<Promise<T>> command, Function<Throwable,T> fallback)Executes the given operation with the circuit breaker control.<T> Future<T>executeWithFallback(Handler<Promise<T>> command, Function<Throwable,T> fallback)Executes the given operation with the circuit breaker control.<T> Future<T>executeWithFallback(Supplier<Future<T>> command, Function<Throwable,T> fallback)Executes the given operation with the circuit breaker control.longfailureCount()default <T> CircuitBreakerfailurePolicy(FailurePolicy<T> failurePolicy)Configures the failure policy for this circuit-breaker.<T> CircuitBreakerfallback(Function<Throwable,T> handler)Sets a default fallbackFunctionto be invoked when the circuit breaker is open or when failure occurs andCircuitBreakerOptions.isFallbackOnFailure()is enabled.CircuitBreakerhalfOpenHandler(Handler<Void> handler)Sets aHandlerto be invoked when the circuit breaker state switches to half-open.Stringname()CircuitBreakeropen()Explicitly opens the circuit breaker.CircuitBreakeropenHandler(Handler<Void> handler)Sets aHandlerto be invoked when the circuit breaker state switches to open.CircuitBreakerreset()Resets the circuit breaker state.CircuitBreakerretryPolicy(RetryPolicy retryPolicy)Set aRetryPolicywhich computes a delay before a retry attempt.CircuitBreakerStatestate()
-
-
-
Method Detail
-
create
static CircuitBreaker create(String name, Vertx vertx, CircuitBreakerOptions options)
Creates a new instance ofCircuitBreaker.- Parameters:
name- the namevertx- the Vert.x instanceoptions- the configuration options- Returns:
- the created instance
-
create
static CircuitBreaker create(String name, Vertx vertx)
Creates a new instance ofCircuitBreaker, with default options.- Parameters:
name- the namevertx- the Vert.x instance- Returns:
- the created instance
-
close
CircuitBreaker close()
Closes the circuit breaker. It stops sending events on its state on the event bus.This method is not related to the
closedstate of the circuit breaker. To move the circuit breaker to theclosedstate, usereset().
-
openHandler
CircuitBreaker openHandler(Handler<Void> handler)
Sets aHandlerto be invoked when the circuit breaker state switches to open.- Parameters:
handler- the handler, must not benull- Returns:
- this
CircuitBreaker
-
halfOpenHandler
CircuitBreaker halfOpenHandler(Handler<Void> handler)
Sets aHandlerto be invoked when the circuit breaker state switches to half-open.- Parameters:
handler- the handler, must not benull- Returns:
- this
CircuitBreaker
-
closeHandler
CircuitBreaker closeHandler(Handler<Void> handler)
Sets aHandlerto be invoked when the circuit breaker state switches to closed.- Parameters:
handler- the handler, must not benull- Returns:
- this
CircuitBreaker
-
executeWithFallback
<T> Future<T> executeWithFallback(Handler<Promise<T>> command, Function<Throwable,T> fallback)
Executes the given operation with the circuit breaker control. The operation is generally calling an external system. The operation receives aPromiseobject as parameter and must callPromise.complete(Object)when the operation has terminated successfully. The operation must also callPromise.fail(Throwable)in case of a failure.The operation is not invoked if the circuit breaker is open, and the given fallback is called instead. The circuit breaker also monitors whether the operation completes in time. The operation is considered failed if it does not terminate before the configured timeout.
This method returns a
Futureobject to retrieve the status and result of the operation, with the status being a success or a failure. If the fallback is called, the returned future is successfully completed with the value returned from the fallback. If the fallback throws an exception, the returned future is marked as failed.- Type Parameters:
T- the type of result- Parameters:
command- the operationfallback- the fallback function; gets an exception as parameter and returns the fallback result- Returns:
- a future object completed when the operation or the fallback completes
-
executeWithFallback
<T> Future<T> executeWithFallback(Supplier<Future<T>> command, Function<Throwable,T> fallback)
Executes the given operation with the circuit breaker control. The operation is generally calling an external system. The operation receives aPromiseobject as parameter and must callPromise.complete(Object)when the operation has terminated successfully. The operation must also callPromise.fail(Throwable)in case of a failure.The operation is not invoked if the circuit breaker is open, and the given fallback is called instead. The circuit breaker also monitors whether the operation completes in time. The operation is considered failed if it does not terminate before the configured timeout.
This method returns a
Futureobject to retrieve the status and result of the operation, with the status being a success or a failure. If the fallback is called, the returned future is successfully completed with the value returned from the fallback. If the fallback throws an exception, the returned future is marked as failed.- Type Parameters:
T- the type of result- Parameters:
command- the operationfallback- the fallback function; gets an exception as parameter and returns the fallback result- Returns:
- a future object completed when the operation or the fallback completes
-
execute
<T> Future<T> execute(Handler<Promise<T>> command)
Same asexecuteWithFallback(Handler, Function)but using the circuit breaker default fallback.- Type Parameters:
T- the type of result- Parameters:
command- the operation- Returns:
- a future object completed when the operation or its fallback completes
-
execute
<T> Future<T> execute(Supplier<Future<T>> command)
Same asexecuteWithFallback(Supplier, Function)but using the circuit breaker default fallback.- Type Parameters:
T- the type of result- Parameters:
command- the operation- Returns:
- a future object completed when the operation or its fallback completes
-
executeAndReport
<T> CircuitBreaker executeAndReport(Promise<T> resultPromise, Handler<Promise<T>> command)
Same asexecuteAndReportWithFallback(Promise, Handler, Function)but using the circuit breaker default fallback.- Type Parameters:
T- the type of result- Parameters:
resultPromise- the promise on which the operation result is reportedcommand- the operation- Returns:
- this
CircuitBreaker
-
executeAndReportWithFallback
<T> CircuitBreaker executeAndReportWithFallback(Promise<T> resultPromise, Handler<Promise<T>> command, Function<Throwable,T> fallback)
Executes the given operation with the circuit breaker control. The operation is generally calling an external system. The operation receives aPromiseobject as parameter and must callPromise.complete(Object)when the operation has terminated successfully. The operation must also callPromise.fail(Throwable)in case of a failure.The operation is not invoked if the circuit breaker is open, and the given fallback is called instead. The circuit breaker also monitors whether the operation completes in time. The operation is considered failed if it does not terminate before the configured timeout.
Unlike
executeWithFallback(Handler, Function), this method does not return aFutureobject, but lets the caller pass aPromiseobject on which the result is reported. If the fallback is called, the promise is successfully completed with the value returned by the fallback function. If the fallback throws an exception, the promise is marked as failed.- Type Parameters:
T- the type of result- Parameters:
resultPromise- the promise on which the operation result is reportedcommand- the operationfallback- the fallback function; gets an exception as parameter and returns the fallback result- Returns:
- this
CircuitBreaker
-
fallback
<T> CircuitBreaker fallback(Function<Throwable,T> handler)
Sets a default fallbackFunctionto be invoked when the circuit breaker is open or when failure occurs andCircuitBreakerOptions.isFallbackOnFailure()is enabled.The function gets the exception as parameter and returns the fallback result.
- Parameters:
handler- the fallback handler- Returns:
- this
CircuitBreaker
-
failurePolicy
default <T> CircuitBreaker failurePolicy(FailurePolicy<T> failurePolicy)
Configures the failure policy for this circuit-breaker.- Returns:
- the current
CircuitBreaker - See Also:
FailurePolicy
-
reset
CircuitBreaker reset()
Resets the circuit breaker state. The number of recent failures is set to 0 and if the state is half-open, it is set to closed.- Returns:
- this
CircuitBreaker
-
open
CircuitBreaker open()
Explicitly opens the circuit breaker.- Returns:
- this
CircuitBreaker
-
state
CircuitBreakerState state()
- Returns:
- the current state of this circuit breaker
-
failureCount
long failureCount()
- Returns:
- the current number of recorded failures
-
name
String name()
- Returns:
- the name of this circuit breaker
-
retryPolicy
CircuitBreaker retryPolicy(RetryPolicy retryPolicy)
Set aRetryPolicywhich computes a delay before a retry attempt.
-
-