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

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

  ViewVC Help
Powered by ViewVC 1.1.20