add defaultWaitForShutdownTimeoutMs
authorEnrico Olivelli <eolivelli@apache.org>
Sun, 24 Jun 2018 16:05:11 +0000 (18:05 +0200)
committerEnrico Olivelli <eolivelli@apache.org>
Sun, 24 Jun 2018 16:05:11 +0000 (18:05 +0200)
curator-client/src/main/java/org/apache/curator/CuratorZookeeperClient.java
curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java
curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java

index dd3ae82..065743b 100644 (file)
@@ -52,6 +52,7 @@ public class CuratorZookeeperClient implements Closeable
     private final ConnectionState state;
     private final AtomicReference<RetryPolicy> retryPolicy = new AtomicReference<RetryPolicy>();
     private final int connectionTimeoutMs;
+    private final int defaultWaitForShutdownTimeoutMs;
     private final AtomicBoolean started = new AtomicBoolean(false);
     private final AtomicReference<TracerDriver> tracer = new AtomicReference<TracerDriver>(new DefaultTracerDriver());
     private final ConnectionHandlingPolicy connectionHandlingPolicy;
@@ -112,7 +113,28 @@ public class CuratorZookeeperClient implements Closeable
      * @param connectionHandlingPolicy connection handling policy - use one of the pre-defined policies or write your own
      * @since 3.0.0
      */
-    public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy)
+    public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy) {
+        this(zookeeperFactory, ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, 0,
+                watcher, retryPolicy, canBeReadOnly, connectionHandlingPolicy);
+    }
+    /**
+     * @param zookeeperFactory factory for creating {@link ZooKeeper} instances
+     * @param ensembleProvider the ensemble provider
+     * @param sessionTimeoutMs session timeout
+     * @param connectionTimeoutMs connection timeout
+     * @param defaultWaitForShutdownTimeoutMs default timeout fo close operation
+     * @param watcher default watcher or null
+     * @param retryPolicy the retry policy to use
+     * @param canBeReadOnly if true, allow ZooKeeper client to enter
+     *                      read only mode in case of a network partition. See
+     *                      {@link ZooKeeper#ZooKeeper(String, int, Watcher, long, byte[], boolean)}
+     *                      for details
+     * @param connectionHandlingPolicy connection handling policy - use one of the pre-defined policies or write your own
+     * @since 4.0.2
+     */
+    public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider,
+            int sessionTimeoutMs, int connectionTimeoutMs, int defaultWaitForShutdownTimeoutMs, Watcher watcher,
+            RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy)
     {
         this.connectionHandlingPolicy = connectionHandlingPolicy;
         if ( sessionTimeoutMs < connectionTimeoutMs )
@@ -124,6 +146,7 @@ public class CuratorZookeeperClient implements Closeable
         ensembleProvider = Preconditions.checkNotNull(ensembleProvider, "ensembleProvider cannot be null");
 
         this.connectionTimeoutMs = connectionTimeoutMs;
+        this.defaultWaitForShutdownTimeoutMs = defaultWaitForShutdownTimeoutMs;
         state = new ConnectionState(zookeeperFactory, ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, watcher, tracer, canBeReadOnly, connectionHandlingPolicy);
         setRetryPolicy(retryPolicy);
     }
@@ -215,10 +238,15 @@ public class CuratorZookeeperClient implements Closeable
     }
     
     /**
-     * Close the client
+     * Close the client.
+     *
+     * Same as {@link #close(int) } using the default timeout set at construction time.
+     *
+     * @see #close(int)
      */
+    @Override
     public void close() {
-        close(0);
+        close(defaultWaitForShutdownTimeoutMs);
     }
             
     /**
index a617198..dad164e 100644 (file)
@@ -49,6 +49,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
+import org.apache.curator.CuratorZookeeperClient;
 
 import static org.apache.curator.utils.Compatibility.isZK34;
 
@@ -147,7 +148,7 @@ public class CuratorFrameworkFactory
         private ConnectionHandlingPolicy connectionHandlingPolicy = new StandardConnectionHandlingPolicy();
         private SchemaSet schemaSet = SchemaSet.getDefaultSchemaSet();
         private boolean zk34CompatibilityMode = isZK34();
-
+        private int defaultWaitForShutdownTimeoutMs = 0;
         /**
          * Apply the current values and build a new CuratorFramework
          *
@@ -402,6 +403,20 @@ public class CuratorFrameworkFactory
         }
 
         /**
+         * Set a default timeout for {@link CuratorZookeeperClient#close() }.
+         * The default is 0, which means that this feature is disabled.
+         *
+         * @since 4.0.2
+         * @param defaultWaitForShutdownTimeoutMs default timeout
+         * @return this
+         */
+        public Builder defaultWaitForShutdownTimeoutMs(int defaultWaitForShutdownTimeoutMs)
+        {
+            this.defaultWaitForShutdownTimeoutMs = defaultWaitForShutdownTimeoutMs;
+            return this;
+        }
+
+        /**
          * <p>
          *     Change the connection handling policy. The default policy is {@link StandardConnectionHandlingPolicy}.
          * </p>
@@ -494,6 +509,11 @@ public class CuratorFrameworkFactory
             return connectionTimeoutMs;
         }
 
+        public int getDefaultWaitForShutdownTimeoutMs()
+        {
+            return defaultWaitForShutdownTimeoutMs;
+        }
+
         public int getMaxCloseWaitMs()
         {
             return maxCloseWaitMs;
index 2bd5c7c..80a7b56 100644 (file)
@@ -122,6 +122,7 @@ public class CuratorFrameworkImpl implements CuratorFramework
                 builder.getEnsembleProvider(),
                 builder.getSessionTimeoutMs(),
                 builder.getConnectionTimeoutMs(),
+                builder.getDefaultWaitForShutdownTimeoutMs(),
                 new Watcher()
                 {
                     @Override