--- CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/05 19:46:15 394 +++ CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java 2009/10/07 07:07:00 409 @@ -2,10 +2,15 @@ import java.util.logging.Logger; +import dk.thoerup.curcuitbreaker.notification.Notifier; +import dk.thoerup.curcuitbreaker.notification.NullNotifier; + /* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java * * 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) { @@ -49,6 +54,8 @@ private String name; + private Notifier notifier = new NullNotifier(); + public CircuitBreaker(String name, int threshold, long timeoutMS) { closed.setThreshold(threshold); open.setTimeout(timeoutMS); @@ -78,33 +85,69 @@ public void tripBreaker() { synchronized(this) { - currentState = open; open.trip(); + currentState = open; - logger.warning("Circuitbreaker tripBreaker - " + name); - } - + notifier.sendNotification(name, Notifier.Event.BreakerTripped); + } } public void attemptReset() { synchronized(this) { currentState = halfOpen; - - logger.warning("Circuitbreaker attemptReset - " + name); + notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); } + } public void reset() { synchronized(this) { currentState = closed; - - logger.warning("Circuitbreaker reset - " + name); + notifier.sendNotification(name, Notifier.Event.BreakerReset); } } + private CircuitBreakerState getState() { synchronized(this) { return currentState; } } + + public String getName() { + return name; + } + + public String getStateName() { + return getState().getName(); + } + + public int getThreshold() { + return closed.getThreshold(); + } + + 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.toString(); + } + }