--- CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/08 20:39:39 424 +++ CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/20 10:47:36 452 @@ -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; @@ -32,7 +31,7 @@ try { String s = (String) cb.invoke(new TestInvocation("http://rafiki/test")); response.getWriter().print(s); - } catch (Throwable e) { + } catch (Exception e) { logger.warning( e.getMessage() ); response.sendError(500); return; @@ -43,10 +42,8 @@ public class CircuitBreaker{ - Logger logger = Logger.getLogger(CircuitBreaker.class.getName()); - - private CircuitBreakerState currentState; + private volatile CircuitBreakerState currentState; private final OpenState open = new OpenState(); private final HalfOpenState halfOpen = new HalfOpenState(); @@ -66,7 +63,7 @@ } - public Object invoke(CircuitInvocation invocation) throws Throwable + public Object invoke(CircuitInvocation invocation) throws Exception { Object result = null; try @@ -75,35 +72,41 @@ result = invocation.proceed(); getState().postInvoke(this); } - catch(Throwable t) + catch(Exception e) { - getState().onError(this, t); - throw t; + getState().onError(this, e); + throw e; } return result; } 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; }