--- miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java 2015/06/09 07:42:41 2568 +++ miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java 2015/06/09 08:55:10 2569 @@ -9,6 +9,8 @@ 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 * @@ -59,6 +61,7 @@ private ExecutorService executor = null; private Notifier notifier = new NullNotifier(); + private Statistics stats = new NullStatistics(); @Deprecated public CircuitBreaker(String name, int threshold, int timeoutMS) { @@ -84,6 +87,8 @@ public Object invoke(CircuitInvocation invocation) throws Exception { + stats.addStatistics(Event.Invocation); + Object result = null; try { @@ -92,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; } @@ -100,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); } } } @@ -123,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); } } @@ -133,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); } } } @@ -199,6 +213,14 @@ 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) {