package dk.thoerup.circuitbreaker; import java.util.*; import dk.thoerup.circuitbreaker.notification.*; public class LoggingCircuitBreaker extends AccountingCircuitBreaker { private LinkedList log = new LinkedList(); final int maxSize = 50; public class LogEntry { public long time; public Notifier.Event event; public LogEntry(Notifier.Event evnt) { this.event = evnt; this.time = System.currentTimeMillis(); } } public LoggingCircuitBreaker(String name, int threshold, long timeoutMS) { super(name, threshold, timeoutMS); } @Override public void tripBreaker() { super.tripBreaker(); addEntry(Notifier.Event.BreakerTripped); } @Override public void retripBreaker() { super.retripBreaker(); addEntry(Notifier.Event.BreakerRetripped); } @Override public void reset() { super.reset(); addEntry(Notifier.Event.BreakerReset); } private void addEntry(Notifier.Event event) { synchronized(this) { log.addFirst( new LogEntry(event) ); if(log.size() > maxSize) { log.removeLast(); } } } public LinkedList getLog() { synchronized(this) { return new LinkedList(log); //return a copy so caller can to whatever he wants, when he wants } } }