1 |
package dk.thoerup.curcuitbreaker;
|
2 |
|
3 |
import java.util.concurrent.atomic.AtomicInteger;
|
4 |
|
5 |
public class AccountingCircuitBreaker extends CircuitBreaker {
|
6 |
|
7 |
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
|
9 |
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) {
|
15 |
super(name, threshold, timeoutMS);
|
16 |
}
|
17 |
|
18 |
@Override
|
19 |
public Object invoke(CircuitInvocation invocation) throws Throwable {
|
20 |
Object result;
|
21 |
try {
|
22 |
totalCallCount.incrementAndGet();
|
23 |
result = super.invoke(invocation);
|
24 |
} catch (Throwable t) {
|
25 |
if (t instanceof CircuitBreakerException) {
|
26 |
blockCount.incrementAndGet();
|
27 |
} else {
|
28 |
totalFailureCount.incrementAndGet();
|
29 |
}
|
30 |
throw t;
|
31 |
}
|
32 |
|
33 |
return result;
|
34 |
}
|
35 |
|
36 |
@Override
|
37 |
public void tripBreaker() {
|
38 |
super.tripBreaker();
|
39 |
tripCount.incrementAndGet();
|
40 |
}
|
41 |
|
42 |
public int getTripCount() {
|
43 |
return tripCount.get();
|
44 |
}
|
45 |
|
46 |
public int getBlockCount() {
|
47 |
return blockCount.get();
|
48 |
}
|
49 |
|
50 |
public int getTotalFailureCount() {
|
51 |
return totalFailureCount.get();
|
52 |
}
|
53 |
|
54 |
public long getLastResetCounters() {
|
55 |
return lastResetCounters;
|
56 |
}
|
57 |
|
58 |
public int getTotalCallCount() {
|
59 |
return totalCallCount.get();
|
60 |
}
|
61 |
|
62 |
public void resetCounters() {
|
63 |
tripCount.set(0);
|
64 |
blockCount.set(0);
|
65 |
totalFailureCount.set(0);
|
66 |
totalCallCount.set(0);
|
67 |
|
68 |
lastResetCounters = System.currentTimeMillis();
|
69 |
}
|
70 |
|
71 |
}
|