--- CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/05 19:44:40 393 +++ CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java 2010/06/21 17:16:46 871 @@ -1,11 +1,15 @@ -package dk.thoerup.curcuitbreaker; +package dk.thoerup.circuitbreaker; -import java.util.logging.Logger; +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 + * + * example of how it can be used -/* example of how it can be used -protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + private CircuitBreaker cb = new CircuitBreaker("test", 5, 10000); + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { class TestInvocation implements CircuitInvocation { String url; public TestInvocation(String url) { @@ -27,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; @@ -38,28 +42,29 @@ 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; + private Notifier notifier = new NullNotifier(); + public CircuitBreaker(String name, int threshold, long timeoutMS) { closed.setThreshold(threshold); 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 @@ -68,43 +73,112 @@ 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) { - currentState = open; - open.trip(); - - logger.warning("Circuitbreaker tripBreaker - " + name); - } + 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, event); + } + } + } + public void attemptReset() { synchronized(this) { - currentState = halfOpen; - - logger.warning("Circuitbreaker attemptReset - " + name); + if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? + currentState = halfOpen; + notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); + } } + } public void reset() { synchronized(this) { - currentState = closed; - - logger.warning("Circuitbreaker reset - " + name); + if (currentState != closed) { // TODO: Is this conditional necessary ?? + 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) { return currentState; } } + + public boolean isClosed() { + return (getState() == closed); + } + + public boolean isOpen() { + return (getState() == open); + } + + public String getName() { + return name; + } + + public String getStateName() { + return getState().getName(); + } + + public int getThreshold() { + return closed.getThreshold(); + } + + public int getTimeout() { + return (int)open.getTimeout(); + } + + public int getFailureCount() { + if (getState() == closed) { + return closed.getFailureCount(); + } else { + return -1; + } + } + + public long getElapsed() { + if (getState() == open) { + return open.getElapsed(); + } else { + return -1; + } + } + + public void setNotifier(Notifier notifier) { + this.notifier = notifier; + } + + public String getNotifierName() { + return notifier.getClass().getName(); + } + }