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 |
|
|
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 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 |
11 |
private AtomicInteger totalCallCount = new AtomicInteger(0); |
private AtomicInteger totalCallCount = new AtomicInteger(0); |
12 |
|
private AtomicLong lastTrip = new AtomicLong(0); |
13 |
private long lastResetCounters = 0; |
private long lastResetCounters = 0; |
14 |
|
|
15 |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
16 |
super(name, threshold, timeoutMS); |
super(name, threshold, timeoutMS); |
17 |
|
|
18 |
|
resetCounters(); |
19 |
} |
} |
20 |
|
|
21 |
@Override |
@Override |
22 |
public Object invoke(CircuitInvocation invocation) throws Throwable { |
public Object invoke(CircuitInvocation invocation) throws Exception { |
23 |
Object result; |
Object result; |
24 |
try { |
try { |
25 |
totalCallCount.incrementAndGet(); |
totalCallCount.incrementAndGet(); |
26 |
result = super.invoke(invocation); |
result = super.invoke(invocation); |
27 |
} catch (Throwable t) { |
} catch (Exception e) { |
28 |
if (t instanceof CircuitBreakerException) { |
if (e instanceof CircuitBreakerException) { |
29 |
blockCount.incrementAndGet(); |
blockCount.incrementAndGet(); |
30 |
} else { |
} else { |
31 |
totalFailureCount.incrementAndGet(); |
totalFailureCount.incrementAndGet(); |
32 |
} |
} |
33 |
throw t; |
throw e; |
34 |
} |
} |
35 |
|
|
36 |
return result; |
return result; |
40 |
public void tripBreaker() { |
public void tripBreaker() { |
41 |
super.tripBreaker(); |
super.tripBreaker(); |
42 |
tripCount.incrementAndGet(); |
tripCount.incrementAndGet(); |
43 |
|
lastTrip.set( System.currentTimeMillis() ); |
44 |
} |
} |
45 |
|
|
46 |
public int getTripCount() { |
public int getTripCount() { |
59 |
return lastResetCounters; |
return lastResetCounters; |
60 |
} |
} |
61 |
|
|
62 |
|
public long getLastTrip() { |
63 |
|
return lastTrip.get(); |
64 |
|
} |
65 |
|
|
66 |
public int getTotalCallCount() { |
public int getTotalCallCount() { |
67 |
return totalCallCount.get(); |
return totalCallCount.get(); |
68 |
} |
} |
72 |
blockCount.set(0); |
blockCount.set(0); |
73 |
totalFailureCount.set(0); |
totalFailureCount.set(0); |
74 |
totalCallCount.set(0); |
totalCallCount.set(0); |
75 |
|
lastTrip.set(0); |
76 |
|
|
77 |
lastResetCounters = System.currentTimeMillis(); |
lastResetCounters = System.currentTimeMillis(); |
78 |
} |
} |