Class ThresholdCircuitBreaker
java.lang.Object
org.apache.commons.lang3.concurrent.AbstractCircuitBreaker<Long>
org.apache.commons.lang3.concurrent.ThresholdCircuitBreaker
- All Implemented Interfaces:
CircuitBreaker<Long>
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
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractCircuitBreaker
AbstractCircuitBreaker.StateModifier and TypeClassDescriptionprotected static enumEnumerates the different states of a circuit breaker. -
Field Summary
Fields inherited from class AbstractCircuitBreaker
PROPERTY_NAME, stateModifier and TypeFieldDescriptionstatic final StringThe name of the open property as it is passed to registered change listeners.protected final AtomicReference<AbstractCircuitBreaker.State> The current state of this circuit breaker. -
Constructor Summary
ConstructorsConstructorDescriptionThresholdCircuitBreaker(long threshold) Creates a new instance ofThresholdCircuitBreakerand initializes the threshold. -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks the state of this circuit breaker and changes it if necessary.voidclose()Closes this circuit breaker.longGets the threshold.booleanincrementAndCheckState(Long increment) Increments the monitored value and performs a check of the current state of this circuit breaker.Methods inherited from class AbstractCircuitBreaker
addChangeListener, changeState, isClosed, isOpen, isOpen, open, removeChangeListenerModifier and TypeMethodDescriptionvoidaddChangeListener(PropertyChangeListener listener) Adds a change listener to this circuit breaker.protected voidchangeState(AbstractCircuitBreaker.State newState) Changes the internal state of this circuit breaker.booleanisClosed()Tests the current closed state of this circuit breaker.booleanisOpen()Tests the current open state of this circuit breaker.protected static booleanTests whether the given state represents an open circuit breaker.voidopen()Opens this circuit breaker.voidremoveChangeListener(PropertyChangeListener listener) Removes the specified change listener from this circuit breaker.
-
Constructor Details
-
ThresholdCircuitBreaker
Creates a new instance ofThresholdCircuitBreakerand initializes the threshold.- Parameters:
threshold- The threshold.
-
-
Method Details
-
checkState
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.- Specified by:
checkStatein interfaceCircuitBreaker<Long>- Specified by:
checkStatein classAbstractCircuitBreaker<Long>- Returns:
- true if the circuit breaker is now closed; false otherwise.
-
close
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).
- Specified by:
closein interfaceCircuitBreaker<Long>- Overrides:
closein classAbstractCircuitBreaker<Long>
-
getThreshold
-
incrementAndCheckState
Increments the monitored value and performs a check of the current state of this circuit breaker. This method works likeCircuitBreaker.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).- Specified by:
incrementAndCheckStatein interfaceCircuitBreaker<Long>- Specified by:
incrementAndCheckStatein classAbstractCircuitBreaker<Long>- Parameters:
increment- value to increment in the monitored value of the circuit breaker- Returns:
- true if the circuit breaker is now closed; false otherwise
- Throws:
IllegalArgumentException- Thrown if the increment is negative.
-