CURATOR-408 Handle graceful close of ZookKeeper client waiting for all resources...
authorEnrico Olivelli <eolivelli@apache.org>
Mon, 11 Jun 2018 19:28:30 +0000 (21:28 +0200)
committerEnrico Olivelli <eolivelli@apache.org>
Mon, 11 Jun 2018 19:28:30 +0000 (21:28 +0200)
curator-client/src/main/java/org/apache/curator/ConnectionState.java
curator-client/src/main/java/org/apache/curator/CuratorZookeeperClient.java
curator-client/src/main/java/org/apache/curator/HandleHolder.java
curator-framework/src/test/java/org/apache/curator/framework/state/TestConnectionStateManager.java
pom.xml

index b5af9f2..24b9948 100644 (file)
@@ -112,14 +112,17 @@ class ConnectionState implements Watcher, Closeable
     }
 
     @Override
-    public void close() throws IOException
-    {
+    public void close() throws IOException {
+        close(0);
+    }
+    
+    public void close(int waitForShutdownTimeoutMs) throws IOException {
         log.debug("Closing");
 
         CloseableUtils.closeQuietly(ensembleProvider);
         try
         {
-            zooKeeper.closeAndClear();
+            zooKeeper.closeAndClear(waitForShutdownTimeoutMs);
         }
         catch ( Exception e )
         {
index b7bb33a..dd3ae82 100644 (file)
@@ -213,18 +213,29 @@ public class CuratorZookeeperClient implements Closeable
 
         state.start();
     }
-
+    
     /**
      * Close the client
      */
-    public void close()
+    public void close() {
+        close(0);
+    }
+            
+    /**
+     * Close this client object as the {@link #close() } method.
+     * This method will wait for internal resources to be released.
+     * 
+     * @param waitForShutdownTimeoutMs timeout (in milliseconds) to wait for resources to be released.
+     *                  Use zero or a negative value to skip the wait.
+     */
+    public void close(int waitForShutdownTimeoutMs)
     {
-        log.debug("Closing");
+        log.debug("Closing, waitForShutdownTimeoutMs {}", waitForShutdownTimeoutMs);
 
         started.set(false);
         try
         {
-            state.close();
+            state.close(waitForShutdownTimeoutMs);
         }
         catch ( IOException e )
         {
index 98b39ce..33250d8 100644 (file)
@@ -73,15 +73,15 @@ class HandleHolder
         return ((helperConnectionString != null) && !ensembleProvider.getConnectionString().equals(helperConnectionString)) ? helperConnectionString : null;
     }
 
-    void closeAndClear() throws Exception
+    void closeAndClear(int timeout) throws Exception
     {
-        internalClose();
+        internalClose(timeout);
         helper = null;
     }
 
     void closeAndReset() throws Exception
     {
-        internalClose();
+        internalClose(0);
 
         // first helper is synchronized when getZooKeeper is called. Subsequent calls
         // are not synchronized.
@@ -140,7 +140,7 @@ class HandleHolder
         };
     }
 
-    private void internalClose() throws Exception
+    private void internalClose(int waitForShutdownTimeoutMs) throws Exception
     {
         try
         {
@@ -155,7 +155,7 @@ class HandleHolder
                     }
                 };
                 zooKeeper.register(dummyWatcher);   // clear the default watcher so that no new events get processed by mistake
-                zooKeeper.close();
+                zooKeeper.close(waitForShutdownTimeoutMs);
             }
         }
         catch ( InterruptedException dummy )
index e313c56..97ea941 100644 (file)
@@ -1,3 +1,21 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.curator.framework.state;
 
 import com.google.common.collect.Queues;
diff --git a/pom.xml b/pom.xml
index c75912b..dd4abdd 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -59,7 +59,7 @@
         <jdk-version>1.7</jdk-version>
 
         <!-- versions -->
-        <zookeeper-version>3.5.3-beta</zookeeper-version>
+        <zookeeper-version>3.5.4-beta</zookeeper-version>
         <maven-project-info-reports-plugin-version>2.9</maven-project-info-reports-plugin-version>
         <maven-bundle-plugin-version>3.2.0</maven-bundle-plugin-version>
         <maven-javadoc-plugin-version>2.10.4</maven-javadoc-plugin-version>