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 failureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
|
10 |
|
11 |
private long lastReset = 0;
|
12 |
|
13 |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
|
14 |
super(name, threshold, timeoutMS);
|
15 |
}
|
16 |
|
17 |
@Override
|
18 |
public Object invoke(CircuitInvocation invocation) throws Throwable {
|
19 |
Object result;
|
20 |
try {
|
21 |
result = super.invoke(invocation);
|
22 |
} catch (Throwable t) {
|
23 |
if (t instanceof CircuitBreakerException) {
|
24 |
blockCount.incrementAndGet();
|
25 |
} else {
|
26 |
failureCount.incrementAndGet();
|
27 |
}
|
28 |
throw t;
|
29 |
}
|
30 |
|
31 |
return result;
|
32 |
}
|
33 |
|
34 |
@Override
|
35 |
public void tripBreaker() {
|
36 |
super.tripBreaker();
|
37 |
tripCount.incrementAndGet();
|
38 |
}
|
39 |
|
40 |
public int getTripCount() {
|
41 |
return tripCount.get();
|
42 |
}
|
43 |
|
44 |
public int getBlockCount() {
|
45 |
return blockCount.get();
|
46 |
}
|
47 |
|
48 |
public int getFailureCount() {
|
49 |
return failureCount.get();
|
50 |
}
|
51 |
|
52 |
public long getLastReset() {
|
53 |
return lastReset;
|
54 |
}
|
55 |
|
56 |
public void resetCounters() {
|
57 |
tripCount.set(0);
|
58 |
blockCount.set(0);
|
59 |
failureCount.set(0);
|
60 |
|
61 |
lastReset = System.currentTimeMillis();
|
62 |
}
|
63 |
|
64 |
}
|