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

Annotation of /miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/AccountingCircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 621 - (hide annotations) (download)
Mon Mar 8 08:38:36 2010 UTC (14 years, 3 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/circuitbreaker/AccountingCircuitBreaker.java
File size: 2257 byte(s)
Also take not of the time when the last failure occurred 
1 torben 467 package dk.thoerup.circuitbreaker;
2 torben 399
3     import java.util.concurrent.atomic.AtomicInteger;
4 torben 460 import java.util.concurrent.atomic.AtomicLong;
5 torben 399
6     public class AccountingCircuitBreaker extends CircuitBreaker {
7    
8     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
10 torben 403 private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
11     private AtomicInteger totalCallCount = new AtomicInteger(0);
12 torben 460 private AtomicLong lastTrip = new AtomicLong(0);
13 torben 621 private AtomicLong lastFailure = new AtomicLong(0);
14 torben 404 private long lastResetCounters = 0;
15 torben 400
16 torben 399 public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
17     super(name, threshold, timeoutMS);
18 torben 444
19     resetCounters();
20 torben 399 }
21    
22     @Override
23 torben 450 public Object invoke(CircuitInvocation invocation) throws Exception {
24 torben 399 Object result;
25     try {
26 torben 403 totalCallCount.incrementAndGet();
27 torben 399 result = super.invoke(invocation);
28 torben 450 } catch (Exception e) {
29     if (e instanceof CircuitBreakerException) {
30 torben 399 blockCount.incrementAndGet();
31     } else {
32 torben 403 totalFailureCount.incrementAndGet();
33 torben 621 lastFailure.set( System.currentTimeMillis() );
34 torben 399 }
35 torben 450 throw e;
36 torben 399 }
37    
38     return result;
39     }
40    
41     @Override
42     public void tripBreaker() {
43     super.tripBreaker();
44     tripCount.incrementAndGet();
45 torben 460 lastTrip.set( System.currentTimeMillis() );
46 torben 399 }
47    
48     public int getTripCount() {
49     return tripCount.get();
50     }
51    
52     public int getBlockCount() {
53     return blockCount.get();
54     }
55    
56 torben 403 public int getTotalFailureCount() {
57     return totalFailureCount.get();
58 torben 399 }
59    
60 torben 404 public long getLastResetCounters() {
61     return lastResetCounters;
62 torben 400 }
63    
64 torben 460 public long getLastTrip() {
65     return lastTrip.get();
66     }
67    
68 torben 621 public long getLastFailure() {
69     return lastFailure.get();
70     }
71    
72 torben 403 public int getTotalCallCount() {
73     return totalCallCount.get();
74     }
75    
76 torben 399 public void resetCounters() {
77     tripCount.set(0);
78     blockCount.set(0);
79 torben 403 totalFailureCount.set(0);
80     totalCallCount.set(0);
81 torben 460 lastTrip.set(0);
82 torben 621 lastFailure.set(0);
83 torben 400
84 torben 404 lastResetCounters = System.currentTimeMillis();
85 torben 399 }
86    
87     }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20