1 |
torben |
399 |
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 |
torben |
403 |
private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
|
10 |
|
|
private AtomicInteger totalCallCount = new AtomicInteger(0);
|
11 |
torben |
399 |
|
12 |
torben |
404 |
private long lastResetCounters = 0;
|
13 |
torben |
400 |
|
14 |
torben |
399 |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
|
15 |
|
|
super(name, threshold, timeoutMS);
|
16 |
torben |
444 |
|
17 |
|
|
resetCounters();
|
18 |
torben |
399 |
}
|
19 |
|
|
|
20 |
|
|
@Override
|
21 |
torben |
450 |
public Object invoke(CircuitInvocation invocation) throws Exception {
|
22 |
torben |
399 |
Object result;
|
23 |
|
|
try {
|
24 |
torben |
403 |
totalCallCount.incrementAndGet();
|
25 |
torben |
399 |
result = super.invoke(invocation);
|
26 |
torben |
450 |
} catch (Exception e) {
|
27 |
|
|
if (e instanceof CircuitBreakerException) {
|
28 |
torben |
399 |
blockCount.incrementAndGet();
|
29 |
|
|
} else {
|
30 |
torben |
403 |
totalFailureCount.incrementAndGet();
|
31 |
torben |
399 |
}
|
32 |
torben |
450 |
throw e;
|
33 |
torben |
399 |
}
|
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 |
torben |
403 |
public int getTotalFailureCount() {
|
53 |
|
|
return totalFailureCount.get();
|
54 |
torben |
399 |
}
|
55 |
|
|
|
56 |
torben |
404 |
public long getLastResetCounters() {
|
57 |
|
|
return lastResetCounters;
|
58 |
torben |
400 |
}
|
59 |
|
|
|
60 |
torben |
403 |
public int getTotalCallCount() {
|
61 |
|
|
return totalCallCount.get();
|
62 |
|
|
}
|
63 |
|
|
|
64 |
torben |
399 |
public void resetCounters() {
|
65 |
|
|
tripCount.set(0);
|
66 |
|
|
blockCount.set(0);
|
67 |
torben |
403 |
totalFailureCount.set(0);
|
68 |
|
|
totalCallCount.set(0);
|
69 |
torben |
400 |
|
70 |
torben |
404 |
lastResetCounters = System.currentTimeMillis();
|
71 |
torben |
399 |
}
|
72 |
|
|
|
73 |
|
|
}
|