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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 399 by torben, Tue Oct 6 13:17:41 2009 UTC revision 460 by torben, Wed Oct 21 07:56:37 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.curcuitbreaker;  package dk.thoerup.curcuitbreaker;
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            private AtomicLong lastTrip = new AtomicLong(0);
13            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();
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                                  failureCount.incrementAndGet();                                  totalFailureCount.incrementAndGet();
32                          }                          }
33                          throw t;                          throw e;
34                  }                  }
35                                    
36                  return result;                  return result;
# Line 33  public class AccountingCircuitBreaker ex Line 40  public class AccountingCircuitBreaker ex
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() {
# Line 43  public class AccountingCircuitBreaker ex Line 51  public class AccountingCircuitBreaker ex
51                  return blockCount.get();                          return blockCount.get();        
52          }          }
53                    
54          public int getFailureCount() {          public int getTotalFailureCount() {
55                  return failureCount.get();                  return totalFailureCount.get();
56            }
57            
58            public long getLastResetCounters() {
59                    return lastResetCounters;
60            }
61            
62            public long getLastTrip() {
63                    return lastTrip.get();
64            }
65            
66            public int getTotalCallCount() {
67                    return totalCallCount.get();
68          }          }
69                    
70          public void resetCounters() {          public void resetCounters() {
71                  tripCount.set(0);                  tripCount.set(0);
72                  blockCount.set(0);                  blockCount.set(0);
73                  failureCount.set(0);                  totalFailureCount.set(0);
74                    totalCallCount.set(0);
75                    lastTrip.set(0);
76                    
77                    lastResetCounters = System.currentTimeMillis();
78          }          }
79                    
80  }  }

Legend:
Removed from v.399  
changed lines
  Added in v.460

  ViewVC Help
Powered by ViewVC 1.1.20