--- CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/09 06:56:21 427 +++ CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java 2010/03/08 10:12:59 627 @@ -1,8 +1,8 @@ -package dk.thoerup.curcuitbreaker; +package dk.thoerup.circuitbreaker; -import dk.thoerup.curcuitbreaker.notification.Notifier; -import dk.thoerup.curcuitbreaker.notification.NullNotifier; +import dk.thoerup.circuitbreaker.notification.Notifier; +import dk.thoerup.circuitbreaker.notification.NullNotifier; /* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java * @@ -31,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,7 +43,7 @@ public class CircuitBreaker{ - private CircuitBreakerState currentState; + private volatile CircuitBreakerState currentState; private final OpenState open = new OpenState(); private final HalfOpenState halfOpen = new HalfOpenState(); @@ -58,12 +58,13 @@ open.setTimeout(timeoutMS); this.name = name; - - reset(); + + //set correct intial state + internalReset(); } - public Object invoke(CircuitInvocation invocation) throws Throwable + public Object invoke(CircuitInvocation invocation) throws Exception { Object result = null; try @@ -72,10 +73,10 @@ 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; } @@ -104,12 +105,18 @@ public void reset() { synchronized(this) { if (currentState != closed) { // TODO: Is this conditional necessary ?? - currentState = closed; + internalReset(); notifier.sendNotification(name, Notifier.Event.BreakerReset); } } } + //This one actually sets the correct closed/reset state + private void internalReset() { + closed.resetFailureCount(); + currentState = closed; + } + private CircuitBreakerState getState() { synchronized(this) { @@ -117,6 +124,14 @@ } } + public boolean isClosed() { + return (getState() == closed); + } + + public boolean isOpen() { + return (getState() == open); + } + public String getName() { return name; }