1 |
package dk.thoerup.circuitbreaker;
|
2 |
|
3 |
import java.util.Date;
|
4 |
import java.util.LinkedList;
|
5 |
|
6 |
import dk.thoerup.circuitbreaker.notification.Notifier;
|
7 |
|
8 |
public class LoggingCircuitBreaker extends AccountingCircuitBreaker {
|
9 |
|
10 |
private LinkedList<LogEntry> log = new LinkedList<LogEntry>();
|
11 |
|
12 |
final int maxSize = 50;
|
13 |
|
14 |
public class LogEntry {
|
15 |
public long time;
|
16 |
public Notifier.Event event;
|
17 |
public int count = 0;
|
18 |
|
19 |
public LogEntry(Notifier.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 == Notifier.Event.BreakerRetripped) {
|
35 |
str += ( ", " + count + " re-trips" );
|
36 |
}
|
37 |
|
38 |
return str ;
|
39 |
}
|
40 |
}
|
41 |
|
42 |
|
43 |
|
44 |
public LoggingCircuitBreaker(String name, int threshold, long timeoutMS) {
|
45 |
super(name, threshold, timeoutMS);
|
46 |
|
47 |
}
|
48 |
|
49 |
@Override
|
50 |
public void tripBreaker() {
|
51 |
super.tripBreaker();
|
52 |
addEntry(Notifier.Event.BreakerTripped);
|
53 |
}
|
54 |
|
55 |
|
56 |
@Override
|
57 |
public void retripBreaker() {
|
58 |
super.retripBreaker();
|
59 |
addEntry(Notifier.Event.BreakerRetripped);
|
60 |
}
|
61 |
|
62 |
|
63 |
@Override
|
64 |
public void reset() {
|
65 |
super.reset();
|
66 |
addEntry(Notifier.Event.BreakerReset);
|
67 |
}
|
68 |
|
69 |
private void addEntry(Notifier.Event event) {
|
70 |
synchronized(this) {
|
71 |
|
72 |
if (log.getFirst().event != Notifier.Event.BreakerRetripped) {
|
73 |
|
74 |
log.addFirst( new LogEntry(event) );
|
75 |
if(log.size() > maxSize) {
|
76 |
log.removeLast();
|
77 |
}
|
78 |
|
79 |
} else {
|
80 |
log.getFirst().newRetrip();
|
81 |
}
|
82 |
}
|
83 |
}
|
84 |
|
85 |
public void clearLog() {
|
86 |
synchronized(this) {
|
87 |
log.clear();
|
88 |
}
|
89 |
}
|
90 |
|
91 |
public LinkedList<LogEntry> getLog() {
|
92 |
synchronized(this) {
|
93 |
return new LinkedList<LogEntry>(log); //return a copy so caller can to whatever he wants, when he wants
|
94 |
}
|
95 |
}
|
96 |
|
97 |
}
|