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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20