public class ThresholdCircuitBreaker extends AbstractCircuitBreaker<java.lang.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#
PROPERTY_NAME
Constructor and Description |
---|
ThresholdCircuitBreaker(long threshold)
Creates a new instance of
ThresholdCircuitBreaker and initializes the threshold. |
Modifier and Type | Method and Description |
---|---|
boolean |
checkState()
Checks the state of this circuit breaker and changes it if necessary.
|
void |
close()
Closes this circuit breaker.
|
long |
getThreshold()
Gets the threshold.
|
boolean |
incrementAndCheckState(java.lang.Long increment)
Increments the monitored value and performs a check of the current state of this
circuit breaker.
|
addChangeListener, isClosed, isOpen, open, removeChangeListener
public ThresholdCircuitBreaker(long threshold)
Creates a new instance of ThresholdCircuitBreaker
and initializes the threshold.
threshold
- the threshold.public long getThreshold()
public boolean checkState()
CLOSED
; a value
of true typically means that the current operation can continue.checkState
in interface CircuitBreaker<java.lang.Long>
checkState
in class AbstractCircuitBreaker<java.lang.Long>
public void close()
Resets the internal counter back to its initial value (zero).
close
in interface CircuitBreaker<java.lang.Long>
close
in class AbstractCircuitBreaker<java.lang.Long>
public boolean incrementAndCheckState(java.lang.Long increment)
CircuitBreaker.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.
incrementAndCheckState
in interface CircuitBreaker<java.lang.Long>
incrementAndCheckState
in class AbstractCircuitBreaker<java.lang.Long>
increment
- value to increment in the monitored value of the circuit breakerCopyright © 2010 - 2020 Adobe. All Rights Reserved