package dk.thoerup.circuitbreaker; import java.util.concurrent.atomic.AtomicInteger; import dk.thoerup.circuitbreaker.config.BreakerConfig; public final class ClosedState implements CircuitBreakerState { final AtomicInteger failureCount = new AtomicInteger(0); BreakerConfig config; public void setThreshold(BreakerConfig config) { this.config = config; } public void preInvoke(CircuitBreaker circuitBreaker) throws Exception { // NO OP } public void postInvoke(CircuitBreaker circuitBreaker) throws Exception { resetFailureCount(); } public void onError(CircuitBreaker circuitBreaker, Exception t) throws Exception { int currentCount = failureCount.incrementAndGet(); int threshold = config.getTreshold(); if(currentCount >= threshold) circuitBreaker.tripBreaker(); } public void resetFailureCount() { failureCount.set(0); } public String getName() { return "Closed"; } public int getFailureCount() { return failureCount.get(); } public int getThreshold() { return config.getTreshold(); } }