It contains an internal counter that starts in zero, and each call increments the counter by a given amount. If the threshold is zero, the circuit breaker will be in a permanent open state.
An example of use case could be a memory circuit breaker.
long threshold = 10L;
ThresholdCircuitBreaker breaker = new ThresholdCircuitBreaker(10L);
...
public void handleRequest(Request request) {
long memoryUsed = estimateMemoryUsage(request);
if (breaker.incrementAndCheckState(memoryUsed)) {
// actually handle this request
} else {
// do something else, e.g. send an error code
}
}
#Thread safe#
| Modifier and Type | Field and Description |
|---|---|
| private static final long | INITIAL_COUNT
The initial value of the internal counter. |
| private final long | threshold
The threshold. |
| private final AtomicLong | used
Controls the amount used. |
| Access | Constructor and Description |
|---|---|
| public | ThresholdCircuitBreaker(final long
the threshold. threshold)Creates a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | checkState()
Implements abstract org. Implements org. Checks the state of this circuit breaker and changes it if necessary. |
| public void | close()
Overrides org. Implements org. Closes this circuit breaker. |
| public long | |
| public boolean | incrementAndCheckState(final Long increment)
Implements abstract org. Implements org. Increments the monitored value and performs a check of the current state of this circuit breaker. |
| INITIAL_COUNT | back to summary |
|---|---|
| private static final long INITIAL_COUNT The initial value of the internal counter. | |
| threshold | back to summary |
|---|---|
| private final long threshold The threshold. | |
| used | back to summary |
|---|---|
| private final AtomicLong used Controls the amount used. | |
| ThresholdCircuitBreaker | back to summary |
|---|---|
| public ThresholdCircuitBreaker(final long threshold) Creates a new instance of
| |
| checkState | back to summary |
|---|---|
| public boolean checkState() Implements abstract org. Implements org. Doc from org. Checks the state of this circuit breaker and changes it if necessary. The return value indicates whether the circuit breaker is now in state closed; a value of true typically means that the current operation can continue.
| |
| close | back to summary |
|---|---|
| public void close() Overrides org. Implements org. Doc from org. Closes this circuit breaker. Its state is changed to closed. If this circuit breaker is already closed, this method has no effect. Resets the internal counter back to its initial value (zero).
| |
| getThreshold | back to summary |
|---|---|
| public long getThreshold() Gets the threshold.
| |
| incrementAndCheckState | back to summary |
|---|---|
| public boolean incrementAndCheckState(final Long increment) Implements abstract org. Implements org. Doc from org. Increments the monitored value and performs a check of the current state of this
circuit breaker. This method works like If the threshold is zero, the circuit breaker will be in a permanent open state.
| |