CURATOR-495 - have methods return a CompletableFuture so that callers can check compl...
authorrandgalt <randgalt@apache.org>
Fri, 14 Dec 2018 19:20:12 +0000 (14:20 -0500)
committerrandgalt <randgalt@apache.org>
Fri, 14 Dec 2018 19:20:12 +0000 (14:20 -0500)
curator-framework/src/main/java/org/apache/curator/framework/CuratorFramework.java
curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java

index 02c458a..3737faa 100644 (file)
@@ -34,6 +34,7 @@ import org.apache.curator.utils.EnsurePath;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier;
 import java.io.Closeable;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -335,11 +336,12 @@ public interface CuratorFramework extends Closeable
      * done from the {@link #runSafe(Runnable)} thread.
      *
      * @param monitorHolder object to sync on and notify
+     * @return a CompletableFuture that can be used to monitor when the call is complete
      * @since 4.1.0
      */
-    default void postSafeNotify(Object monitorHolder)
+    default CompletableFuture<Void> postSafeNotify(Object monitorHolder)
     {
-        runSafe(() -> {
+        return runSafe(() -> {
             synchronized(monitorHolder) {
                 monitorHolder.notifyAll();
             }
@@ -351,7 +353,8 @@ public interface CuratorFramework extends Closeable
      * and other blocking calls that might normally block ZooKeeper's event thread.
 
      * @param runnable proc to call from a safe internal thread
+     * @return a CompletableFuture that can be used to monitor when the call is complete
      * @since 4.1.0
      */
-    void runSafe(Runnable runnable);
+    CompletableFuture<Void> runSafe(Runnable runnable);
 }
index 34002a0..736b737 100644 (file)
@@ -58,15 +58,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.Callable;
-import java.util.concurrent.DelayQueue;
-import java.util.concurrent.Executor;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.ThreadFactory;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
@@ -194,9 +186,9 @@ public class CuratorFrameworkImpl implements CuratorFramework
     }
 
     @Override
-    public void runSafe(Runnable runnable)
+    public CompletableFuture<Void> runSafe(Runnable runnable)
     {
-        runSafeService.execute(runnable);
+        return CompletableFuture.runAsync(runnable, runSafeService);
     }
 
     @Override