public final class CircuitBreakerStateMachine extends java.lang.Object implements CircuitBreaker
CircuitBreaker.CircuitBreakerFuture<T>, CircuitBreaker.EventPublisher, CircuitBreaker.Metrics, CircuitBreaker.State, CircuitBreaker.StateTransition
Constructor and 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,
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,
CircuitBreakerConfig circuitBreakerConfig,
io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a circuitBreaker.
|
CircuitBreakerStateMachine(java.lang.String name,
CircuitBreakerConfig circuitBreakerConfig,
SchedulerFactory schedulerFactory)
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.
|
Modifier and Type | Method and Description |
---|---|
void |
acquirePermission()
Try to obtain a permission to execute a call.
|
CircuitBreakerConfig |
getCircuitBreakerConfig()
Get the config of this CircuitBreaker.
|
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.
|
void |
onError(long duration,
java.util.concurrent.TimeUnit durationUnit,
java.lang.Throwable throwable)
Records a failed call.
|
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.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
decorateCallable, decorateCallable, decorateCheckedConsumer, decorateCheckedConsumer, decorateCheckedFunction, decorateCheckedRunnable, decorateCheckedRunnable, decorateCheckedSupplier, decorateCheckedSupplier, decorateCompletionStage, decorateCompletionStage, decorateConsumer, decorateConsumer, decorateEitherSupplier, decorateEitherSupplier, decorateFunction, decorateFuture, decorateFuture, decorateRunnable, decorateRunnable, decorateSupplier, decorateSupplier, decorateTrySupplier, decorateTrySupplier, executeCallable, executeCheckedRunnable, executeCheckedSupplier, executeCompletionStage, executeEitherSupplier, executeRunnable, executeSupplier, executeTrySupplier, of, of, of, of, ofDefaults
public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, SchedulerFactory schedulerFactory)
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.schedulerFactory
- A SchedulerFactory which can be mocked in tests.public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, java.time.Clock clock)
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, java.time.Clock clock, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig)
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.public CircuitBreakerStateMachine(java.lang.String name, CircuitBreakerConfig circuitBreakerConfig, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration.tags
- Tags to add to the CircuitBreaker.public CircuitBreakerStateMachine(java.lang.String name)
name
- the name of the CircuitBreakerpublic CircuitBreakerStateMachine(java.lang.String name, java.util.function.Supplier<CircuitBreakerConfig> circuitBreakerConfig)
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration supplier.public CircuitBreakerStateMachine(java.lang.String name, java.util.function.Supplier<CircuitBreakerConfig> circuitBreakerConfig, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
name
- the name of the CircuitBreakercircuitBreakerConfig
- The CircuitBreaker configuration supplier.public boolean tryAcquirePermission()
CircuitBreaker
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.
tryAcquirePermission
in interface CircuitBreaker
true
if a permission was acquired and false
otherwisepublic void releasePermission()
CircuitBreaker
Should only be used when a permission was acquired but not used. Otherwise use CircuitBreaker.onSuccess(long, TimeUnit)
or CircuitBreaker.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.
releasePermission
in interface CircuitBreaker
public void acquirePermission()
CircuitBreaker
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.
acquirePermission
in interface CircuitBreaker
public void onError(long duration, java.util.concurrent.TimeUnit durationUnit, java.lang.Throwable throwable)
CircuitBreaker
onError
in interface CircuitBreaker
duration
- The elapsed time duration of the calldurationUnit
- The duration unitthrowable
- The throwable which must be recordedpublic void onSuccess(long duration, java.util.concurrent.TimeUnit durationUnit)
CircuitBreaker
onSuccess
in interface CircuitBreaker
duration
- The elapsed time duration of the calldurationUnit
- The duration unitpublic CircuitBreaker.State getState()
getState
in interface CircuitBreaker
public java.lang.String getName()
getName
in interface CircuitBreaker
public CircuitBreakerConfig getCircuitBreakerConfig()
getCircuitBreakerConfig
in interface CircuitBreaker
public CircuitBreaker.Metrics getMetrics()
CircuitBreaker
getMetrics
in interface CircuitBreaker
public io.vavr.collection.Map<java.lang.String,java.lang.String> getTags()
CircuitBreaker
getTags
in interface CircuitBreaker
public java.lang.String toString()
toString
in class java.lang.Object
public void reset()
CircuitBreaker
Should only be used, when you want to want to fully reset the circuit breaker without creating a new one.
reset
in interface CircuitBreaker
public void transitionToDisabledState()
CircuitBreaker
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
transitionToDisabledState
in interface CircuitBreaker
public void transitionToMetricsOnlyState()
CircuitBreaker
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.
transitionToMetricsOnlyState
in interface CircuitBreaker
public void transitionToForcedOpenState()
CircuitBreaker
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
transitionToForcedOpenState
in interface CircuitBreaker
public void transitionToClosedState()
CircuitBreaker
Should only be used, when you want to force a state transition. State transition are normally done internally.
transitionToClosedState
in interface CircuitBreaker
public void transitionToOpenState()
CircuitBreaker
Should only be used, when you want to force a state transition. State transition are normally done internally.
transitionToOpenState
in interface CircuitBreaker
public void transitionToHalfOpenState()
CircuitBreaker
Should only be used, when you want to force a state transition. State transition are normally done internally.
transitionToHalfOpenState
in interface CircuitBreaker
public CircuitBreaker.EventPublisher getEventPublisher()
CircuitBreaker
getEventPublisher
in interface CircuitBreaker