1 |
package dk.thoerup.circuitbreaker;
|
2 |
|
3 |
import java.util.concurrent.atomic.AtomicInteger;
|
4 |
import java.util.concurrent.atomic.AtomicLong;
|
5 |
|
6 |
import dk.thoerup.circuitbreaker.config.BreakerConfig;
|
7 |
import dk.thoerup.circuitbreaker.config.StaticConfig;
|
8 |
|
9 |
public class AccountingCircuitBreaker extends CircuitBreaker {
|
10 |
|
11 |
private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped
|
12 |
private AtomicInteger retripCount = new AtomicInteger(0);
|
13 |
private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend
|
14 |
private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
|
15 |
private AtomicInteger totalCallCount = new AtomicInteger(0);
|
16 |
|
17 |
private AtomicLong lastTrip = new AtomicLong(0);
|
18 |
private AtomicLong lastRetrip = new AtomicLong(0);
|
19 |
private AtomicLong lastFailure = new AtomicLong(0);
|
20 |
private AtomicLong lastReset = new AtomicLong(0);
|
21 |
|
22 |
private long lastResetCounters = 0;
|
23 |
|
24 |
@Deprecated
|
25 |
public AccountingCircuitBreaker(String name, int treshold, int timeout) {
|
26 |
this(name, new StaticConfig(treshold, timeout) ) ;
|
27 |
}
|
28 |
|
29 |
public AccountingCircuitBreaker(String name, BreakerConfig config) {
|
30 |
super(name, config);
|
31 |
|
32 |
resetCounters();
|
33 |
}
|
34 |
|
35 |
@Override
|
36 |
public Object invoke(CircuitInvocation invocation) throws Exception {
|
37 |
Object result;
|
38 |
try {
|
39 |
totalCallCount.incrementAndGet();
|
40 |
result = super.invoke(invocation);
|
41 |
} catch (Exception e) {
|
42 |
if (e instanceof CircuitBreakerException) {
|
43 |
blockCount.incrementAndGet();
|
44 |
} else {
|
45 |
totalFailureCount.incrementAndGet();
|
46 |
lastFailure.set( System.currentTimeMillis() );
|
47 |
}
|
48 |
throw e;
|
49 |
}
|
50 |
|
51 |
return result;
|
52 |
}
|
53 |
|
54 |
@Override
|
55 |
public void tripBreaker() {
|
56 |
super.tripBreaker();
|
57 |
tripCount.incrementAndGet();
|
58 |
lastTrip.set( System.currentTimeMillis() );
|
59 |
}
|
60 |
|
61 |
@Override
|
62 |
public void retripBreaker() {
|
63 |
super.retripBreaker();
|
64 |
retripCount.incrementAndGet();
|
65 |
lastRetrip.set( System.currentTimeMillis() );
|
66 |
}
|
67 |
|
68 |
|
69 |
@Override
|
70 |
public void reset() {
|
71 |
super.reset();
|
72 |
lastReset.set( System.currentTimeMillis());
|
73 |
}
|
74 |
|
75 |
public int getTripCount() {
|
76 |
return tripCount.get();
|
77 |
}
|
78 |
|
79 |
public int getRetripCount() {
|
80 |
return retripCount.get();
|
81 |
}
|
82 |
|
83 |
public int getBlockCount() {
|
84 |
return blockCount.get();
|
85 |
}
|
86 |
|
87 |
public int getTotalFailureCount() {
|
88 |
return totalFailureCount.get();
|
89 |
}
|
90 |
|
91 |
public long getLastResetCounters() {
|
92 |
return lastResetCounters;
|
93 |
}
|
94 |
|
95 |
public long getLastTrip() {
|
96 |
return lastTrip.get();
|
97 |
}
|
98 |
|
99 |
public long getLastRetrip() {
|
100 |
return lastRetrip.get();
|
101 |
}
|
102 |
|
103 |
public long getLastFailure() {
|
104 |
return lastFailure.get();
|
105 |
}
|
106 |
|
107 |
public long getLastReset() {
|
108 |
return lastReset.get();
|
109 |
}
|
110 |
|
111 |
public int getTotalCallCount() {
|
112 |
return totalCallCount.get();
|
113 |
}
|
114 |
|
115 |
public void resetCounters() {
|
116 |
tripCount.set(0);
|
117 |
retripCount.set(0);
|
118 |
blockCount.set(0);
|
119 |
totalFailureCount.set(0);
|
120 |
totalCallCount.set(0);
|
121 |
lastTrip.set(0);
|
122 |
lastRetrip.set(0);
|
123 |
lastReset.set(0);
|
124 |
lastFailure.set(0);
|
125 |
|
126 |
lastResetCounters = System.currentTimeMillis();
|
127 |
}
|
128 |
|
129 |
}
|