/[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 404 - (hide annotations) (download)
Wed Oct 7 04:17:45 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/curcuitbreaker/AccountingCircuitBreaker.java
File size: 1811 byte(s)
Better naming

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     }
17    
18     @Override
19     public Object invoke(CircuitInvocation invocation) throws Throwable {
20     Object result;
21     try {
22 torben 403 totalCallCount.incrementAndGet();
23 torben 399 result = super.invoke(invocation);
24     } catch (Throwable t) {
25     if (t instanceof CircuitBreakerException) {
26     blockCount.incrementAndGet();
27     } else {
28 torben 403 totalFailureCount.incrementAndGet();
29 torben 399 }
30     throw t;
31     }
32    
33     return result;
34     }
35    
36     @Override
37     public void tripBreaker() {
38     super.tripBreaker();
39     tripCount.incrementAndGet();
40     }
41    
42     public int getTripCount() {
43     return tripCount.get();
44     }
45    
46     public int getBlockCount() {
47     return blockCount.get();
48     }
49    
50 torben 403 public int getTotalFailureCount() {
51     return totalFailureCount.get();
52 torben 399 }
53    
54 torben 404 public long getLastResetCounters() {
55     return lastResetCounters;
56 torben 400 }
57    
58 torben 403 public int getTotalCallCount() {
59     return totalCallCount.get();
60     }
61    
62 torben 399 public void resetCounters() {
63     tripCount.set(0);
64     blockCount.set(0);
65 torben 403 totalFailureCount.set(0);
66     totalCallCount.set(0);
67 torben 400
68 torben 404 lastResetCounters = System.currentTimeMillis();
69 torben 399 }
70    
71     }

  ViewVC Help
Powered by ViewVC 1.1.20