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 |
resetCounters();
|
18 |
}
|
19 |
|
20 |
@Override
|
21 |
public Object invoke(CircuitInvocation invocation) throws Exception {
|
22 |
Object result;
|
23 |
try {
|
24 |
totalCallCount.incrementAndGet();
|
25 |
result = super.invoke(invocation);
|
26 |
} catch (Exception e) {
|
27 |
if (e instanceof CircuitBreakerException) {
|
28 |
blockCount.incrementAndGet();
|
29 |
} else {
|
30 |
totalFailureCount.incrementAndGet();
|
31 |
}
|
32 |
throw e;
|
33 |
}
|
34 |
|
35 |
return result;
|
36 |
}
|
37 |
|
38 |
@Override
|
39 |
public void tripBreaker() {
|
40 |
super.tripBreaker();
|
41 |
tripCount.incrementAndGet();
|
42 |
}
|
43 |
|
44 |
public int getTripCount() {
|
45 |
return tripCount.get();
|
46 |
}
|
47 |
|
48 |
public int getBlockCount() {
|
49 |
return blockCount.get();
|
50 |
}
|
51 |
|
52 |
public int getTotalFailureCount() {
|
53 |
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() {
|
65 |
tripCount.set(0);
|
66 |
blockCount.set(0);
|
67 |
totalFailureCount.set(0);
|
68 |
totalCallCount.set(0);
|
69 |
|
70 |
lastResetCounters = System.currentTimeMillis();
|
71 |
}
|
72 |
|
73 |
}
|