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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20