/[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 399 by torben, Tue Oct 6 13:17:41 2009 UTC CircuitBreaker/src/dk/thoerup/circuitbreaker/AccountingCircuitBreaker.java revision 1376 by torben, Sat Apr 23 10:43:40 2011 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    import dk.thoerup.circuitbreaker.config.BreakerConfig;
7    import dk.thoerup.circuitbreaker.config.StaticConfig;
8    
9  public class AccountingCircuitBreaker extends CircuitBreaker {  public class AccountingCircuitBreaker extends CircuitBreaker {
10    
11          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  
12            private AtomicInteger retripCount = new AtomicInteger(0);  
13          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
14          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
15            private AtomicInteger totalCallCount = new AtomicInteger(0);
16            
17            private AtomicLong lastTrip = new AtomicLong(0);
18            private AtomicLong lastRetrip = new AtomicLong(0);
19            private AtomicLong lastFailure = new AtomicLong(0);
20            private AtomicLong lastReset = new AtomicLong(0);
21                    
22          public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {          private long lastResetCounters = 0;
23                  super(name, threshold, timeoutMS);          
24            @Deprecated
25            public AccountingCircuitBreaker(String name, int treshold, int timeout) {
26                    this(name,  new StaticConfig(treshold, timeout) ) ;
27            }
28            
29            public AccountingCircuitBreaker(String name, BreakerConfig config) {
30                    super(name, config);
31    
32                    resetCounters();
33          }          }
34    
35          @Override          @Override
36          public Object invoke(CircuitInvocation invocation) throws Throwable {          public Object invoke(CircuitInvocation invocation) throws Exception {
37                  Object result;                  Object result;
38                  try {                  try {
39                            totalCallCount.incrementAndGet();
40                          result = super.invoke(invocation);                          result = super.invoke(invocation);
41                  } catch (Throwable t) {                  } catch (Exception e) {
42                          if (t instanceof CircuitBreakerException) {                          if (e instanceof CircuitBreakerException) {
43                                  blockCount.incrementAndGet();                                  blockCount.incrementAndGet();
44                          } else {                          } else {
45                                  failureCount.incrementAndGet();                                  totalFailureCount.incrementAndGet();
46                                    lastFailure.set( System.currentTimeMillis() );
47                          }                          }
48                          throw t;                          throw e;
49                  }                  }
50                                    
51                  return result;                  return result;
# Line 33  public class AccountingCircuitBreaker ex Line 55  public class AccountingCircuitBreaker ex
55          public void tripBreaker() {          public void tripBreaker() {
56                  super.tripBreaker();                  super.tripBreaker();
57                  tripCount.incrementAndGet();                  tripCount.incrementAndGet();
58                    lastTrip.set( System.currentTimeMillis() );
59            }
60            
61            @Override
62            public void retripBreaker() {
63                    super.retripBreaker();
64                    retripCount.incrementAndGet();
65                    lastRetrip.set( System.currentTimeMillis() );
66          }          }
67                    
68            
69            @Override
70            public void reset() {
71                    super.reset();
72                    lastReset.set( System.currentTimeMillis());
73            }
74    
75          public int getTripCount() {          public int getTripCount() {
76                  return tripCount.get();                  return tripCount.get();
77          }          }
78                    
79            public int getRetripCount() {
80                    return retripCount.get();
81            }
82    
83          public int getBlockCount() {          public int getBlockCount() {
84                  return blockCount.get();                          return blockCount.get();        
85          }          }
86                    
87          public int getFailureCount() {          public int getTotalFailureCount() {
88                  return failureCount.get();                  return totalFailureCount.get();
89            }
90            
91            public long getLastResetCounters() {
92                    return lastResetCounters;
93            }
94            
95            public long getLastTrip() {
96                    return lastTrip.get();
97            }
98            
99            public long getLastRetrip() {
100                    return lastRetrip.get();
101            }
102    
103            public long getLastFailure() {
104                    return lastFailure.get();
105            }
106            
107            public long getLastReset() {
108                    return lastReset.get();
109            }
110            
111            public int getTotalCallCount() {
112                    return totalCallCount.get();
113          }          }
114                    
115          public void resetCounters() {          public void resetCounters() {
116                  tripCount.set(0);                  tripCount.set(0);
117                    retripCount.set(0);
118                  blockCount.set(0);                  blockCount.set(0);
119                  failureCount.set(0);                  totalFailureCount.set(0);
120                    totalCallCount.set(0);
121                    lastTrip.set(0);
122                    lastRetrip.set(0);
123                    lastReset.set(0);
124                    lastFailure.set(0);
125                    
126                    lastResetCounters = System.currentTimeMillis();
127          }          }
128                    
129  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20