/[projects]/CircuitBreaker/src/dk/thoerup/circuitbreaker/AccountingCircuitBreaker.java
ViewVC logotype

Contents of /CircuitBreaker/src/dk/thoerup/circuitbreaker/AccountingCircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1314 - (show annotations) (download)
Tue Apr 19 17:12:27 2011 UTC (13 years, 1 month ago) by torben
File size: 3000 byte(s)
Switch entirely to BreakerConfig inits
1 package dk.thoerup.circuitbreaker;
2
3 import java.util.concurrent.atomic.AtomicInteger;
4 import java.util.concurrent.atomic.AtomicLong;
5
6 import dk.thoerup.circuitbreaker.config.BreakerConfig;
7
8 public class AccountingCircuitBreaker extends CircuitBreaker {
9
10 private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped
11 private AtomicInteger retripCount = new AtomicInteger(0);
12 private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend
13 private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
14 private AtomicInteger totalCallCount = new AtomicInteger(0);
15
16 private AtomicLong lastTrip = new AtomicLong(0);
17 private AtomicLong lastRetrip = new AtomicLong(0);
18 private AtomicLong lastFailure = new AtomicLong(0);
19 private AtomicLong lastReset = new AtomicLong(0);
20
21 private long lastResetCounters = 0;
22
23 public AccountingCircuitBreaker(String name, BreakerConfig config) {
24 super(name, config);
25
26 resetCounters();
27 }
28
29 @Override
30 public Object invoke(CircuitInvocation invocation) throws Exception {
31 Object result;
32 try {
33 totalCallCount.incrementAndGet();
34 result = super.invoke(invocation);
35 } catch (Exception e) {
36 if (e instanceof CircuitBreakerException) {
37 blockCount.incrementAndGet();
38 } else {
39 totalFailureCount.incrementAndGet();
40 lastFailure.set( System.currentTimeMillis() );
41 }
42 throw e;
43 }
44
45 return result;
46 }
47
48 @Override
49 public void tripBreaker() {
50 super.tripBreaker();
51 tripCount.incrementAndGet();
52 lastTrip.set( System.currentTimeMillis() );
53 }
54
55 @Override
56 public void retripBreaker() {
57 super.retripBreaker();
58 retripCount.incrementAndGet();
59 lastRetrip.set( System.currentTimeMillis() );
60 }
61
62
63 @Override
64 public void reset() {
65 super.reset();
66 lastReset.set( System.currentTimeMillis());
67 }
68
69 public int getTripCount() {
70 return tripCount.get();
71 }
72
73 public int getRetripCount() {
74 return retripCount.get();
75 }
76
77 public int getBlockCount() {
78 return blockCount.get();
79 }
80
81 public int getTotalFailureCount() {
82 return totalFailureCount.get();
83 }
84
85 public long getLastResetCounters() {
86 return lastResetCounters;
87 }
88
89 public long getLastTrip() {
90 return lastTrip.get();
91 }
92
93 public long getLastRetrip() {
94 return lastRetrip.get();
95 }
96
97 public long getLastFailure() {
98 return lastFailure.get();
99 }
100
101 public long getLastReset() {
102 return lastReset.get();
103 }
104
105 public int getTotalCallCount() {
106 return totalCallCount.get();
107 }
108
109 public void resetCounters() {
110 tripCount.set(0);
111 retripCount.set(0);
112 blockCount.set(0);
113 totalFailureCount.set(0);
114 totalCallCount.set(0);
115 lastTrip.set(0);
116 lastRetrip.set(0);
117 lastReset.set(0);
118 lastFailure.set(0);
119
120 lastResetCounters = System.currentTimeMillis();
121 }
122
123 }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20