/[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 460 - (hide annotations) (download)
Wed Oct 21 07:56:37 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/curcuitbreaker/AccountingCircuitBreaker.java
File size: 2059 byte(s)
AccountingCircuitBreaker, should also keep track on when it last was tripped
1 torben 399 package dk.thoerup.curcuitbreaker;
2    
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 460 private AtomicLong lastTrip = new AtomicLong(0);
13 torben 404 private long lastResetCounters = 0;
14 torben 400
15 torben 399 public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
16     super(name, threshold, timeoutMS);
17 torben 444
18     resetCounters();
19 torben 399 }
20    
21     @Override
22 torben 450 public Object invoke(CircuitInvocation invocation) throws Exception {
23 torben 399 Object result;
24     try {
25 torben 403 totalCallCount.incrementAndGet();
26 torben 399 result = super.invoke(invocation);
27 torben 450 } catch (Exception e) {
28     if (e instanceof CircuitBreakerException) {
29 torben 399 blockCount.incrementAndGet();
30     } else {
31 torben 403 totalFailureCount.incrementAndGet();
32 torben 399 }
33 torben 450 throw e;
34 torben 399 }
35    
36     return result;
37     }
38    
39     @Override
40     public void tripBreaker() {
41     super.tripBreaker();
42     tripCount.incrementAndGet();
43 torben 460 lastTrip.set( System.currentTimeMillis() );
44 torben 399 }
45    
46     public int getTripCount() {
47     return tripCount.get();
48     }
49    
50     public int getBlockCount() {
51     return blockCount.get();
52     }
53    
54 torben 403 public int getTotalFailureCount() {
55     return totalFailureCount.get();
56 torben 399 }
57    
58 torben 404 public long getLastResetCounters() {
59     return lastResetCounters;
60 torben 400 }
61    
62 torben 460 public long getLastTrip() {
63     return lastTrip.get();
64     }
65    
66 torben 403 public int getTotalCallCount() {
67     return totalCallCount.get();
68     }
69    
70 torben 399 public void resetCounters() {
71     tripCount.set(0);
72     blockCount.set(0);
73 torben 403 totalFailureCount.set(0);
74     totalCallCount.set(0);
75 torben 460 lastTrip.set(0);
76 torben 400
77 torben 404 lastResetCounters = System.currentTimeMillis();
78 torben 399 }
79    
80     }

  ViewVC Help
Powered by ViewVC 1.1.20