--- CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/07 07:07:00 409 +++ CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/19 13:57:42 447 @@ -1,6 +1,5 @@ package dk.thoerup.curcuitbreaker; -import java.util.logging.Logger; import dk.thoerup.curcuitbreaker.notification.Notifier; import dk.thoerup.curcuitbreaker.notification.NullNotifier; @@ -43,14 +42,12 @@ public class CircuitBreaker{ - Logger logger = Logger.getLogger(CircuitBreaker.class.getName()); - - private CircuitBreakerState currentState; + private volatile 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; @@ -85,25 +82,31 @@ public void tripBreaker() { synchronized(this) { - open.trip(); - currentState = open; + if (currentState != open) { // TODO:Is this conditional necessary ?? + open.trip(); + currentState = open; - notifier.sendNotification(name, Notifier.Event.BreakerTripped); + notifier.sendNotification(name, Notifier.Event.BreakerTripped); + } } } public void attemptReset() { synchronized(this) { - currentState = halfOpen; - notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); + if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? + currentState = halfOpen; + notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); + } } } public void reset() { synchronized(this) { - currentState = closed; - notifier.sendNotification(name, Notifier.Event.BreakerReset); + if (currentState != closed) { // TODO: Is this conditional necessary ?? + currentState = closed; + notifier.sendNotification(name, Notifier.Event.BreakerReset); + } } } @@ -114,6 +117,14 @@ } } + public boolean isClosed() { + return (getState() == closed); + } + + public boolean isOpen() { + return (getState() == open); + } + public String getName() { return name; } @@ -126,6 +137,10 @@ return closed.getThreshold(); } + public int getTimeout() { + return (int)open.getTimeout(); + } + public int getFailureCount() { if (getState() == closed) { return closed.getFailureCount(); @@ -147,7 +162,7 @@ } public String getNotifierName() { - return notifier.toString(); + return notifier.getClass().getName(); } }