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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1293 - (hide annotations) (download)
Sat Apr 16 11:11:07 2011 UTC (13 years, 2 months ago) by torben
File size: 1958 byte(s)
Log retrips
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 1293
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 torben 1285 }
82     }
83     }
84    
85 torben 1292 public void clearLog() {
86     synchronized(this) {
87     log.clear();
88     }
89     }
90    
91 torben 1285 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     }

  ViewVC Help
Powered by ViewVC 1.1.20