/[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 400 - (hide annotations) (download)
Tue Oct 6 13:21:12 2009 UTC (14 years, 8 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/curcuitbreaker/AccountingCircuitBreaker.java
File size: 1553 byte(s)
Keep track of the last counter reset
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     private AtomicInteger failureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
10    
11 torben 400 private long lastReset = 0;
12    
13 torben 399 public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) {
14     super(name, threshold, timeoutMS);
15     }
16    
17     @Override
18     public Object invoke(CircuitInvocation invocation) throws Throwable {
19     Object result;
20     try {
21     result = super.invoke(invocation);
22     } catch (Throwable t) {
23     if (t instanceof CircuitBreakerException) {
24     blockCount.incrementAndGet();
25     } else {
26     failureCount.incrementAndGet();
27     }
28     throw t;
29     }
30    
31     return result;
32     }
33    
34     @Override
35     public void tripBreaker() {
36     super.tripBreaker();
37     tripCount.incrementAndGet();
38     }
39    
40     public int getTripCount() {
41     return tripCount.get();
42     }
43    
44     public int getBlockCount() {
45     return blockCount.get();
46     }
47    
48     public int getFailureCount() {
49     return failureCount.get();
50     }
51    
52 torben 400 public long getLastReset() {
53     return lastReset;
54     }
55    
56 torben 399 public void resetCounters() {
57     tripCount.set(0);
58     blockCount.set(0);
59     failureCount.set(0);
60 torben 400
61     lastReset = System.currentTimeMillis();
62 torben 399 }
63    
64     }

  ViewVC Help
Powered by ViewVC 1.1.20