ThresholdCircuitBreaker¶
- public class org.apache.commons.lang3.concurrent.ThresholdCircuitBreaker extends AbstractCircuitBreaker<Long>[source]¶
A simple implementation of the Circuit Breaker pattern that opens if the requested increment amount is greater than a given threshold.
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#
- Since:
3.5
Constructors¶
Creates a new instance of |
- public ThresholdCircuitBreaker(long threshold)[source]¶
Creates a new instance of
ThresholdCircuitBreakerand initializes the threshold.- Parameters:
threshold – The threshold.
Methods¶
Checks the state of this circuit breaker and changes it if necessary. |
|
Closes this circuit breaker. |
|
Gets the threshold. |
|
Increments the monitored value and performs a check of the current state of this circuit breaker. |
- @Override public boolean checkState()[source]¶
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.
- Returns:
true if the circuit breaker is now closed; false otherwise.
- @Override public void close()[source]¶
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).
- @Override public boolean incrementAndCheckState(Long increment)[source]¶
Increments the monitored value and performs a check of the current state of this circuit breaker. This method works like
#checkState(), but the monitored value is incremented before the state check is performed.If the threshold is zero, the circuit breaker will be in a permanent open state.
The internal counter is a protective counter and only moves toward the threshold: negative increments are rejected, and an increment that would overflow
Long#MAX_VALUEsaturates the counter atLong#MAX_VALUEand opens the circuit breaker instead of silently wrapping negative (which would disable the trip condition).- Throws:
java.lang.IllegalArgumentException – Thrown if the increment is negative.
- Parameters:
increment – value to increment in the monitored value of the circuit breaker
- Returns:
true if the circuit breaker is now closed; false otherwise