1 |
package dk.thoerup.circuitbreaker.statistics;
|
2 |
|
3 |
import java.util.Date;
|
4 |
import java.util.LinkedList;
|
5 |
|
6 |
import dk.thoerup.circuitbreaker.Event;
|
7 |
|
8 |
public class LoggingStatistics extends AccountingStatistics {
|
9 |
|
10 |
private LinkedList<LogEntry> log = new LinkedList<LogEntry>();
|
11 |
|
12 |
static final int MAX_SIZE = 50;
|
13 |
|
14 |
public static class LogEntry {
|
15 |
public long time;
|
16 |
public Event event;
|
17 |
public int count = 1;
|
18 |
|
19 |
public LogEntry(Event evnt) {
|
20 |
this.event = evnt;
|
21 |
this.time = System.currentTimeMillis();
|
22 |
}
|
23 |
|
24 |
public void newRetrip() {//
|
25 |
count++;
|
26 |
time = System.currentTimeMillis();
|
27 |
}
|
28 |
|
29 |
@Override
|
30 |
public String toString() {
|
31 |
|
32 |
String str = new Date(time).toString() + (" : ") + event;
|
33 |
|
34 |
if (event == Event.BreakerRetripped) {
|
35 |
str += ( ", " + count + " re-trips" );
|
36 |
}
|
37 |
|
38 |
return str ;
|
39 |
}
|
40 |
}
|
41 |
|
42 |
|
43 |
@Override
|
44 |
public void addStatistics(Event event) {
|
45 |
|
46 |
super.addStatistics(event);
|
47 |
|
48 |
switch(event) {
|
49 |
|
50 |
case BreakerReset:
|
51 |
addEntry(Event.BreakerReset);
|
52 |
break;
|
53 |
case BreakerRetripped:
|
54 |
addEntry(Event.BreakerRetripped);
|
55 |
break;
|
56 |
case BreakerTripped:
|
57 |
addEntry(Event.BreakerTripped);
|
58 |
break;
|
59 |
|
60 |
/*case InvocationBlocked:
|
61 |
break;
|
62 |
case InvocationFailure:
|
63 |
break;
|
64 |
case Invocation:
|
65 |
break;
|
66 |
case BreakerAttemptReset:
|
67 |
break;
|
68 |
*/
|
69 |
|
70 |
default:
|
71 |
break;
|
72 |
}
|
73 |
}
|
74 |
|
75 |
private void addEntry(Event event) {
|
76 |
synchronized(this) {
|
77 |
|
78 |
if (event != Event.BreakerRetripped ) {
|
79 |
log.addFirst( new LogEntry(event) ); //trip and reset are added unconditionally
|
80 |
} else {
|
81 |
|
82 |
if (log.size() == 0 || log.getFirst().event != Event.BreakerRetripped) {
|
83 |
log.addFirst( new LogEntry(event) );
|
84 |
} else {
|
85 |
log.getFirst().newRetrip();
|
86 |
}
|
87 |
}
|
88 |
|
89 |
if(log.size() > MAX_SIZE) {
|
90 |
log.removeLast();
|
91 |
}
|
92 |
|
93 |
}
|
94 |
}
|
95 |
|
96 |
public void clearLog() {
|
97 |
synchronized(this) {
|
98 |
log.clear();
|
99 |
}
|
100 |
}
|
101 |
|
102 |
public LinkedList<LogEntry> getLog() {
|
103 |
synchronized(this) {
|
104 |
return new LinkedList<LogEntry>(log); //return a copy so caller can to whatever he wants, when he wants
|
105 |
}
|
106 |
}
|
107 |
|
108 |
}
|