/[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 444 - (hide annotations) (download)
Sun Oct 18 09:15:52 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/curcuitbreaker/AccountingCircuitBreaker.java
File size: 1833 byte(s)
Reset counters and timestamp on creation

1 torben 399 package dk.thoerup.curcuitbreaker;
2    
3     import java.util.concurrent.atomic.AtomicInteger;
4    
5     public class AccountingCircuitBreaker extends CircuitBreaker {
6    
7     private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped
8     private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend
9 torben 403 private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
10     private AtomicInteger totalCallCount = new AtomicInteger(0);
11 torben 399
12 torben 404 private long lastResetCounters = 0;
13 torben 400
14 torben 399 public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
15     super(name, threshold, timeoutMS);
16 torben 444
17     resetCounters();
18 torben 399 }
19    
20     @Override
21     public Object invoke(CircuitInvocation invocation) throws Throwable {
22     Object result;
23     try {
24 torben 403 totalCallCount.incrementAndGet();
25 torben 399 result = super.invoke(invocation);
26     } catch (Throwable t) {
27     if (t instanceof CircuitBreakerException) {
28     blockCount.incrementAndGet();
29     } else {
30 torben 403 totalFailureCount.incrementAndGet();
31 torben 399 }
32     throw t;
33     }
34    
35     return result;
36     }
37    
38     @Override
39     public void tripBreaker() {
40     super.tripBreaker();
41     tripCount.incrementAndGet();
42     }
43    
44     public int getTripCount() {
45     return tripCount.get();
46     }
47    
48     public int getBlockCount() {
49     return blockCount.get();
50     }
51    
52 torben 403 public int getTotalFailureCount() {
53     return totalFailureCount.get();
54 torben 399 }
55    
56 torben 404 public long getLastResetCounters() {
57     return lastResetCounters;
58 torben 400 }
59    
60 torben 403 public int getTotalCallCount() {
61     return totalCallCount.get();
62     }
63    
64 torben 399 public void resetCounters() {
65     tripCount.set(0);
66     blockCount.set(0);
67 torben 403 totalFailureCount.set(0);
68     totalCallCount.set(0);
69 torben 400
70 torben 404 lastResetCounters = System.currentTimeMillis();
71 torben 399 }
72    
73     }

  ViewVC Help
Powered by ViewVC 1.1.20