6 |
|
|
7 |
private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped |
private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped |
8 |
private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend |
private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend |
9 |
private AtomicInteger failureCount = new AtomicInteger(0); //how many times has the backend thrown an exception |
private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception |
10 |
|
private AtomicInteger totalCallCount = new AtomicInteger(0); |
11 |
|
|
12 |
|
private long lastResetCounters = 0; |
13 |
|
|
14 |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
15 |
super(name, threshold, timeoutMS); |
super(name, threshold, timeoutMS); |
16 |
|
|
17 |
|
resetCounters(); |
18 |
} |
} |
19 |
|
|
20 |
@Override |
@Override |
21 |
public Object invoke(CircuitInvocation invocation) throws Throwable { |
public Object invoke(CircuitInvocation invocation) throws Throwable { |
22 |
Object result; |
Object result; |
23 |
try { |
try { |
24 |
|
totalCallCount.incrementAndGet(); |
25 |
result = super.invoke(invocation); |
result = super.invoke(invocation); |
26 |
} catch (Throwable t) { |
} catch (Throwable t) { |
27 |
if (t instanceof CircuitBreakerException) { |
if (t instanceof CircuitBreakerException) { |
28 |
blockCount.incrementAndGet(); |
blockCount.incrementAndGet(); |
29 |
} else { |
} else { |
30 |
failureCount.incrementAndGet(); |
totalFailureCount.incrementAndGet(); |
31 |
} |
} |
32 |
throw t; |
throw t; |
33 |
} |
} |
49 |
return blockCount.get(); |
return blockCount.get(); |
50 |
} |
} |
51 |
|
|
52 |
public int getFailureCount() { |
public int getTotalFailureCount() { |
53 |
return failureCount.get(); |
return totalFailureCount.get(); |
54 |
|
} |
55 |
|
|
56 |
|
public long getLastResetCounters() { |
57 |
|
return lastResetCounters; |
58 |
|
} |
59 |
|
|
60 |
|
public int getTotalCallCount() { |
61 |
|
return totalCallCount.get(); |
62 |
} |
} |
63 |
|
|
64 |
public void resetCounters() { |
public void resetCounters() { |
65 |
tripCount.set(0); |
tripCount.set(0); |
66 |
blockCount.set(0); |
blockCount.set(0); |
67 |
failureCount.set(0); |
totalFailureCount.set(0); |
68 |
|
totalCallCount.set(0); |
69 |
|
|
70 |
|
lastResetCounters = System.currentTimeMillis(); |
71 |
} |
} |
72 |
|
|
73 |
} |
} |