Class CircuitBreakerStateMachine
- java.lang.Object
-
- io.github.resilience4j.circuitbreaker.internal.CircuitBreakerStateMachine
-
- All Implemented Interfaces:
CircuitBreaker
public final class CircuitBreakerStateMachine extends java.lang.Object implements CircuitBreaker
A CircuitBreaker finite state machine.
-
-
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
Constructors Constructor Description CircuitBreakerStateMachine(java.lang.String name)
Creates a circuitBreaker with default config.CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig)
Creates a circuitBreaker.CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, SchedulerFactory schedulerFactory)
Creates a circuitBreaker.CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a circuitBreaker.CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, java.time.Clock clock)
Creates a circuitBreaker.CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, java.time.Clock clock, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a circuitBreaker.CircuitBreakerStateMachine(java.lang.String name, java.util.function.Supplier<CircuitBreakerConfig> circuitBreakerConfig)
Creates a circuitBreaker.CircuitBreakerStateMachine(java.lang.String name, java.util.function.Supplier<CircuitBreakerConfig> circuitBreakerConfig, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a circuitBreaker.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
acquirePermission()
Try to obtain a permission to execute a call.CircuitBreakerConfig
getCircuitBreakerConfig()
Get the config of this CircuitBreaker.long
getCurrentTimestamp()
Returns the current time with respect to the CircuitBreaker currentTimeFunction.CircuitBreaker.EventPublisher
getEventPublisher()
Returns an EventPublisher which can be used to register event consumers.CircuitBreaker.Metrics
getMetrics()
Returns the Metrics of this CircuitBreaker.java.lang.String
getName()
Get the name of this CircuitBreaker.CircuitBreaker.State
getState()
Get the state of this CircuitBreaker.io.vavr.collection.Map<java.lang.String,java.lang.String>
getTags()
Returns an unmodifiable map with tags assigned to this Retry.java.util.concurrent.TimeUnit
getTimestampUnit()
Returns the timeUnit of current timestamp.void
onError(long duration, java.util.concurrent.TimeUnit durationUnit, java.lang.Throwable throwable)
Records a failed call.void
onResult(long duration, java.util.concurrent.TimeUnit durationUnit, java.lang.Object result)
This method must be invoked when a call returned a result and the result predicate should decide if the call was successful or not.void
onSuccess(long duration, java.util.concurrent.TimeUnit durationUnit)
Records a successful call.void
releasePermission()
Releases a permission.void
reset()
Returns the circuit breaker to its original closed state, losing statistics.java.lang.String
toString()
void
transitionToClosedState()
Transitions the state machine to CLOSED state.void
transitionToDisabledState()
Transitions the state machine to a DISABLED state, stopping state transition, metrics and event publishing.void
transitionToForcedOpenState()
Transitions the state machine to a FORCED_OPEN state, stopping state transition, metrics and event publishing.void
transitionToHalfOpenState()
Transitions the state machine to HALF_OPEN state.void
transitionToMetricsOnlyState()
Transitions the state machine to METRICS_ONLY state, stopping all state transitions but continues to capture metrics and publish events.void
transitionToOpenState()
Transitions the state machine to OPEN state.boolean
tryAcquirePermission()
Acquires 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, wait
-
Methods inherited from interface io.github.resilience4j.circuitbreaker.CircuitBreaker
decorateCallable, decorateCheckedConsumer, decorateCheckedRunnable, decorateCheckedSupplier, decorateCompletionStage, decorateConsumer, decorateEitherSupplier, decorateFuture, decorateRunnable, decorateSupplier, decorateTrySupplier, executeCallable, executeCheckedRunnable, executeCheckedSupplier, executeCompletionStage, executeEitherSupplier, executeRunnable, executeSupplier, executeTrySupplier
-
-
-
-
Constructor Detail
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(java.lang.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
public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, java.time.Clock clock)
Creates a circuitBreaker.- Parameters:
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, java.time.Clock clock, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a circuitBreaker.- Parameters:
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig)
Creates a circuitBreaker.- Parameters:
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a circuitBreaker.- Parameters:
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.tags
- Tags to add to the CircuitBreaker.
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(java.lang.String name)
Creates a circuitBreaker with default config.- Parameters:
name
- the name of the CircuitBreaker
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(java.lang.String name, java.util.function.Supplier<CircuitBreakerConfig> circuitBreakerConfig)
Creates a circuitBreaker.- Parameters:
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration supplier.
-
CircuitBreakerStateMachine
public CircuitBreakerStateMachine(java.lang.String name, java.util.function.Supplier<CircuitBreakerConfig> circuitBreakerConfig, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a circuitBreaker.- Parameters:
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration supplier.
-
-
Method Detail
-
getCurrentTimestamp
public long getCurrentTimestamp()
Description copied from interface:CircuitBreaker
Returns the current time with respect to the CircuitBreaker currentTimeFunction. Returns System.nanoTime() by default.- Specified by:
getCurrentTimestamp
in interfaceCircuitBreaker
- Returns:
- current timestamp
-
getTimestampUnit
public java.util.concurrent.TimeUnit getTimestampUnit()
Description copied from interface:CircuitBreaker
Returns the timeUnit of current timestamp. Default is TimeUnit.NANOSECONDS.- Specified by:
getTimestampUnit
in interfaceCircuitBreaker
- Returns:
- the timeUnit of current timestamp
-
tryAcquirePermission
public boolean tryAcquirePermission()
Description copied from interface:CircuitBreaker
Acquires 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:
tryAcquirePermission
in interfaceCircuitBreaker
- Returns:
true
if a permission was acquired andfalse
otherwise
-
releasePermission
public void releasePermission()
Description copied from interface:CircuitBreaker
Releases 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:
releasePermission
in interfaceCircuitBreaker
-
acquirePermission
public void acquirePermission()
Description copied from interface:CircuitBreaker
Try 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:
acquirePermission
in interfaceCircuitBreaker
-
onError
public void onError(long duration, java.util.concurrent.TimeUnit durationUnit, java.lang.Throwable throwable)
Description copied from interface:CircuitBreaker
Records a failed call. This method must be invoked when a call failed.- Specified by:
onError
in interfaceCircuitBreaker
- Parameters:
duration
- The elapsed time duration of the calldurationUnit
- The duration unitthrowable
- The throwable which must be recorded
-
onSuccess
public void onSuccess(long duration, java.util.concurrent.TimeUnit durationUnit)
Description copied from interface:CircuitBreaker
Records a successful call. This method must be invoked when a call was successful.- Specified by:
onSuccess
in interfaceCircuitBreaker
- Parameters:
duration
- The elapsed time duration of the calldurationUnit
- The duration unit
-
onResult
public void onResult(long duration, java.util.concurrent.TimeUnit durationUnit, @Nullable java.lang.Object result)
Description copied from interface:CircuitBreaker
This 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:
onResult
in interfaceCircuitBreaker
- Parameters:
duration
- The elapsed time duration of the calldurationUnit
- The duration unitresult
- The result of the protected function
-
getState
public CircuitBreaker.State getState()
Get the state of this CircuitBreaker.- Specified by:
getState
in interfaceCircuitBreaker
- Returns:
- the the state of this CircuitBreaker
-
getName
public java.lang.String getName()
Get the name of this CircuitBreaker.- Specified by:
getName
in interfaceCircuitBreaker
- Returns:
- the the name of this CircuitBreaker
-
getCircuitBreakerConfig
public CircuitBreakerConfig getCircuitBreakerConfig()
Get the config of this CircuitBreaker.- Specified by:
getCircuitBreakerConfig
in interfaceCircuitBreaker
- Returns:
- the config of this CircuitBreaker
-
getMetrics
public CircuitBreaker.Metrics getMetrics()
Description copied from interface:CircuitBreaker
Returns the Metrics of this CircuitBreaker.- Specified by:
getMetrics
in interfaceCircuitBreaker
- Returns:
- the Metrics of this CircuitBreaker
-
getTags
public io.vavr.collection.Map<java.lang.String,java.lang.String> getTags()
Description copied from interface:CircuitBreaker
Returns an unmodifiable map with tags assigned to this Retry.- Specified by:
getTags
in interfaceCircuitBreaker
- Returns:
- the tags assigned to this Retry in an unmodifiable map
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
reset
public void reset()
Description copied from interface:CircuitBreaker
Returns the circuit breaker to its original closed state, losing statistics.Should only be used, when you want to want to fully reset the circuit breaker without creating a new one.
- Specified by:
reset
in interfaceCircuitBreaker
-
transitionToDisabledState
public void transitionToDisabledState()
Description copied from interface:CircuitBreaker
Transitions 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:
transitionToDisabledState
in interfaceCircuitBreaker
-
transitionToMetricsOnlyState
public void transitionToMetricsOnlyState()
Description copied from interface:CircuitBreaker
Transitions 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:
transitionToMetricsOnlyState
in interfaceCircuitBreaker
-
transitionToForcedOpenState
public void transitionToForcedOpenState()
Description copied from interface:CircuitBreaker
Transitions 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:
transitionToForcedOpenState
in interfaceCircuitBreaker
-
transitionToClosedState
public void transitionToClosedState()
Description copied from interface:CircuitBreaker
Transitions 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:
transitionToClosedState
in interfaceCircuitBreaker
-
transitionToOpenState
public void transitionToOpenState()
Description copied from interface:CircuitBreaker
Transitions 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:
transitionToOpenState
in interfaceCircuitBreaker
-
transitionToHalfOpenState
public void transitionToHalfOpenState()
Description copied from interface:CircuitBreaker
Transitions 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:
transitionToHalfOpenState
in interfaceCircuitBreaker
-
getEventPublisher
public CircuitBreaker.EventPublisher getEventPublisher()
Description copied from interface:CircuitBreaker
Returns an EventPublisher which can be used to register event consumers.- Specified by:
getEventPublisher
in interfaceCircuitBreaker
- Returns:
- an EventPublisher
-
-