Class CircuitBreakerStateMachine
- All Implemented Interfaces:
CircuitBreaker
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.resilience4j.circuitbreaker.CircuitBreaker
CircuitBreaker.CircuitBreakerFuture<T>, CircuitBreaker.EventPublisher, CircuitBreaker.Metrics, CircuitBreaker.State, CircuitBreaker.StateTransition -
Constructor Summary
ConstructorsConstructorDescriptionCreates a circuitBreaker with default config.CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig) Creates a circuitBreaker.CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig, SchedulerFactory schedulerFactory) Creates a circuitBreaker.CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig, Map<String, String> tags) Creates a circuitBreaker.CircuitBreakerStateMachine(String name, Supplier<CircuitBreakerConfig> circuitBreakerConfig) Creates a circuitBreaker.CircuitBreakerStateMachine(String name, Supplier<CircuitBreakerConfig> circuitBreakerConfig, Map<String, String> tags) Creates a circuitBreaker. -
Method Summary
Modifier and TypeMethodDescriptionvoidTry to obtain a permission to execute a call.Get the config of this CircuitBreaker.longReturns the current time with respect to the CircuitBreaker currentTimeFunction.Returns an EventPublisher which can be used to register event consumers.Returns the Metrics of this CircuitBreaker.getName()Get the name of this CircuitBreaker.getState()Get the state of this CircuitBreaker.getTags()Returns an unmodifiable map with tags assigned to this CircuitBreaker.Returns the timeUnit of current timestamp.voidRecords a failed call.voidThis method must be invoked when a call returned a result and the result predicate should decide if the call was successful or not.voidRecords a successful call.voidReleases a permission.voidreset()Returns the circuit breaker to its original closed state, losing statistics.toString()voidTransitions the state machine to CLOSED state.voidTransitions the state machine to a DISABLED state, stopping state transition, metrics and event publishing.voidTransitions the state machine to a FORCED_OPEN state, stopping state transition, metrics and event publishing.voidTransitions the state machine to HALF_OPEN state.voidTransitions the state machine to METRICS_ONLY state, stopping all state transitions but continues to capture metrics and publish events.voidTransitions the state machine to OPEN state.voidtransitionToOpenStateFor(Duration waitDuration) Same asCircuitBreaker.transitionToOpenState()but waits in open state for the given amount of time instead of relaying on configurations to determine it.voidtransitionToOpenStateUntil(Instant waitUntil) Same asCircuitBreaker.transitionToOpenState()but waits in open state until the given in time instead of relaying on configurations to determine it.booleanAcquires a permission to execute a call, only if one is available at the time of invocation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.github.resilience4j.circuitbreaker.CircuitBreaker
decorateCallable, decorateCheckedConsumer, decorateCheckedRunnable, decorateCheckedSupplier, decorateCompletionStage, decorateConsumer, decorateFuture, decorateRunnable, decorateSupplier, executeCallable, executeCheckedRunnable, executeCheckedSupplier, executeCompletionStage, executeRunnable, executeSupplier
-
Constructor Details
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig, SchedulerFactory schedulerFactory) Creates a circuitBreaker.- Parameters:
name- the name of the CircuitBreakercircuitBreakerConfig- The CircuitBreaker configuration.schedulerFactory- A SchedulerFactory which can be mocked in tests.
-
CircuitBreakerStateMachine
Creates a circuitBreaker.- Parameters:
name- the name of the CircuitBreakercircuitBreakerConfig- The CircuitBreaker configuration.
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig, Map<String, String> tags) Creates a circuitBreaker.- Parameters:
name- the name of the CircuitBreakercircuitBreakerConfig- The CircuitBreaker configuration.
-
CircuitBreakerStateMachine
Creates a circuitBreaker with default config.- Parameters:
name- the name of the CircuitBreaker
-
CircuitBreakerStateMachine
Creates a circuitBreaker.- Parameters:
name- the name of the CircuitBreakercircuitBreakerConfig- The CircuitBreaker configuration supplier.
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(String name, Supplier<CircuitBreakerConfig> circuitBreakerConfig, Map<String, String> tags) Creates a circuitBreaker.- Parameters:
name- the name of the CircuitBreakercircuitBreakerConfig- The CircuitBreaker configuration supplier.
-
-
Method Details
-
getCurrentTimestamp
public long getCurrentTimestamp()Description copied from interface:CircuitBreakerReturns the current time with respect to the CircuitBreaker currentTimeFunction. Returns System.nanoTime() by default.- Specified by:
getCurrentTimestampin interfaceCircuitBreaker- Returns:
- current timestamp
-
getTimestampUnit
Description copied from interface:CircuitBreakerReturns the timeUnit of current timestamp. Default is TimeUnit.NANOSECONDS.- Specified by:
getTimestampUnitin interfaceCircuitBreaker- Returns:
- the timeUnit of current timestamp
-
tryAcquirePermission
public boolean tryAcquirePermission()Description copied from interface:CircuitBreakerAcquires a permission to execute a call, only if one is available at the time of invocation. If a call is not permitted, the number of not permitted calls is increased.Returns false when the state is OPEN or FORCED_OPEN. Returns true when the state is CLOSED or DISABLED. Returns true when the state is HALF_OPEN and further test calls are allowed. Returns false when the state is HALF_OPEN and the number of test calls has been reached. If the state is HALF_OPEN, the number of allowed test calls is decreased. Important: Make sure to call onSuccess or onError after the call is finished. If the call is cancelled before it is invoked, you have to release the permission again.
- Specified by:
tryAcquirePermissionin interfaceCircuitBreaker- Returns:
trueif a permission was acquired andfalseotherwise
-
releasePermission
public void releasePermission()Description copied from interface:CircuitBreakerReleases a permission.Should only be used when a permission was acquired but not used. Otherwise use
CircuitBreaker.onSuccess(long, TimeUnit)orCircuitBreaker.onError(long, TimeUnit, Throwable)to signal a completed or failed call.If the state is HALF_OPEN, the number of allowed test calls is increased by one.
- Specified by:
releasePermissionin interfaceCircuitBreaker
-
acquirePermission
public void acquirePermission()Description copied from interface:CircuitBreakerTry to obtain a permission to execute a call. If a call is not permitted, the number of not permitted calls is increased.Throws a CallNotPermittedException when the state is OPEN or FORCED_OPEN. Returns when the state is CLOSED or DISABLED. Returns when the state is HALF_OPEN and further test calls are allowed. Throws a CallNotPermittedException when the state is HALF_OPEN and the number of test calls has been reached. If the state is HALF_OPEN, the number of allowed test calls is decreased. Important: Make sure to call onSuccess or onError after the call is finished. If the call is cancelled before it is invoked, you have to release the permission again.
- Specified by:
acquirePermissionin interfaceCircuitBreaker
-
onError
Description copied from interface:CircuitBreakerRecords a failed call. This method must be invoked when a call failed.- Specified by:
onErrorin interfaceCircuitBreaker- Parameters:
duration- The elapsed time duration of the calldurationUnit- The duration unitthrowable- The throwable which must be recorded
-
onSuccess
Description copied from interface:CircuitBreakerRecords a successful call. This method must be invoked when a call was successful.- Specified by:
onSuccessin interfaceCircuitBreaker- Parameters:
duration- The elapsed time duration of the calldurationUnit- The duration unit
-
onResult
Description copied from interface:CircuitBreakerThis method must be invoked when a call returned a result and the result predicate should decide if the call was successful or not.- Specified by:
onResultin interfaceCircuitBreaker- Parameters:
duration- The elapsed time duration of the calldurationUnit- The duration unitresult- The result of the protected function
-
getState
Get the state of this CircuitBreaker.- Specified by:
getStatein interfaceCircuitBreaker- Returns:
- the state of this CircuitBreaker
-
getName
Get the name of this CircuitBreaker.- Specified by:
getNamein interfaceCircuitBreaker- Returns:
- the name of this CircuitBreaker
-
getCircuitBreakerConfig
Get the config of this CircuitBreaker.- Specified by:
getCircuitBreakerConfigin interfaceCircuitBreaker- Returns:
- the config of this CircuitBreaker
-
getMetrics
Description copied from interface:CircuitBreakerReturns the Metrics of this CircuitBreaker.- Specified by:
getMetricsin interfaceCircuitBreaker- Returns:
- the Metrics of this CircuitBreaker
-
getTags
Description copied from interface:CircuitBreakerReturns an unmodifiable map with tags assigned to this CircuitBreaker.- Specified by:
getTagsin interfaceCircuitBreaker- Returns:
- the tags assigned to this CircuitBreaker in an unmodifiable map
-
toString
-
reset
public void reset()Description copied from interface:CircuitBreakerReturns the circuit breaker to its original closed state, losing statistics.Should only be used, when you want to fully reset the circuit breaker without creating a new one.
- Specified by:
resetin interfaceCircuitBreaker
-
transitionToDisabledState
public void transitionToDisabledState()Description copied from interface:CircuitBreakerTransitions the state machine to a DISABLED state, stopping state transition, metrics and event publishing. This call is idempotent and will not have any effect if the state machine is already in DISABLED state.Should only be used, when you want to disable the circuit breaker allowing all calls to pass. To recover from this state you must force a new state transition
- Specified by:
transitionToDisabledStatein interfaceCircuitBreaker
-
transitionToMetricsOnlyState
public void transitionToMetricsOnlyState()Description copied from interface:CircuitBreakerTransitions the state machine to METRICS_ONLY state, stopping all state transitions but continues to capture metrics and publish events. This call is idempotent and will not have any effect if the state machine is already in METRICS_ONLY state.Should only be used when you want to collect metrics while keeping the circuit breaker disabled, allowing all calls to pass. To recover from this state you must force a new state transition.
- Specified by:
transitionToMetricsOnlyStatein interfaceCircuitBreaker
-
transitionToForcedOpenState
public void transitionToForcedOpenState()Description copied from interface:CircuitBreakerTransitions the state machine to a FORCED_OPEN state, stopping state transition, metrics and event publishing. This call is idempotent and will not have any effect if the state machine is already in FORCED_OPEN state.Should only be used, when you want to disable the circuit breaker allowing no call to pass. To recover from this state you must force a new state transition
- Specified by:
transitionToForcedOpenStatein interfaceCircuitBreaker
-
transitionToClosedState
public void transitionToClosedState()Description copied from interface:CircuitBreakerTransitions the state machine to CLOSED state. This call is idempotent and will not have any effect if the state machine is already in CLOSED state.Should only be used, when you want to force a state transition. State transition are normally done internally.
- Specified by:
transitionToClosedStatein interfaceCircuitBreaker
-
transitionToOpenState
public void transitionToOpenState()Description copied from interface:CircuitBreakerTransitions the state machine to OPEN state. This call is idempotent and will not have any effect if the state machine is already in OPEN state.Should only be used, when you want to force a state transition. State transition are normally done internally.
- Specified by:
transitionToOpenStatein interfaceCircuitBreaker
-
transitionToOpenStateFor
Description copied from interface:CircuitBreakerSame asCircuitBreaker.transitionToOpenState()but waits in open state for the given amount of time instead of relaying on configurations to determine it.- Specified by:
transitionToOpenStateForin interfaceCircuitBreaker- Parameters:
waitDuration- how long should we wait in open state
-
transitionToOpenStateUntil
Description copied from interface:CircuitBreakerSame asCircuitBreaker.transitionToOpenState()but waits in open state until the given in time instead of relaying on configurations to determine it.- Specified by:
transitionToOpenStateUntilin interfaceCircuitBreaker- Parameters:
waitUntil- how long should we wait in open state
-
transitionToHalfOpenState
public void transitionToHalfOpenState()Description copied from interface:CircuitBreakerTransitions the state machine to HALF_OPEN state. This call is idempotent and will not have any effect if the state machine is already in HALF_OPEN state.Should only be used, when you want to force a state transition. State transition are normally done internally.
- Specified by:
transitionToHalfOpenStatein interfaceCircuitBreaker
-
getEventPublisher
Description copied from interface:CircuitBreakerReturns an EventPublisher which can be used to register event consumers.- Specified by:
getEventPublisherin interfaceCircuitBreaker- Returns:
- an EventPublisher
-