Interface CircuitBreakerClientHandler
- All Known Implementing Classes:
Resilience4JCircuitBreakerClientHandler
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A handler used by a
CircuitBreakerClient
to integrate with a circuit breaker.
One may extend this interface to use a custom circuit breaker with CircuitBreakerClient
.
// using a pre-defined handler
CircuitBreakerClient.newDecorator(
CircuitBreakerClientHandler.of(CircuitBreakerMapping.ofDefault()),
CircuitBreakerRule.onException());
// defining a custom handler
CircuitBreakerClient.newDecorator(
new CircuitBreakerClientHandler() {
...
public CircuitBreakerCallback tryRequest(ClientRequestContext ctx, HttpRequest req) {
...
MyCustomCircuitBreaker cb = ...
return new CircuitBreakerCallback() {
@Override
public void onSuccess(RequestContext ctx) {
cb.onSuccess();
}
@Override
public void onFailure(RequestContext ctx, @Nullable Throwable throwable) {
cb.onFailure();
}
};
}
...
},
CircuitBreakerRule.onException());
-
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
Determines if the givenException
is related to a circuit breaker.static CircuitBreakerClientHandler
of
(CircuitBreaker circuitBreaker) Creates a defaultCircuitBreakerClientHandler
which uses the providedCircuitBreaker
to handle requests.static CircuitBreakerClientHandler
of
(CircuitBreakerMapping mapping) Creates a defaultCircuitBreakerClientHandler
which uses the providedCircuitBreakerMapping
to handle requests.tryRequest
(ClientRequestContext ctx, Request req) Invoked byCircuitBreakerClient
right before sending a request.
-
Method Details
-
of
Creates a defaultCircuitBreakerClientHandler
which uses the providedCircuitBreaker
to handle requests. -
of
Creates a defaultCircuitBreakerClientHandler
which uses the providedCircuitBreakerMapping
to handle requests. -
tryRequest
Invoked byCircuitBreakerClient
right before sending a request. In a typical implementation, users may extract the appropriate circuit breaker implementation using the providedClientRequestContext
andRequest
. Afterwards, one of the following can occur:- If the circuit breaker rejects the request, an exception such as
FailFastException
may be thrown. - If the circuit breaker doesn't reject the request, users may choose to return a
CircuitBreakerCallback
. A callback method will be invoked depending on the configuredCircuitBreakerRule
. - One may return
null
to proceed normally as if theCircuitBreakerClient
doesn't exist.
- If the circuit breaker rejects the request, an exception such as
-
isCircuitBreakerException
Determines if the givenException
is related to a circuit breaker. This method is invoked by theCircuitBreakerClient
to determine if a fallback logic should be executed.
-