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

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

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

CircuitBreaker/src/dk/thoerup/curcuitbreaker/AccountingCircuitBreaker.java revision 403 by torben, Wed Oct 7 04:14:14 2009 UTC CircuitBreaker/src/dk/thoerup/circuitbreaker/AccountingCircuitBreaker.java revision 621 by torben, Mon Mar 8 08:38:36 2010 UTC
# Line 1  Line 1 
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    
# Line 8  public class AccountingCircuitBreaker ex Line 9  public class AccountingCircuitBreaker ex
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 lastReset = 0;          private AtomicLong lastFailure = new AtomicLong(0);
14            private long lastResetCounters = 0;
15                    
16          public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {          public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
17                  super(name, threshold, timeoutMS);                  super(name, threshold, timeoutMS);
18    
19                    resetCounters();
20          }          }
21    
22          @Override          @Override
23          public Object invoke(CircuitInvocation invocation) throws Throwable {          public Object invoke(CircuitInvocation invocation) throws Exception {
24                  Object result;                  Object result;
25                  try {                  try {
26                          totalCallCount.incrementAndGet();                          totalCallCount.incrementAndGet();
27                          result = super.invoke(invocation);                          result = super.invoke(invocation);
28                  } catch (Throwable t) {                  } catch (Exception e) {
29                          if (t instanceof CircuitBreakerException) {                          if (e instanceof CircuitBreakerException) {
30                                  blockCount.incrementAndGet();                                  blockCount.incrementAndGet();
31                          } else {                          } else {
32                                  totalFailureCount.incrementAndGet();                                  totalFailureCount.incrementAndGet();
33                                    lastFailure.set( System.currentTimeMillis() );
34                          }                          }
35                          throw t;                          throw e;
36                  }                  }
37                                    
38                  return result;                  return result;
# Line 37  public class AccountingCircuitBreaker ex Line 42  public class AccountingCircuitBreaker ex
42          public void tripBreaker() {          public void tripBreaker() {
43                  super.tripBreaker();                  super.tripBreaker();
44                  tripCount.incrementAndGet();                  tripCount.incrementAndGet();
45                    lastTrip.set( System.currentTimeMillis() );
46          }          }
47                    
48          public int getTripCount() {          public int getTripCount() {
# Line 51  public class AccountingCircuitBreaker ex Line 57  public class AccountingCircuitBreaker ex
57                  return totalFailureCount.get();                  return totalFailureCount.get();
58          }          }
59                    
60          public long getLastReset() {          public long getLastResetCounters() {
61                  return lastReset;                  return lastResetCounters;
62            }
63            
64            public long getLastTrip() {
65                    return lastTrip.get();
66            }
67            
68            public long getLastFailure() {
69                    return lastFailure.get();
70          }          }
71                    
72          public int getTotalCallCount() {          public int getTotalCallCount() {
# Line 64  public class AccountingCircuitBreaker ex Line 78  public class AccountingCircuitBreaker ex
78                  blockCount.set(0);                  blockCount.set(0);
79                  totalFailureCount.set(0);                  totalFailureCount.set(0);
80                  totalCallCount.set(0);                  totalCallCount.set(0);
81                    lastTrip.set(0);
82                    lastFailure.set(0);
83                                    
84                  lastReset = System.currentTimeMillis();                  lastResetCounters = System.currentTimeMillis();
85          }          }
86                    
87  }  }

Legend:
Removed from v.403  
changed lines
  Added in v.621

  ViewVC Help
Powered by ViewVC 1.1.20