/[projects]/CircuitBreaker/src/dk/thoerup/curcuitbreaker/AccountingCircuitBreaker.java
ViewVC logotype

Contents of /CircuitBreaker/src/dk/thoerup/curcuitbreaker/AccountingCircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 403 - (show annotations) (download)
Wed Oct 7 04:14:14 2009 UTC (14 years, 7 months ago) by torben
File size: 1779 byte(s)
Finished JMX interfaces to CB and AccountingCB
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 totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
10 private AtomicInteger totalCallCount = new AtomicInteger(0);
11
12 private long lastReset = 0;
13
14 public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
15 super(name, threshold, timeoutMS);
16 }
17
18 @Override
19 public Object invoke(CircuitInvocation invocation) throws Throwable {
20 Object result;
21 try {
22 totalCallCount.incrementAndGet();
23 result = super.invoke(invocation);
24 } catch (Throwable t) {
25 if (t instanceof CircuitBreakerException) {
26 blockCount.incrementAndGet();
27 } else {
28 totalFailureCount.incrementAndGet();
29 }
30 throw t;
31 }
32
33 return result;
34 }
35
36 @Override
37 public void tripBreaker() {
38 super.tripBreaker();
39 tripCount.incrementAndGet();
40 }
41
42 public int getTripCount() {
43 return tripCount.get();
44 }
45
46 public int getBlockCount() {
47 return blockCount.get();
48 }
49
50 public int getTotalFailureCount() {
51 return totalFailureCount.get();
52 }
53
54 public long getLastReset() {
55 return lastReset;
56 }
57
58 public int getTotalCallCount() {
59 return totalCallCount.get();
60 }
61
62 public void resetCounters() {
63 tripCount.set(0);
64 blockCount.set(0);
65 totalFailureCount.set(0);
66 totalCallCount.set(0);
67
68 lastReset = System.currentTimeMillis();
69 }
70
71 }

  ViewVC Help
Powered by ViewVC 1.1.20