org.eclipse.microprofile.faulttolerance

## Annotation Type CircuitBreaker

    @Inherited
     @Retention(value=RUNTIME)
     @Documented
     @InterceptorBinding
     @Target(value={METHOD,TYPE})
    public @interface CircuitBreaker

Defines a circuit breaker policy to an individual method or a class.

A circuit breaker aims to prevent further damage by not executing functionality that is doomed to fail. After a failure situation has been detected, circuit breakers prevent methods from being executed and instead throw exceptions immediately. After a certain delay or wait time, the functionality is attempted to be executed again.

A circuit breaker can be in one of the following states:

- Closed: In normal operation, the circuit is closed. If a failure occurs, the Circuit Breaker records the event. In closed state the `requestVolumeThreshold` and `failureRatio` parameters may be configured in order to specify the conditions under which the breaker will transition the circuit to open. If the failure conditions are met, the circuit will be opened.
- Open: When the circuit is open, calls to the service operating under the circuit breaker will fail immediately. A delay may be configured for the circuit breaker. After the specified delay, the circuit transitions to half-open state.
- Half-open: In half-open state, trial executions of the service are allowed. By default one trial call to the service is permitted. If the call fails, the circuit will return to open state. The `successThreshold` parameter allows the configuration of the number of trial executions that must succeed before the circuit can be closed. After the specified number of successful executions, the circuit will be closed. If a failure occurs before the successThreshold is reached the circuit will transition to open.

Circuit state transitions will reset the circuit breaker's records.

When a method returns a result, the following rules are applied to determine whether the result is a success or a failure:

- If the method does not throw a `Throwable`, it is considered a success
- Otherwise, if the thrown object is assignable to any value in the `skipOn()` parameter, it is considered a success
- Otherwise, if the thrown object is assignable to any value in the `failOn()` parameter, it is considered a failure
- Otherwise it is considered a success

If a method throws a `Throwable` which is not an `Error` or `Exception`, non-portable behavior results.

Author:
  Emily Jiang
See Also:
  `failOn()`, `skipOn()`, `delay()`, `delayUnit()`, `requestVolumeThreshold()`, `failureRatio()`, `successThreshold()`

### Optional Element Summary

Optional Elements
`long` `delay` The delay after which an open circuit will transitions to half-open state.
`ChronoUnit` `delayUnit` The unit of the delay after which an open circuit will transitions to half-open state.
`Class<? extends Throwable>[]` `failOn` The list of exception types which should be considered failures.
`double` `failureRatio` The ratio of failures within the rolling window that will trip the circuit to open.
`int` `requestVolumeThreshold` The number of consecutive requests in a rolling window.
`Class<? extends Throwable>[]` `skipOn` The list of exception types which should not be considered failures.
`int` `successThreshold` The number of successful executions, before a half-open circuit is closed again.

### Element Detail

#### failOn

    public abstract Class<? extends Throwable>[] failOn

The list of exception types which should be considered failures.

Note that if a method throws a `Throwable` which is not an `Error` or `Exception`, non-portable behavior results.

Returns:
  the exception types which should be considered failures

Default:
  java.lang.Throwable.class

#### skipOn

    public abstract Class<? extends Throwable>[] skipOn

The list of exception types which should not be considered failures.

This list takes priority over the types listed in `failOn()`.

Note that if a method throws a `Throwable` which is not an `Error` or `Exception`, non-portable behavior results.

Returns:
  the exception types which should not be considered failures

Default:
  {}

#### delay

    public abstract long delay

The delay after which an open circuit will transitions to half-open state.

The amount of delay is taken from this delay value and the `delayUnit`, and defaults to five seconds. The value must be greater than or equal to `0`. `0` means no delay.

Returns:
  the delay time after which an open circuit transitions to half-open state

Default:
  5000L

#### delayUnit

    public abstract ChronoUnit delayUnit

The unit of the delay after which an open circuit will transitions to half-open state.

Returns:
  The unit of the delay
See Also:
  `delay()`

Default:
  java.time.temporal.ChronoUnit.MILLIS

#### requestVolumeThreshold

    public abstract int requestVolumeThreshold

The number of consecutive requests in a rolling window.

The circuit breaker will trip if the number of failures exceed the `failureRatio` within the rolling window of consecutive requests. The value must be greater than or equal to `1`.

Returns:
  the number of the consecutive requests in a rolling window

Default:
  20

#### failureRatio

    public abstract double failureRatio

The ratio of failures within the rolling window that will trip the circuit to open.

The circuit breaker will trip if the number of failures exceed the `failureRatio` within the rolling window of consecutive requests. For example, if the `requestVolumeThreshold` is `20` and `failureRatio` is `.50`, ten or more failures in 20 consecutive requests will trigger the circuit to open. The value must be between `0` and `1` inclusive.

Returns:
  the failure ratio threshold

Default:
  0.5

#### successThreshold

    public abstract int successThreshold

The number of successful executions, before a half-open circuit is closed again.

A half-open circuit will be closed once `successThreshold` executions were made without failures. If a failure occurs while in half-open state the circuit is immediately opened again. The value must be greater than or equal to `1`.

Returns:
  the success threshold to fully close the circuit

Default:
  1