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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2569 - (show annotations) (download)
Tue Jun 9 08:55:10 2015 UTC (8 years, 11 months ago) by torben
File size: 2043 byte(s)
Statistics should be added by composition instead of inheritance
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
8 @Deprecated
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 Event event;
18 public int count = 1;
19
20 public LogEntry(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 == 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(Event.BreakerTripped);
54 }
55
56
57 @Override
58 public void retripBreaker() {
59 super.retripBreaker();
60 addEntry(Event.BreakerRetripped);
61 }
62
63
64 @Override
65 public void reset() {
66 super.reset();
67 addEntry(Event.BreakerReset);
68 }
69
70 private void addEntry(Event event) {
71 synchronized(this) {
72
73 if (event != 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 != 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