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 retripCount = new AtomicInteger(0); |
10 |
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 |
11 |
private AtomicInteger totalFailureCount = 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 |
12 |
private AtomicInteger totalCallCount = new AtomicInteger(0); |
private AtomicInteger totalCallCount = new AtomicInteger(0); |
13 |
|
|
14 |
|
private AtomicLong lastTrip = new AtomicLong(0); |
15 |
|
private AtomicLong lastRetrip = new AtomicLong(0); |
16 |
|
private AtomicLong lastFailure = new AtomicLong(0); |
17 |
|
private AtomicLong lastReset = new AtomicLong(0); |
18 |
|
|
19 |
private long lastResetCounters = 0; |
private long lastResetCounters = 0; |
20 |
|
|
21 |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
22 |
super(name, threshold, timeoutMS); |
super(name, threshold, timeoutMS); |
23 |
|
|
24 |
|
resetCounters(); |
25 |
} |
} |
26 |
|
|
27 |
@Override |
@Override |
28 |
public Object invoke(CircuitInvocation invocation) throws Throwable { |
public Object invoke(CircuitInvocation invocation) throws Exception { |
29 |
Object result; |
Object result; |
30 |
try { |
try { |
31 |
totalCallCount.incrementAndGet(); |
totalCallCount.incrementAndGet(); |
32 |
result = super.invoke(invocation); |
result = super.invoke(invocation); |
33 |
} catch (Throwable t) { |
} catch (Exception e) { |
34 |
if (t instanceof CircuitBreakerException) { |
if (e instanceof CircuitBreakerException) { |
35 |
blockCount.incrementAndGet(); |
blockCount.incrementAndGet(); |
36 |
} else { |
} else { |
37 |
totalFailureCount.incrementAndGet(); |
totalFailureCount.incrementAndGet(); |
38 |
|
lastFailure.set( System.currentTimeMillis() ); |
39 |
} |
} |
40 |
throw t; |
throw e; |
41 |
} |
} |
42 |
|
|
43 |
return result; |
return result; |
47 |
public void tripBreaker() { |
public void tripBreaker() { |
48 |
super.tripBreaker(); |
super.tripBreaker(); |
49 |
tripCount.incrementAndGet(); |
tripCount.incrementAndGet(); |
50 |
|
lastTrip.set( System.currentTimeMillis() ); |
51 |
|
} |
52 |
|
|
53 |
|
@Override |
54 |
|
public void retripBreaker() { |
55 |
|
super.retripBreaker(); |
56 |
|
retripCount.incrementAndGet(); |
57 |
|
lastRetrip.set( System.currentTimeMillis() ); |
58 |
} |
} |
59 |
|
|
60 |
|
|
61 |
|
@Override |
62 |
|
public void reset() { |
63 |
|
super.reset(); |
64 |
|
lastReset.set( System.currentTimeMillis()); |
65 |
|
} |
66 |
|
|
67 |
public int getTripCount() { |
public int getTripCount() { |
68 |
return tripCount.get(); |
return tripCount.get(); |
69 |
} |
} |
70 |
|
|
71 |
|
public int getRetripCount() { |
72 |
|
return retripCount.get(); |
73 |
|
} |
74 |
|
|
75 |
public int getBlockCount() { |
public int getBlockCount() { |
76 |
return blockCount.get(); |
return blockCount.get(); |
77 |
} |
} |
84 |
return lastResetCounters; |
return lastResetCounters; |
85 |
} |
} |
86 |
|
|
87 |
|
public long getLastTrip() { |
88 |
|
return lastTrip.get(); |
89 |
|
} |
90 |
|
|
91 |
|
public long getLastRetrip() { |
92 |
|
return lastRetrip.get(); |
93 |
|
} |
94 |
|
|
95 |
|
public long getLastFailure() { |
96 |
|
return lastFailure.get(); |
97 |
|
} |
98 |
|
|
99 |
|
public long getLastReset() { |
100 |
|
return lastReset.get(); |
101 |
|
} |
102 |
|
|
103 |
public int getTotalCallCount() { |
public int getTotalCallCount() { |
104 |
return totalCallCount.get(); |
return totalCallCount.get(); |
105 |
} |
} |
106 |
|
|
107 |
public void resetCounters() { |
public void resetCounters() { |
108 |
tripCount.set(0); |
tripCount.set(0); |
109 |
|
retripCount.set(0); |
110 |
blockCount.set(0); |
blockCount.set(0); |
111 |
totalFailureCount.set(0); |
totalFailureCount.set(0); |
112 |
totalCallCount.set(0); |
totalCallCount.set(0); |
113 |
|
lastTrip.set(0); |
114 |
|
lastRetrip.set(0); |
115 |
|
lastReset.set(0); |
116 |
|
lastFailure.set(0); |
117 |
|
|
118 |
lastResetCounters = System.currentTimeMillis(); |
lastResetCounters = System.currentTimeMillis(); |
119 |
} |
} |