--- CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java 2010/03/08 10:01:25 626 +++ miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java 2015/06/09 09:28:14 2572 @@ -1,8 +1,16 @@ package dk.thoerup.circuitbreaker; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import dk.thoerup.circuitbreaker.config.BreakerConfig; +import dk.thoerup.circuitbreaker.config.StaticConfig; +import dk.thoerup.circuitbreaker.notification.NotiferHelper; import dk.thoerup.circuitbreaker.notification.Notifier; import dk.thoerup.circuitbreaker.notification.NullNotifier; +import dk.thoerup.circuitbreaker.statistics.NullStatistics; +import dk.thoerup.circuitbreaker.statistics.Statistics; /* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java * @@ -51,22 +59,36 @@ private String name; + private ExecutorService executor = null; private Notifier notifier = new NullNotifier(); + private Statistics stats = new NullStatistics(); + + @Deprecated + public CircuitBreaker(String name, int threshold, int timeoutMS) { + this(name, new StaticConfig(threshold, timeoutMS) ); + } - public CircuitBreaker(String name, int threshold, long timeoutMS) { - closed.setThreshold(threshold); - open.setTimeout(timeoutMS); + public CircuitBreaker(String name, BreakerConfig config) { + closed.setThreshold(config); + open.setTimeout(config); this.name = name; - // Initial state is closed - like a reset() was called initially - closed.resetFailureCount(); - currentState = closed; + //set correct intial state + internalReset(); + } + + public synchronized void shutdown() { + if (executor != null) { + executor.shutdown(); + } } public Object invoke(CircuitInvocation invocation) throws Exception { + stats.addStatistics(Event.Invocation); + Object result = null; try { @@ -75,7 +97,13 @@ getState().postInvoke(this); } catch(Exception e) - { + { + if (e instanceof CircuitBreakerException) { + stats.addStatistics(Event.InvocationBlocked); + } else { + stats.addStatistics(Event.InvocationFailure); + } + getState().onError(this, e); throw e; } @@ -83,21 +111,32 @@ } public void tripBreaker() { - synchronized(this) { + commonTripBreaker(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(Event.BreakerRetripped); + } + + private void commonTripBreaker(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); + stats.addStatistics(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, Event.BreakerAttemptReset); + stats.addStatistics(Event.BreakerAttemptReset); } } @@ -106,13 +145,19 @@ public void reset() { synchronized(this) { if (currentState != closed) { // TODO: Is this conditional necessary ?? - closed.resetFailureCount(); - currentState = closed; - notifier.sendNotification(name, Notifier.Event.BreakerReset); + internalReset(); + notifier.sendNotification(this, Event.BreakerReset); + stats.addStatistics(Event.BreakerReset); } } } + //This one actually sets the correct closed/reset state + private void internalReset() { + closed.resetFailureCount(); + currentState = closed; + } + private CircuitBreakerState getState() { synchronized(this) { @@ -161,11 +206,31 @@ } public void setNotifier(Notifier notifier) { - this.notifier = notifier; + synchronized(this) { + this.notifier = notifier; + } + } + + public String getNotifierName() { + return NotiferHelper.getName(notifier); } - public String getNotifierName() { - return notifier.getClass().getName(); + public void setStatistics(Statistics newStats) { + this.stats = newStats; + } + + public Statistics getStatistics() { + return this.stats; + } + + public synchronized ExecutorService getExecutor() { + + if (executor == null) { + executor = Executors.newFixedThreadPool(1); + } + + return executor; + } }