Interface CircuitBreakerRule
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Determines whether a
Response
should be reported as a success or failure to a
CircuitBreaker
. If you need to determine whether the request was successful by looking into the
Response
content, use CircuitBreakerRuleWithContent
.
Note that if the last CircuitBreakerRule
completes with CircuitBreakerDecision.next()
or
a Response
is not matched with the CircuitBreakerRule
s, then the Response
will be
reported as a success.
For example:
// If a response status is 500(Interval Server Error), the response will be reported as a failure.
// Otherwise, the response will be reported as a success.
CircuitBreakerRule.onStatus(HttpStatus.INTERNAL_SERVER_ERROR);
// A response will be reported as a success if no exception is raised.
CircuitBreakerRule.onException();
// A CircuitBreakerRule that reports a response as a failure except that a response status code is 2xx.
CircuitBreakerRule.of(
// Report as a success if the class of a response status is 2xx
CircuitBreakerRule.builder()
.onStatusClass(HttpStatusClass.SUCCESS)
.thenSuccess(),
// Everything else is reported as a failure
ClientBreakerRule.builder().thenFailure());
-
Method Summary
Modifier and TypeMethodDescriptionstatic CircuitBreakerRuleBuilder
builder()
Returns a newly createdCircuitBreakerRuleBuilder
.static CircuitBreakerRuleBuilder
builder
(HttpMethod... methods) Returns a newly createdCircuitBreakerRuleBuilder
with the specifiedHttpMethod
s.static CircuitBreakerRuleBuilder
builder
(Iterable<HttpMethod> methods) Returns a newly createdCircuitBreakerRuleBuilder
with the specifiedHttpMethod
s.static CircuitBreakerRuleBuilder
builder
(BiPredicate<? super ClientRequestContext, ? super RequestHeaders> requestHeadersFilter) Returns a newly createdCircuitBreakerRuleBuilder
with the specifiedrequestHeadersFilter
.static CircuitBreakerRule
of
(CircuitBreakerRule... circuitBreakerRules) Returns aCircuitBreakerRule
that combines the specifiedCircuitBreakerRule
s.static CircuitBreakerRule
of
(Iterable<? extends CircuitBreakerRule> circuitBreakerRules) Returns aCircuitBreakerRule
that combines the specifiedCircuitBreakerRule
s.static CircuitBreakerRule
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if anException
is raised.static CircuitBreakerRule
onException
(Class<? extends Throwable> exception) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if anException
is raised and it is an instance of the specifiedexception
.static CircuitBreakerRule
onException
(BiPredicate<? super ClientRequestContext, ? super Throwable> exceptionFilter) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if anException
is raised and the specifiedexceptionFilter
returnstrue
.static CircuitBreakerRule
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the class of the response status isHttpStatusClass.SERVER_ERROR
.static CircuitBreakerRule
onStatus
(HttpStatus... statuses) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the response status is one of the specifiedHttpStatus
es.static CircuitBreakerRule
onStatus
(Iterable<HttpStatus> statuses) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the response status is one of the specifiedHttpStatus
es.static CircuitBreakerRule
onStatus
(BiPredicate<? super ClientRequestContext, ? super HttpStatus> statusFilter) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the specifiedstatusFilter
returnstrue
.static CircuitBreakerRule
onStatusClass
(HttpStatusClass statusClass) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the class of the response status is one of the specifiedHttpStatusClass
es.static CircuitBreakerRule
onStatusClass
(Iterable<HttpStatusClass> statusClasses) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the class of the response status is one of the specifiedHttpStatusClass
es.default CircuitBreakerRule
orElse
(CircuitBreakerRule other) Returns a composedCircuitBreakerRule
that represents a logical OR of thisCircuitBreakerRule
and another.default boolean
Returns whether this rule requires the response trailers to determine if aResponse
is successful or not.shouldReportAsSuccess
(ClientRequestContext ctx, @Nullable Throwable cause) Returns aCompletionStage
that contains aCircuitBreakerDecision
which indicates aResponse
is successful or not.
-
Method Details
-
onStatusClass
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the class of the response status is one of the specifiedHttpStatusClass
es. -
onStatusClass
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the class of the response status is one of the specifiedHttpStatusClass
es. -
onServerErrorStatus
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the class of the response status isHttpStatusClass.SERVER_ERROR
. -
onStatus
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the response status is one of the specifiedHttpStatus
es. -
onStatus
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the response status is one of the specifiedHttpStatus
es. -
onStatus
static CircuitBreakerRule onStatus(BiPredicate<? super ClientRequestContext, ? super HttpStatus> statusFilter) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if the specifiedstatusFilter
returnstrue
. -
onException
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if anException
is raised and it is an instance of the specifiedexception
. -
onException
static CircuitBreakerRule onException(BiPredicate<? super ClientRequestContext, ? super Throwable> exceptionFilter) Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if anException
is raised and the specifiedexceptionFilter
returnstrue
. -
onException
Returns a newly createdCircuitBreakerRule
that will report aResponse
as a failure, if anException
is raised. -
builder
Returns a newly createdCircuitBreakerRuleBuilder
. -
builder
Returns a newly createdCircuitBreakerRuleBuilder
with the specifiedHttpMethod
s. -
builder
Returns a newly createdCircuitBreakerRuleBuilder
with the specifiedHttpMethod
s. -
builder
static CircuitBreakerRuleBuilder builder(BiPredicate<? super ClientRequestContext, ? super RequestHeaders> requestHeadersFilter) Returns a newly createdCircuitBreakerRuleBuilder
with the specifiedrequestHeadersFilter
. -
of
Returns aCircuitBreakerRule
that combines the specifiedCircuitBreakerRule
s. -
of
Returns aCircuitBreakerRule
that combines the specifiedCircuitBreakerRule
s. -
orElse
Returns a composedCircuitBreakerRule
that represents a logical OR of thisCircuitBreakerRule
and another. If thisCircuitBreakerRule
completes withCircuitBreakerDecision.next()
, then otherCircuitBreakerRule
is evaluated. -
shouldReportAsSuccess
CompletionStage<CircuitBreakerDecision> shouldReportAsSuccess(ClientRequestContext ctx, @Nullable @Nullable Throwable cause) Returns aCompletionStage
that contains aCircuitBreakerDecision
which indicates aResponse
is successful or not. IfCircuitBreakerDecision.success()
is returned,CircuitBreaker.onSuccess()
is called so that theCircuitBreaker
increases its success count and uses it to make a decision to close or open the circuit. IfCircuitBreakerDecision.failure()
is returned, it works the other way around. IfCircuitBreakerDecision.ignore()
is returned, theCircuitBreaker
ignores it. IfCircuitBreakerDecision.next()
is returned, a nextCircuitBreakerRule
will be evaluated.Note that if the last
CircuitBreakerRule
completes withCircuitBreakerDecision.next()
or aResponse
is not matched with the givenCircuitBreakerRule
s, then theResponse
is reported as a success.To retrieve the
ResponseHeaders
, you can use the specifiedClientRequestContext
:> CompletionStage<CircuitBreakerDecision> shouldReportAsSuccess(ClientRequestContext ctx, > @Nullable Throwable cause) { > if (cause != null) { > return UnmodifiableFuture.completedFuture(CircuitBreakerDecision.failure()); > } > ResponseHeaders responseHeaders = ctx.log().responseHeaders(); > if (responseHeaders.status().codeClass() == HttpStatusClass.SERVER_ERROR) { > return UnmodifiableFuture.completedFuture(CircuitBreakerDecision.failure()); > } > ... > return UnmodifiableFuture.completedFuture(CircuitBreakerDecision.success()) > }
- Parameters:
ctx
- theClientRequestContext
of this requestcause
- theThrowable
which is raised while sending a request.null
if there's no exception.
-
requiresResponseTrailers
default boolean requiresResponseTrailers()Returns whether this rule requires the response trailers to determine if aResponse
is successful or not.
-