You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by io...@apache.org on 2013/08/16 11:46:11 UTC

git commit: [CURATOR-27] Add a flag to CloseableExecutorService to indicate if executor needs to be shut down on close

Updated Branches:
  refs/heads/CURATOR-27 [created] 80c677192


[CURATOR-27] Add a flag to CloseableExecutorService to indicate if executor needs to be shut down on close


Project: http://git-wip-us.apache.org/repos/asf/incubator-curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-curator/commit/80c67719
Tree: http://git-wip-us.apache.org/repos/asf/incubator-curator/tree/80c67719
Diff: http://git-wip-us.apache.org/repos/asf/incubator-curator/diff/80c67719

Branch: refs/heads/CURATOR-27
Commit: 80c677192f0be69536c53b3796ecd009c1aff94c
Parents: 175094f
Author: Ioannis Canellos <io...@apache.org>
Authored: Fri Aug 16 12:45:41 2013 +0300
Committer: Ioannis Canellos <io...@apache.org>
Committed: Fri Aug 16 12:45:41 2013 +0300

----------------------------------------------------------------------
 .../curator/utils/CloseableExecutorService.java | 14 ++++++++++++
 .../CloseableScheduledExecutorService.java      | 12 +++++++++-
 .../recipes/cache/PathChildrenCache.java        | 24 +++++++++++++++-----
 3 files changed, 43 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/80c67719/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java b/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
index bb4855d..8590959 100644
--- a/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
+++ b/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
@@ -44,6 +44,7 @@ public class CloseableExecutorService implements Closeable
     private final Logger log = LoggerFactory.getLogger(CloseableExecutorService.class);
     private final Set<Future<?>> futures = Sets.newSetFromMap(Maps.<Future<?>, Boolean>newConcurrentMap());
     private final ExecutorService executorService;
+    private final boolean shutdownOnClose;
     protected final AtomicBoolean isOpen = new AtomicBoolean(true);
 
     protected class InternalFutureTask<T> extends FutureTask<T>
@@ -68,7 +69,17 @@ public class CloseableExecutorService implements Closeable
      */
     public CloseableExecutorService(ExecutorService executorService)
     {
+       this(executorService, false);
+    }
+
+    /**
+     * @param executorService the service to decorate
+     * @param shutdownOnClose
+     */
+    public CloseableExecutorService(ExecutorService executorService, boolean shutdownOnClose)
+    {
         this.executorService = executorService;
+        this.shutdownOnClose = shutdownOnClose;
     }
 
     /**
@@ -104,6 +115,9 @@ public class CloseableExecutorService implements Closeable
                 log.warn("Could not cancel " + future);
             }
         }
+        if (shutdownOnClose) {
+            this.executorService.shutdown();
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/80c67719/curator-client/src/main/java/org/apache/curator/utils/CloseableScheduledExecutorService.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/utils/CloseableScheduledExecutorService.java b/curator-client/src/main/java/org/apache/curator/utils/CloseableScheduledExecutorService.java
index 6f3797d..417272c 100644
--- a/curator-client/src/main/java/org/apache/curator/utils/CloseableScheduledExecutorService.java
+++ b/curator-client/src/main/java/org/apache/curator/utils/CloseableScheduledExecutorService.java
@@ -38,7 +38,17 @@ public class CloseableScheduledExecutorService extends CloseableExecutorService
      */
     public CloseableScheduledExecutorService(ScheduledExecutorService scheduledExecutorService)
     {
-        super(scheduledExecutorService);
+        super(scheduledExecutorService, false);
+        this.scheduledExecutorService = scheduledExecutorService;
+    }
+
+    /**
+     * @param scheduledExecutorService
+     * @param shutdownOnClose
+     */
+    public CloseableScheduledExecutorService(ScheduledExecutorService scheduledExecutorService, boolean shutdownOnClose)
+    {
+        super(scheduledExecutorService, shutdownOnClose);
         this.scheduledExecutorService = scheduledExecutorService;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/80c67719/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
index ec2d328..424abe4 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
@@ -143,7 +143,7 @@ public class PathChildrenCache implements Closeable
     @SuppressWarnings("deprecation")
     public PathChildrenCache(CuratorFramework client, String path, PathChildrenCacheMode mode)
     {
-        this(client, path, mode != PathChildrenCacheMode.CACHE_PATHS_ONLY, false, Executors.newSingleThreadExecutor(defaultThreadFactory));
+        this(client, path, mode != PathChildrenCacheMode.CACHE_PATHS_ONLY, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory)));
     }
 
     /**
@@ -156,7 +156,7 @@ public class PathChildrenCache implements Closeable
     @SuppressWarnings("deprecation")
     public PathChildrenCache(CuratorFramework client, String path, PathChildrenCacheMode mode, ThreadFactory threadFactory)
     {
-        this(client, path, mode != PathChildrenCacheMode.CACHE_PATHS_ONLY, false, Executors.newSingleThreadExecutor(threadFactory));
+        this(client, path, mode != PathChildrenCacheMode.CACHE_PATHS_ONLY, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory), true));
     }
 
     /**
@@ -166,7 +166,7 @@ public class PathChildrenCache implements Closeable
      */
     public PathChildrenCache(CuratorFramework client, String path, boolean cacheData)
     {
-        this(client, path, cacheData, false, Executors.newSingleThreadExecutor(defaultThreadFactory));
+        this(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory), true));
     }
 
     /**
@@ -177,7 +177,7 @@ public class PathChildrenCache implements Closeable
      */
     public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, ThreadFactory threadFactory)
     {
-        this(client, path, cacheData, false, Executors.newSingleThreadExecutor(threadFactory));
+        this(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory), true));
     }
 
     /**
@@ -189,7 +189,7 @@ public class PathChildrenCache implements Closeable
      */
     public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, ThreadFactory threadFactory)
     {
-        this(client, path, cacheData, dataIsCompressed, Executors.newSingleThreadExecutor(threadFactory));
+        this(client, path, cacheData, dataIsCompressed, new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory), true));
     }
 
     /**
@@ -201,11 +201,23 @@ public class PathChildrenCache implements Closeable
      */
     public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, final ExecutorService executorService)
     {
+        this(client, path, cacheData, dataIsCompressed, new CloseableExecutorService(executorService));
+    }
+
+    /**
+     * @param client           the client
+     * @param path             path to watch
+     * @param cacheData        if true, node contents are cached in addition to the stat
+     * @param dataIsCompressed if true, data in the path is compressed
+     * @param executorService  Closeable ExecutorService to use for the PathChildrenCache's background thread
+     */
+    public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, final CloseableExecutorService executorService)
+    {
         this.client = client;
         this.path = path;
         this.cacheData = cacheData;
         this.dataIsCompressed = dataIsCompressed;
-        this.executorService = new CloseableExecutorService(executorService);
+        this.executorService = executorService;
         ensurePath = client.newNamespaceAwareEnsurePath(path);
     }