--- CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/06 05:22:40 397 +++ CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/08 20:39:39 424 @@ -2,6 +2,9 @@ import java.util.logging.Logger; +import dk.thoerup.curcuitbreaker.notification.Notifier; +import dk.thoerup.curcuitbreaker.notification.NullNotifier; + /* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java * * example of how it can be used @@ -45,12 +48,14 @@ private CircuitBreakerState currentState; - private OpenState open = new OpenState(); - private HalfOpenState halfOpen = new HalfOpenState(); - private ClosedState closed = new ClosedState(); + private final OpenState open = new OpenState(); + private final HalfOpenState halfOpen = new HalfOpenState(); + private final ClosedState closed = new ClosedState(); private String name; + private Notifier notifier = new NullNotifier(); + public CircuitBreaker(String name, int threshold, long timeoutMS) { closed.setThreshold(threshold); open.setTimeout(timeoutMS); @@ -82,26 +87,23 @@ synchronized(this) { open.trip(); currentState = open; - - logger.warning("Circuitbreaker tripBreaker - " + name); - } - + notifier.sendNotification(name, Notifier.Event.BreakerTripped); + } } public void attemptReset() { synchronized(this) { currentState = halfOpen; - - logger.warning("Circuitbreaker attemptReset - " + name); + notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); } + } public void reset() { synchronized(this) { currentState = closed; - - logger.warning("Circuitbreaker reset - " + name); + notifier.sendNotification(name, Notifier.Event.BreakerReset); } } @@ -124,6 +126,10 @@ return closed.getThreshold(); } + public int getTimeout() { + return (int)open.getTimeout(); + } + public int getFailureCount() { if (getState() == closed) { return closed.getFailureCount(); @@ -139,5 +145,13 @@ return -1; } } + + public void setNotifier(Notifier notifier) { + this.notifier = notifier; + } + + public String getNotifierName() { + return notifier.getClass().getName(); + } }