Single thread executor (#68)
Improved executor
diff --git a/core/src/main/java/org/apache/karaf/cellar/core/event/EventHandlerRegistryDispatcher.java b/core/src/main/java/org/apache/karaf/cellar/core/event/EventHandlerRegistryDispatcher.java
index 6e4a087..7dd7b54 100644
--- a/core/src/main/java/org/apache/karaf/cellar/core/event/EventHandlerRegistryDispatcher.java
+++ b/core/src/main/java/org/apache/karaf/cellar/core/event/EventHandlerRegistryDispatcher.java
@@ -16,17 +16,27 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* Event handler service registry dispatcher.
*/
public class EventHandlerRegistryDispatcher<E extends Event> implements EventDispatcher<E> {
+ private static final transient Logger LOGGER = LoggerFactory.getLogger(EventHandlerRegistryDispatcher.class);
private ExecutorService threadPool;
private EventHandlerRegistry handlerRegistry;
public void init() {
if (threadPool == null) {
- threadPool = Executors.newCachedThreadPool();
+ if (Boolean.getBoolean(this.getClass().getName() + ".threadPool.singleThreadExecutor")) {
+ LOGGER.info("Will use an Executor that uses a single worker thread");
+ threadPool = Executors.newSingleThreadExecutor();
+ } else {
+ LOGGER.info("Will use an Executor with a pool of threads");
+ threadPool = Executors.newCachedThreadPool();
+ }
}
}
@@ -56,4 +66,10 @@
this.threadPool = threadPool;
}
+ public void destroy() {
+ if (threadPool != null) {
+ threadPool.shutdown();
+ }
+ }
+
}
diff --git a/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java b/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java
index 3de5799..87593c1 100644
--- a/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java
+++ b/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java
@@ -98,6 +98,8 @@
private HashMap updatedConfig;
+ private EventHandlerRegistryDispatcher dispatcher;
+
@Override
public void doStart() throws Exception {
@@ -144,7 +146,7 @@
extender.init();
LOGGER.debug("CELLAR HAZELCAST: init dispatcher");
- EventHandlerRegistryDispatcher dispatcher = new EventHandlerRegistryDispatcher();
+ dispatcher = new EventHandlerRegistryDispatcher();
dispatcher.setHandlerRegistry(eventHandlerRegistry);
dispatcher.init();
@@ -402,6 +404,10 @@
combinedClassLoader.destroy();
combinedClassLoader = null;
}
+ if (dispatcher != null) {
+ dispatcher.destroy();
+ dispatcher = null;
+ }
}
@Override