--- CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java 2010/06/21 17:16:46 871 +++ CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java 2011/04/23 10:43:40 1376 @@ -1,6 +1,12 @@ 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; @@ -51,11 +57,17 @@ private String name; + private ExecutorService executor = null; private Notifier notifier = new NullNotifier(); - 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; @@ -63,6 +75,12 @@ internalReset(); } + public synchronized void shutdown() { + if (executor != null) { + executor.shutdown(); + } + } + public Object invoke(CircuitInvocation invocation) throws Exception { @@ -96,7 +114,7 @@ open.trip(); currentState = open; - notifier.sendNotification(name, event); + notifier.sendNotification(this, event); } } } @@ -105,7 +123,7 @@ synchronized(this) { if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? currentState = halfOpen; - notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); + notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset); } } @@ -115,7 +133,7 @@ synchronized(this) { if (currentState != closed) { // TODO: Is this conditional necessary ?? internalReset(); - notifier.sendNotification(name, Notifier.Event.BreakerReset); + notifier.sendNotification(this, Notifier.Event.BreakerReset); } } } @@ -177,8 +195,18 @@ this.notifier = notifier; } - public String getNotifierName() { - return notifier.getClass().getName(); + public String getNotifierName() { + return NotiferHelper.getName(notifier); + } + + public synchronized ExecutorService getExecutor() { + + if (executor == null) { + executor = Executors.newFixedThreadPool(1); + } + + return executor; + } }