--- CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/18 07:57:02 443 +++ CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java 2011/04/11 16:05:10 1289 @@ -1,8 +1,12 @@ -package dk.thoerup.curcuitbreaker; +package dk.thoerup.circuitbreaker; -import dk.thoerup.curcuitbreaker.notification.Notifier; -import dk.thoerup.curcuitbreaker.notification.NullNotifier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import dk.thoerup.circuitbreaker.notification.NotiferHelper; +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 +35,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; @@ -51,6 +55,7 @@ private String name; + private ExecutorService executor = null; private Notifier notifier = new NullNotifier(); public CircuitBreaker(String name, int threshold, long timeoutMS) { @@ -58,12 +63,19 @@ open.setTimeout(timeoutMS); this.name = name; - - reset(); + + //set correct intial state + internalReset(); + } + + public synchronized void shutdown() { + if (executor != null) { + executor.shutdown(); + } } - public Object invoke(CircuitInvocation invocation) throws Throwable + public Object invoke(CircuitInvocation invocation) throws Exception { Object result = null; try @@ -72,30 +84,39 @@ 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(this, event); } - } + } } - + public void attemptReset() { synchronized(this) { if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? currentState = halfOpen; - notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); + notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset); } } @@ -104,12 +125,18 @@ public void reset() { synchronized(this) { if (currentState != closed) { // TODO: Is this conditional necessary ?? - currentState = closed; - notifier.sendNotification(name, Notifier.Event.BreakerReset); + internalReset(); + notifier.sendNotification(this, 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 +144,14 @@ } } + public boolean isClosed() { + return (getState() == closed); + } + + public boolean isOpen() { + return (getState() == open); + } + public String getName() { return name; } @@ -153,8 +188,18 @@ this.notifier = notifier; } - public String getNotifierName() { - return notifier.getClass().getName(); + public String getNotifierName() { + return NotiferHelper.getName(notifier); + } + + public synchronized ExecutorService getExecutor() { + + if (executor == null) { + executor = Executors.newFixedThreadPool(1); + } + + return executor; + } }