--- CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java 2011/04/11 16:05:10 1289 +++ miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java 2015/06/09 09:28:14 2572 @@ -4,9 +4,13 @@ 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 * @@ -57,10 +61,16 @@ private ExecutorService executor = null; private Notifier notifier = new NullNotifier(); + private Statistics stats = new NullStatistics(); - public CircuitBreaker(String name, int threshold, long timeoutMS) { - closed.setThreshold(threshold); - open.setTimeout(timeoutMS); + @Deprecated + public CircuitBreaker(String name, int threshold, int timeoutMS) { + this(name, new StaticConfig(threshold, timeoutMS) ); + } + + public CircuitBreaker(String name, BreakerConfig config) { + closed.setThreshold(config); + open.setTimeout(config); this.name = name; @@ -77,6 +87,8 @@ public Object invoke(CircuitInvocation invocation) throws Exception { + stats.addStatistics(Event.Invocation); + Object result = null; try { @@ -85,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; } @@ -93,21 +111,22 @@ } public void tripBreaker() { - commonTripBreaker(Notifier.Event.BreakerTripped); + 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(Notifier.Event.BreakerRetripped); + commonTripBreaker(Event.BreakerRetripped); } - private void commonTripBreaker(Notifier.Event event) { + private void commonTripBreaker(Event event) { synchronized(this) { if (currentState != open) { // TODO:Is this conditional necessary ?? open.trip(); currentState = open; notifier.sendNotification(this, event); + stats.addStatistics(event); } } } @@ -116,7 +135,8 @@ synchronized(this) { if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? currentState = halfOpen; - notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset); + notifier.sendNotification(this, Event.BreakerAttemptReset); + stats.addStatistics(Event.BreakerAttemptReset); } } @@ -126,7 +146,8 @@ synchronized(this) { if (currentState != closed) { // TODO: Is this conditional necessary ?? internalReset(); - notifier.sendNotification(this, Notifier.Event.BreakerReset); + notifier.sendNotification(this, Event.BreakerReset); + stats.addStatistics(Event.BreakerReset); } } } @@ -185,13 +206,23 @@ } public void setNotifier(Notifier notifier) { - this.notifier = notifier; + synchronized(this) { + this.notifier = notifier; + } } public String getNotifierName() { return NotiferHelper.getName(notifier); } + public void setStatistics(Statistics newStats) { + this.stats = newStats; + } + + public Statistics getStatistics() { + return this.stats; + } + public synchronized ExecutorService getExecutor() { if (executor == null) {