package dk.thoerup.curcuitbreaker; import java.util.concurrent.atomic.AtomicInteger; public class AccountingCircuitBreaker extends CircuitBreaker { private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend private AtomicInteger failureCount = new AtomicInteger(0); //how many times has the backend thrown an exception public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { super(name, threshold, timeoutMS); } @Override public Object invoke(CircuitInvocation invocation) throws Throwable { Object result; try { result = super.invoke(invocation); } catch (Throwable t) { if (t instanceof CircuitBreakerException) { blockCount.incrementAndGet(); } else { failureCount.incrementAndGet(); } throw t; } return result; } @Override public void tripBreaker() { super.tripBreaker(); tripCount.incrementAndGet(); } public int getTripCount() { return tripCount.get(); } public int getBlockCount() { return blockCount.get(); } public int getFailureCount() { return failureCount.get(); } public void resetCounters() { tripCount.set(0); blockCount.set(0); failureCount.set(0); } }