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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20