/[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 624 - (hide annotations) (download)
Mon Mar 8 09:46:10 2010 UTC (14 years, 3 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/circuitbreaker/AccountingCircuitBreaker.java
File size: 2514 byte(s)
Also keep track on when things went back to normal
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 624
13 torben 460 private AtomicLong lastTrip = new AtomicLong(0);
14 torben 621 private AtomicLong lastFailure = new AtomicLong(0);
15 torben 624 private AtomicLong lastReset = new AtomicLong(0);
16    
17 torben 404 private long lastResetCounters = 0;
18 torben 400
19 torben 399 public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
20     super(name, threshold, timeoutMS);
21 torben 444
22     resetCounters();
23 torben 399 }
24    
25     @Override
26 torben 450 public Object invoke(CircuitInvocation invocation) throws Exception {
27 torben 399 Object result;
28     try {
29 torben 403 totalCallCount.incrementAndGet();
30 torben 399 result = super.invoke(invocation);
31 torben 450 } catch (Exception e) {
32     if (e instanceof CircuitBreakerException) {
33 torben 399 blockCount.incrementAndGet();
34     } else {
35 torben 403 totalFailureCount.incrementAndGet();
36 torben 621 lastFailure.set( System.currentTimeMillis() );
37 torben 399 }
38 torben 450 throw e;
39 torben 399 }
40    
41     return result;
42     }
43    
44     @Override
45     public void tripBreaker() {
46     super.tripBreaker();
47     tripCount.incrementAndGet();
48 torben 460 lastTrip.set( System.currentTimeMillis() );
49 torben 399 }
50    
51 torben 624
52    
53     @Override
54     public void reset() {
55     super.reset();
56     lastReset.set( System.currentTimeMillis());
57     }
58    
59 torben 399 public int getTripCount() {
60     return tripCount.get();
61     }
62    
63     public int getBlockCount() {
64     return blockCount.get();
65     }
66    
67 torben 403 public int getTotalFailureCount() {
68     return totalFailureCount.get();
69 torben 399 }
70    
71 torben 404 public long getLastResetCounters() {
72     return lastResetCounters;
73 torben 400 }
74    
75 torben 460 public long getLastTrip() {
76     return lastTrip.get();
77     }
78    
79 torben 621 public long getLastFailure() {
80     return lastFailure.get();
81     }
82    
83 torben 624 public long getLastReset() {
84     return lastReset.get();
85     }
86    
87 torben 403 public int getTotalCallCount() {
88     return totalCallCount.get();
89     }
90    
91 torben 399 public void resetCounters() {
92     tripCount.set(0);
93     blockCount.set(0);
94 torben 403 totalFailureCount.set(0);
95     totalCallCount.set(0);
96 torben 460 lastTrip.set(0);
97 torben 624 lastReset.set(0);
98 torben 621 lastFailure.set(0);
99 torben 400
100 torben 404 lastResetCounters = System.currentTimeMillis();
101 torben 399 }
102    
103     }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20