1 |
torben |
1285 |
package dk.thoerup.circuitbreaker;
|
2 |
|
|
|
3 |
|
|
import java.util.*;
|
4 |
|
|
import dk.thoerup.circuitbreaker.notification.*;
|
5 |
|
|
|
6 |
|
|
public class LoggingCircuitBreaker extends AccountingCircuitBreaker {
|
7 |
|
|
|
8 |
|
|
private LinkedList<LogEntry> log = new LinkedList<LogEntry>();
|
9 |
|
|
|
10 |
|
|
final int maxSize = 50;
|
11 |
|
|
|
12 |
|
|
public class LogEntry {
|
13 |
|
|
public long time;
|
14 |
|
|
public Notifier.Event event;
|
15 |
|
|
|
16 |
|
|
public LogEntry(Notifier.Event evnt) {
|
17 |
|
|
this.event = evnt;
|
18 |
|
|
this.time = System.currentTimeMillis();
|
19 |
|
|
}
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
public LoggingCircuitBreaker(String name, int threshold, long timeoutMS) {
|
25 |
|
|
super(name, threshold, timeoutMS);
|
26 |
|
|
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
@Override
|
30 |
|
|
public void tripBreaker() {
|
31 |
|
|
super.tripBreaker();
|
32 |
|
|
addEntry(Notifier.Event.BreakerTripped);
|
33 |
|
|
}
|
34 |
torben |
1288 |
|
35 |
|
|
/* don't log retrips - they are not as interesting as trips and resets
|
36 |
torben |
1285 |
@Override
|
37 |
|
|
public void retripBreaker() {
|
38 |
|
|
super.retripBreaker();
|
39 |
|
|
addEntry(Notifier.Event.BreakerRetripped);
|
40 |
|
|
}
|
41 |
torben |
1288 |
*/
|
42 |
torben |
1285 |
|
43 |
|
|
@Override
|
44 |
|
|
public void reset() {
|
45 |
|
|
super.reset();
|
46 |
|
|
addEntry(Notifier.Event.BreakerReset);
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
private void addEntry(Notifier.Event event) {
|
50 |
|
|
synchronized(this) {
|
51 |
|
|
log.addFirst( new LogEntry(event) );
|
52 |
|
|
if(log.size() > maxSize) {
|
53 |
|
|
log.removeLast();
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
}
|
57 |
|
|
|
58 |
torben |
1292 |
public void clearLog() {
|
59 |
|
|
synchronized(this) {
|
60 |
|
|
log.clear();
|
61 |
|
|
}
|
62 |
|
|
}
|
63 |
|
|
|
64 |
torben |
1285 |
public LinkedList<LogEntry> getLog() {
|
65 |
|
|
synchronized(this) {
|
66 |
|
|
return new LinkedList<LogEntry>(log); //return a copy so caller can to whatever he wants, when he wants
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
}
|