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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20