/[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 1314 - (hide annotations) (download)
Tue Apr 19 17:12:27 2011 UTC (13 years, 1 month ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/circuitbreaker/AccountingCircuitBreaker.java
File size: 3000 byte(s)
Switch entirely to BreakerConfig inits
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 torben 1314 import dk.thoerup.circuitbreaker.config.BreakerConfig;
7    
8 torben 399 public class AccountingCircuitBreaker extends CircuitBreaker {
9    
10     private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped
11 torben 864 private AtomicInteger retripCount = new AtomicInteger(0);
12 torben 399 private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend
13 torben 403 private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
14     private AtomicInteger totalCallCount = new AtomicInteger(0);
15 torben 624
16 torben 460 private AtomicLong lastTrip = new AtomicLong(0);
17 torben 864 private AtomicLong lastRetrip = new AtomicLong(0);
18 torben 621 private AtomicLong lastFailure = new AtomicLong(0);
19 torben 624 private AtomicLong lastReset = new AtomicLong(0);
20    
21 torben 404 private long lastResetCounters = 0;
22 torben 400
23 torben 1314 public AccountingCircuitBreaker(String name, BreakerConfig config) {
24     super(name, config);
25 torben 444
26     resetCounters();
27 torben 399 }
28    
29     @Override
30 torben 450 public Object invoke(CircuitInvocation invocation) throws Exception {
31 torben 399 Object result;
32     try {
33 torben 403 totalCallCount.incrementAndGet();
34 torben 399 result = super.invoke(invocation);
35 torben 450 } catch (Exception e) {
36     if (e instanceof CircuitBreakerException) {
37 torben 399 blockCount.incrementAndGet();
38     } else {
39 torben 403 totalFailureCount.incrementAndGet();
40 torben 621 lastFailure.set( System.currentTimeMillis() );
41 torben 399 }
42 torben 450 throw e;
43 torben 399 }
44    
45     return result;
46     }
47    
48     @Override
49     public void tripBreaker() {
50     super.tripBreaker();
51     tripCount.incrementAndGet();
52 torben 460 lastTrip.set( System.currentTimeMillis() );
53 torben 399 }
54    
55 torben 864 @Override
56     public void retripBreaker() {
57     super.retripBreaker();
58     retripCount.incrementAndGet();
59     lastRetrip.set( System.currentTimeMillis() );
60     }
61 torben 624
62    
63     @Override
64     public void reset() {
65     super.reset();
66     lastReset.set( System.currentTimeMillis());
67     }
68    
69 torben 399 public int getTripCount() {
70     return tripCount.get();
71     }
72    
73 torben 864 public int getRetripCount() {
74     return retripCount.get();
75     }
76    
77 torben 399 public int getBlockCount() {
78     return blockCount.get();
79     }
80    
81 torben 403 public int getTotalFailureCount() {
82     return totalFailureCount.get();
83 torben 399 }
84    
85 torben 404 public long getLastResetCounters() {
86     return lastResetCounters;
87 torben 400 }
88    
89 torben 460 public long getLastTrip() {
90     return lastTrip.get();
91     }
92    
93 torben 864 public long getLastRetrip() {
94     return lastRetrip.get();
95     }
96    
97 torben 621 public long getLastFailure() {
98     return lastFailure.get();
99     }
100    
101 torben 624 public long getLastReset() {
102     return lastReset.get();
103     }
104    
105 torben 403 public int getTotalCallCount() {
106     return totalCallCount.get();
107     }
108    
109 torben 399 public void resetCounters() {
110     tripCount.set(0);
111 torben 864 retripCount.set(0);
112 torben 399 blockCount.set(0);
113 torben 403 totalFailureCount.set(0);
114     totalCallCount.set(0);
115 torben 460 lastTrip.set(0);
116 torben 864 lastRetrip.set(0);
117 torben 624 lastReset.set(0);
118 torben 621 lastFailure.set(0);
119 torben 400
120 torben 404 lastResetCounters = System.currentTimeMillis();
121 torben 399 }
122    
123     }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20