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

Annotation of /miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/LoggingCircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1295 - (hide annotations) (download)
Sat Apr 16 21:00:08 2011 UTC (13 years, 2 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/circuitbreaker/LoggingCircuitBreaker.java
File size: 2135 byte(s)
Take #2 at logging re-trips
1 torben 1285 package dk.thoerup.circuitbreaker;
2    
3 torben 1293 import java.util.Date;
4     import java.util.LinkedList;
5 torben 1285
6 torben 1293 import dk.thoerup.circuitbreaker.notification.Notifier;
7    
8 torben 1285 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 torben 1293 public Notifier.Event event;
17     public int count = 0;
18 torben 1285
19     public LogEntry(Notifier.Event evnt) {
20     this.event = evnt;
21     this.time = System.currentTimeMillis();
22     }
23 torben 1293
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 torben 1285 }
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 torben 1288
55 torben 1293
56 torben 1285 @Override
57     public void retripBreaker() {
58     super.retripBreaker();
59     addEntry(Notifier.Event.BreakerRetripped);
60     }
61    
62 torben 1293
63 torben 1285 @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 torben 1295
72     if (event != Notifier.Event.BreakerRetripped ) {
73     log.addFirst( new LogEntry(event) ); //trip and reset are added unconditionally
74     } else {
75 torben 1293
76 torben 1295 if (log.size() == 0 || log.getFirst().event != Notifier.Event.BreakerRetripped) {
77     log.addFirst( new LogEntry(event) );
78     } else {
79     log.getFirst().newRetrip();
80     }
81     }
82    
83     if(log.size() > maxSize) {
84     log.removeLast();
85     }
86    
87 torben 1285 }
88     }
89    
90 torben 1292 public void clearLog() {
91     synchronized(this) {
92     log.clear();
93     }
94     }
95    
96 torben 1285 public LinkedList<LogEntry> getLog() {
97     synchronized(this) {
98     return new LinkedList<LogEntry>(log); //return a copy so caller can to whatever he wants, when he wants
99     }
100     }
101    
102     }

  ViewVC Help
Powered by ViewVC 1.1.20