Class CircuitBreakerConfig.Builder
- Enclosing class:
- CircuitBreakerConfig
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionautomaticTransitionFromOpenToHalfOpenEnabled(boolean enableAutomaticTransitionFromOpenToHalfOpen) Enables automatic transition from OPEN to HALF_OPEN state once the waitDurationInOpenState has passed.build()Builds a CircuitBreakerConfigcurrentTimestampFunction(Function<Clock, Long> currentTimestampFunction, TimeUnit timeUnit) Configures a function that returns current timestamp for CircuitBreaker.Enables automatic transition from OPEN to HALF_OPEN state once the waitDurationInOpenState has passed.failureRateThreshold(float failureRateThreshold) Configures the failure rate threshold in percentage.ignoreException(Predicate<Throwable> predicate) Configures a Predicate which evaluates if an exception should be ignored and neither count as a failure nor success.ignoreExceptions(Class<? extends Throwable>... errorClasses) Configures a list of error classes that are ignored and thus neither count as a failure nor success.maxWaitDurationInHalfOpenState(Duration maxWaitDurationInHalfOpenState) Configures CircuitBreaker with a fixed wait duration which controls how long the CircuitBreaker should stay in Half Open state, before it switches to open.minimumNumberOfCalls(int minimumNumberOfCalls) Configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate.permittedNumberOfCallsInHalfOpenState(int permittedNumberOfCallsInHalfOpenState) Configures the number of permitted calls when the CircuitBreaker is half open.recordException(Predicate<Throwable> predicate) Configures a Predicate which evaluates if an exception should be recorded as a failure and thus increase the failure rate.recordExceptions(Class<? extends Throwable>... errorClasses) Configures a list of error classes that are recorded as a failure and thus increase the failure rate.recordResult(Predicate<Object> predicate) Configures a Predicate which evaluates if the result of the protected function call should be recorded as a failure and thus increase the failure rate.slidingWindow(int slidingWindowSize, int minimumNumberOfCalls, CircuitBreakerConfig.SlidingWindowType slidingWindowType) Configures the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.slidingWindowSize(int slidingWindowSize) Configures the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.slidingWindowType(CircuitBreakerConfig.SlidingWindowType slidingWindowType) Configures the type of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.slowCallDurationThreshold(Duration slowCallDurationThreshold) Configures the duration threshold above which calls are considered as slow and increase the slow calls percentage.slowCallRateThreshold(float slowCallRateThreshold) Configures a threshold in percentage.transitionOnResult(Function<io.github.resilience4j.core.functions.Either<Object, Throwable>, CircuitBreakerConfig.TransitionCheckResult> transitionOnResult) Configures a function which can decide if the circuit breaker should transition to a different state base on the result of the protected function.waitDurationInOpenState(Duration waitDurationInOpenState) Configures an interval function with a fixed wait duration which controls how long the CircuitBreaker should stay open, before it switches to half open.waitIntervalFunctionInOpenState(io.github.resilience4j.core.IntervalFunction waitIntervalFunctionInOpenState) Configures an interval function which controls how long the CircuitBreaker should stay open, before it switches to half open.writableStackTraceEnabled(boolean writableStackTraceEnabled) Enables writable stack traces.
-
Constructor Details
-
Builder
-
Builder
public Builder()
-
-
Method Details
-
failureRateThreshold
Configures the failure rate threshold in percentage. If the failure rate is equal to or greater than the threshold, the CircuitBreaker transitions to open and starts short-circuiting calls.The threshold must be greater than 0 and not greater than 100. Default value is 50 percentage.
- Parameters:
failureRateThreshold- the failure rate threshold in percentage- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- iffailureRateThreshold <= 0 || failureRateThreshold > 100
-
slowCallRateThreshold
Configures a threshold in percentage. The CircuitBreaker considers a call as slow when the call duration is greater thanslowCallDurationThreshold(Duration). When the percentage of slow calls is equal to or greater than the threshold, the CircuitBreaker transitions to open and starts short-circuiting calls.The threshold must be greater than 0 and not greater than 100. Default value is 100 percentage which means that all recorded calls must be slower than
slowCallDurationThreshold(Duration).- Parameters:
slowCallRateThreshold- the slow calls threshold in percentage- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- ifslowCallRateThreshold <= 0 || slowCallRateThreshold > 100
-
writableStackTraceEnabled
Enables writable stack traces. When set to false,Throwable.getStackTrace()returns a zero length array. This may be used to reduce log spam when the circuit breaker is open as the cause of the exceptions is already known (the circuit breaker is short-circuiting calls).- Parameters:
writableStackTraceEnabled- the flag to enable writable stack traces.- Returns:
- the CircuitBreakerConfig.Builder
-
waitDurationInOpenState
Configures an interval function with a fixed wait duration which controls how long the CircuitBreaker should stay open, before it switches to half open. Default value is 60 seconds.Do not use with
waitIntervalFunctionInOpenState(IntervalFunction)! Please, when using, make sure not to override the value set earlier from thewaitIntervalFunctionInOpenState(IntervalFunction)- Parameters:
waitDurationInOpenState- the wait duration which specifies how long the CircuitBreaker should stay open- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- ifwaitDurationInOpenState.toMillis() < 1
-
waitIntervalFunctionInOpenState
public CircuitBreakerConfig.Builder waitIntervalFunctionInOpenState(io.github.resilience4j.core.IntervalFunction waitIntervalFunctionInOpenState) Configures an interval function which controls how long the CircuitBreaker should stay open, before it switches to half open. The default interval function returns a fixed wait duration of 60 seconds.A custom interval function is useful if you need an exponential backoff algorithm.
Do not use with
waitDurationInOpenState(Duration)! Please, when using, make sure not to override the value set earlier from thewaitDurationInOpenState(Duration)- Parameters:
waitIntervalFunctionInOpenState- Interval function that returns wait time as a function of attempts- Returns:
- the CircuitBreakerConfig.Builder
-
transitionOnResult
public CircuitBreakerConfig.Builder transitionOnResult(Function<io.github.resilience4j.core.functions.Either<Object, Throwable>, CircuitBreakerConfig.TransitionCheckResult> transitionOnResult) Configures a function which can decide if the circuit breaker should transition to a different state base on the result of the protected function.- Parameters:
transitionOnResult- function which instructs the circuit breaker if it should transition to a different state- Returns:
- the CircuitBreakerConfig.Builder
-
slowCallDurationThreshold
Configures the duration threshold above which calls are considered as slow and increase the slow calls percentage. Default value is 60 seconds.- Parameters:
slowCallDurationThreshold- the duration above which calls are considered as slow- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- ifslowCallDurationThreshold.toNanos() < 1
-
maxWaitDurationInHalfOpenState
public CircuitBreakerConfig.Builder maxWaitDurationInHalfOpenState(Duration maxWaitDurationInHalfOpenState) Configures CircuitBreaker with a fixed wait duration which controls how long the CircuitBreaker should stay in Half Open state, before it switches to open. This is an optional parameter. By default CircuitBreaker will stay in Half Open state untilminimumNumberOfCallsis completed with either success or failure.- Parameters:
maxWaitDurationInHalfOpenState- the wait duration which specifies how long the CircuitBreaker should stay in Half Open- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- ifmaxWaitDurationInHalfOpenState.toMillis() < 0
-
permittedNumberOfCallsInHalfOpenState
public CircuitBreakerConfig.Builder permittedNumberOfCallsInHalfOpenState(int permittedNumberOfCallsInHalfOpenState) Configures the number of permitted calls when the CircuitBreaker is half open.The size must be greater than 0. Default size is 10.
- Parameters:
permittedNumberOfCallsInHalfOpenState- the permitted number of calls when the CircuitBreaker is half open- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- ifpermittedNumberOfCallsInHalfOpenState < 1
-
slidingWindow
public CircuitBreakerConfig.Builder slidingWindow(int slidingWindowSize, int minimumNumberOfCalls, CircuitBreakerConfig.SlidingWindowType slidingWindowType) Configures the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.slidingWindowSizeconfigures the size of the sliding window. Sliding window can either be count-based or time-based, specified byslidingWindowType.minimumNumberOfCallsconfigures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate. For example, ifminimumNumberOfCallsis 10, then at least 10 calls must be recorded, before the failure rate can be calculated. If only 9 calls have been recorded, the CircuitBreaker will not transition to open, even if all 9 calls have failed.If
slidingWindowSizeis 100 andslidingWindowTypeis COUNT_BASED, the last 100 calls are recorded and aggregated. IfslidingWindowSizeis 10 andslidingWindowTypeis TIME_BASED, the calls of the last 10 seconds are recorded and aggregated.The
slidingWindowSizemust be greater than 0. TheminimumNumberOfCallsmust be greater than 0. If theslidingWindowTypeis COUNT_BASED, theminimumNumberOfCallsmay not be greater thanslidingWindowSize. If a greater value is provided,minimumNumberOfCallswill be equal toslidingWindowSize. If theslidingWindowTypeis TIME_BASED, theminimumNumberOfCallsmay be any amount.Default slidingWindowSize is 100, minimumNumberOfCalls is 100 and slidingWindowType is COUNT_BASED.
- Parameters:
slidingWindowSize- the size of the sliding window when the CircuitBreaker is closed.minimumNumberOfCalls- the minimum number of calls that must be recorded before the failure rate can be calculated.slidingWindowType- the type of the sliding window. Either COUNT_BASED or TIME_BASED.- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- ifslidingWindowSize < 1 || minimumNumberOfCalls < 1
-
slidingWindowSize
Configures the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.slidingWindowSizeconfigures the size of the sliding window.The
slidingWindowSizemust be greater than 0.Default slidingWindowSize is 100.
- Parameters:
slidingWindowSize- the size of the sliding window when the CircuitBreaker is closed.- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- ifslidingWindowSize < 1- See Also:
-
minimumNumberOfCalls
Configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate. For example, ifminimumNumberOfCallsis 10, then at least 10 calls must be recorded, before the failure rate can be calculated. If only 9 calls have been recorded, the CircuitBreaker will not transition to open, even if all 9 calls have failed.Default minimumNumberOfCalls is 100
- Parameters:
minimumNumberOfCalls- the minimum number of calls that must be recorded before the failure rate can be calculated.- Returns:
- the CircuitBreakerConfig.Builder
- Throws:
IllegalArgumentException- ifminimumNumberOfCalls < 1- See Also:
-
slidingWindowType
public CircuitBreakerConfig.Builder slidingWindowType(CircuitBreakerConfig.SlidingWindowType slidingWindowType) Configures the type of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed. Sliding window can either be count-based or time-based.Default slidingWindowType is COUNT_BASED.
- Parameters:
slidingWindowType- the type of the sliding window. Either COUNT_BASED or TIME_BASED.- Returns:
- the CircuitBreakerConfig.Builder
- See Also:
-
recordException
Configures a Predicate which evaluates if an exception should be recorded as a failure and thus increase the failure rate. The Predicate must return true if the exception should count as a failure. The Predicate must return false, if the exception should count as a success, unless the exception is explicitly ignored byignoreExceptions(Class[])orignoreException(Predicate).- Parameters:
predicate- the Predicate which evaluates if an exception should count as a failure- Returns:
- the CircuitBreakerConfig.Builder
-
currentTimestampFunction
public CircuitBreakerConfig.Builder currentTimestampFunction(Function<Clock, Long> currentTimestampFunction, TimeUnit timeUnit) Configures a function that returns current timestamp for CircuitBreaker. Default implementation uses System.nanoTime() to compute current timestamp. Configure currentTimestampFunction to provide different implementation to compute current timestamp.- Parameters:
currentTimestampFunction- function that computes current timestamp.timeUnit- TimeUnit of timestamp returned by the function.- Returns:
- the CircuitBreakerConfig.Builder
-
recordResult
Configures a Predicate which evaluates if the result of the protected function call should be recorded as a failure and thus increase the failure rate. The Predicate must return true if the result should count as a failure. The Predicate must return false, if the result should count as a success.- Parameters:
predicate- the Predicate which evaluates if a result should count as a failure- Returns:
- the CircuitBreakerConfig.Builder
-
ignoreException
Configures a Predicate which evaluates if an exception should be ignored and neither count as a failure nor success. The Predicate must return true if the exception should be ignored. The Predicate must return false, if the exception should count as a failure.- Parameters:
predicate- the Predicate which evaluates if an exception should count as a failure- Returns:
- the CircuitBreakerConfig.Builder
-
recordExceptions
@SafeVarargs public final CircuitBreakerConfig.Builder recordExceptions(@Nullable Class<? extends Throwable>... errorClasses) Configures a list of error classes that are recorded as a failure and thus increase the failure rate. Any exception matching or inheriting from one of the list should count as a failure, unless ignored viaignoreExceptions(Class[])orignoreException(Predicate).- Parameters:
errorClasses- the error classes that are recorded- Returns:
- the CircuitBreakerConfig.Builder
- See Also:
-
ignoreExceptions
@SafeVarargs public final CircuitBreakerConfig.Builder ignoreExceptions(@Nullable Class<? extends Throwable>... errorClasses) Configures a list of error classes that are ignored and thus neither count as a failure nor success. Any exception matching or inheriting from one of the list will not count as a failure nor success, even if marked viarecordExceptions(Class[])orrecordException(Predicate).- Parameters:
errorClasses- the error classes that are ignored- Returns:
- the CircuitBreakerConfig.Builder
- See Also:
-
. Ignoring an exception has priority over recording an exception.Example: ignoreExceptions(Throwable.class) and recordExceptions(Exception.class) would capture nothing.
Example: ignoreExceptions(Exception.class) and recordExceptions(Throwable.class) would capture Errors.
For a more sophisticated exception management use the
method
-
enableAutomaticTransitionFromOpenToHalfOpen
Enables automatic transition from OPEN to HALF_OPEN state once the waitDurationInOpenState has passed.- Returns:
- the CircuitBreakerConfig.Builder
-
automaticTransitionFromOpenToHalfOpenEnabled
public CircuitBreakerConfig.Builder automaticTransitionFromOpenToHalfOpenEnabled(boolean enableAutomaticTransitionFromOpenToHalfOpen) Enables automatic transition from OPEN to HALF_OPEN state once the waitDurationInOpenState has passed.- Parameters:
enableAutomaticTransitionFromOpenToHalfOpen- the flag to enable the automatic transitioning.- Returns:
- the CircuitBreakerConfig.Builder
-
build
Builds a CircuitBreakerConfig- Returns:
- the CircuitBreakerConfig
- Throws:
IllegalStateException- when the parameter is invalid
-