1 |
package dk.thoerup.curcuitbreaker; |
package dk.thoerup.circuitbreaker; |
2 |
|
|
3 |
import java.util.concurrent.atomic.AtomicInteger; |
import java.util.concurrent.atomic.AtomicInteger; |
4 |
|
import java.util.concurrent.atomic.AtomicLong; |
5 |
|
|
6 |
public class AccountingCircuitBreaker extends CircuitBreaker { |
public class AccountingCircuitBreaker extends CircuitBreaker { |
7 |
|
|
8 |
private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped |
private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped |
9 |
private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend |
private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend |
10 |
private AtomicInteger failureCount = new AtomicInteger(0); //how many times has the backend thrown an exception |
private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception |
11 |
|
private AtomicInteger totalCallCount = new AtomicInteger(0); |
12 |
|
|
13 |
|
private AtomicLong lastTrip = new AtomicLong(0); |
14 |
|
private AtomicLong lastFailure = new AtomicLong(0); |
15 |
|
private AtomicLong lastReset = new AtomicLong(0); |
16 |
|
|
17 |
|
private long lastResetCounters = 0; |
18 |
|
|
19 |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
20 |
super(name, threshold, timeoutMS); |
super(name, threshold, timeoutMS); |
21 |
|
|
22 |
|
resetCounters(); |
23 |
} |
} |
24 |
|
|
25 |
@Override |
@Override |
26 |
public Object invoke(CircuitInvocation invocation) throws Throwable { |
public Object invoke(CircuitInvocation invocation) throws Exception { |
27 |
Object result; |
Object result; |
28 |
try { |
try { |
29 |
|
totalCallCount.incrementAndGet(); |
30 |
result = super.invoke(invocation); |
result = super.invoke(invocation); |
31 |
} catch (Throwable t) { |
} catch (Exception e) { |
32 |
if (t instanceof CircuitBreakerException) { |
if (e instanceof CircuitBreakerException) { |
33 |
blockCount.incrementAndGet(); |
blockCount.incrementAndGet(); |
34 |
} else { |
} else { |
35 |
failureCount.incrementAndGet(); |
totalFailureCount.incrementAndGet(); |
36 |
|
lastFailure.set( System.currentTimeMillis() ); |
37 |
} |
} |
38 |
throw t; |
throw e; |
39 |
} |
} |
40 |
|
|
41 |
return result; |
return result; |
45 |
public void tripBreaker() { |
public void tripBreaker() { |
46 |
super.tripBreaker(); |
super.tripBreaker(); |
47 |
tripCount.incrementAndGet(); |
tripCount.incrementAndGet(); |
48 |
|
lastTrip.set( System.currentTimeMillis() ); |
49 |
} |
} |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@Override |
54 |
|
public void reset() { |
55 |
|
super.reset(); |
56 |
|
lastReset.set( System.currentTimeMillis()); |
57 |
|
} |
58 |
|
|
59 |
public int getTripCount() { |
public int getTripCount() { |
60 |
return tripCount.get(); |
return tripCount.get(); |
61 |
} |
} |
64 |
return blockCount.get(); |
return blockCount.get(); |
65 |
} |
} |
66 |
|
|
67 |
public int getFailureCount() { |
public int getTotalFailureCount() { |
68 |
return failureCount.get(); |
return totalFailureCount.get(); |
69 |
|
} |
70 |
|
|
71 |
|
public long getLastResetCounters() { |
72 |
|
return lastResetCounters; |
73 |
|
} |
74 |
|
|
75 |
|
public long getLastTrip() { |
76 |
|
return lastTrip.get(); |
77 |
|
} |
78 |
|
|
79 |
|
public long getLastFailure() { |
80 |
|
return lastFailure.get(); |
81 |
|
} |
82 |
|
|
83 |
|
public long getLastReset() { |
84 |
|
return lastReset.get(); |
85 |
|
} |
86 |
|
|
87 |
|
public int getTotalCallCount() { |
88 |
|
return totalCallCount.get(); |
89 |
} |
} |
90 |
|
|
91 |
public void resetCounters() { |
public void resetCounters() { |
92 |
tripCount.set(0); |
tripCount.set(0); |
93 |
blockCount.set(0); |
blockCount.set(0); |
94 |
failureCount.set(0); |
totalFailureCount.set(0); |
95 |
|
totalCallCount.set(0); |
96 |
|
lastTrip.set(0); |
97 |
|
lastReset.set(0); |
98 |
|
lastFailure.set(0); |
99 |
|
|
100 |
|
lastResetCounters = System.currentTimeMillis(); |
101 |
} |
} |
102 |
|
|
103 |
} |
} |