Top Description Fields Constructors Methods
org.apache.commons.lang3.concurrent

public Class ThresholdCircuitBreaker

extends AbstractCircuitBreaker<Long>
Class Inheritance
Imports
java.util.concurrent.atomic.AtomicLong

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

Field Summary

Modifier and TypeField 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.

Inherited from org.apache.commons.lang3.concurrent.AbstractCircuitBreaker:
PROPERTY_NAMEstate

Constructor Summary

AccessConstructor and Description
public
ThresholdCircuitBreaker(final long
the threshold.
threshold
)

Creates a new instance of ThresholdCircuitBreaker and initializes the threshold.

Method Summary

Modifier and TypeMethod and Description
public boolean
checkState()

Implements abstract org.apache.commons.lang3.concurrent.AbstractCircuitBreaker.checkState.

Implements org.apache.commons.lang3.concurrent.CircuitBreaker.checkState.

Checks the state of this circuit breaker and changes it if necessary.

public void
public long

Returns:

the threshold
getThreshold
()

Gets the threshold.

public boolean
incrementAndCheckState(final Long increment)

Implements abstract org.apache.commons.lang3.concurrent.AbstractCircuitBreaker.incrementAndCheckState.

Implements org.apache.commons.lang3.concurrent.CircuitBreaker.incrementAndCheckState.

Increments the monitored value and performs a check of the current state of this circuit breaker.

Inherited from org.apache.commons.lang3.concurrent.AbstractCircuitBreaker:
addChangeListenerchangeStateisClosedisOpenisOpenopenremoveChangeListener

Field Detail

INITIAL_COUNTback to summary
private static final long INITIAL_COUNT

The initial value of the internal counter.

thresholdback to summary
private final long threshold

The threshold.

usedback to summary
private final AtomicLong used

Controls the amount used.

Constructor Detail

ThresholdCircuitBreakerback to summary
public ThresholdCircuitBreaker(final long threshold)

Creates a new instance of ThresholdCircuitBreaker and initializes the threshold.

Parameters
threshold:long

the threshold.

Method Detail

checkStateback to summary
public boolean checkState()

Implements abstract org.apache.commons.lang3.concurrent.AbstractCircuitBreaker.checkState.

Implements org.apache.commons.lang3.concurrent.CircuitBreaker.checkState.

Doc from org.apache.commons.lang3.concurrent.AbstractCircuitBreaker.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.

Annotations
@Override
closeback to summary
public void close()

Overrides org.apache.commons.lang3.concurrent.AbstractCircuitBreaker.close.

Implements org.apache.commons.lang3.concurrent.CircuitBreaker.close.

Doc from org.apache.commons.lang3.concurrent.AbstractCircuitBreaker.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).

Annotations
@Override
getThresholdback to summary
public long getThreshold()

Gets the threshold.

Returns:long

the threshold

incrementAndCheckStateback to summary
public boolean incrementAndCheckState(final Long increment)

Implements abstract org.apache.commons.lang3.concurrent.AbstractCircuitBreaker.incrementAndCheckState.

Implements org.apache.commons.lang3.concurrent.CircuitBreaker.incrementAndCheckState.

Doc from org.apache.commons.lang3.concurrent.AbstractCircuitBreaker.incrementAndCheckState.

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.

Annotations
@Override