--- CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/09 06:56:21 427 +++ CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java 2010/06/21 17:16:46 871 @@ -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,25 +73,34 @@ 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) { + commonTripBreaker(Notifier.Event.BreakerTripped); + } + + //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events + public void retripBreaker() { + commonTripBreaker(Notifier.Event.BreakerRetripped); + } + + private void commonTripBreaker(Notifier.Event event) { + synchronized(this) { if (currentState != open) { // TODO:Is this conditional necessary ?? open.trip(); currentState = open; - notifier.sendNotification(name, Notifier.Event.BreakerTripped); + notifier.sendNotification(name, event); } - } + } } - + public void attemptReset() { synchronized(this) { if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? @@ -104,12 +114,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 +133,14 @@ } } + public boolean isClosed() { + return (getState() == closed); + } + + public boolean isOpen() { + return (getState() == open); + } + public String getName() { return name; }