Repository: zookeeper
Updated Branches:
  refs/heads/branch-3.5 3cdaeedf8 -> df1e50bbd


ZOOKEEPER-2511: Implement AutoCloseable in ZooKeeper.java

Author: Abraham Fine <[email protected]>

Reviewers: Edward Ribeiro <[email protected]>, Michael Han 
<[email protected]>

Closes #143 from afine/ZOOKEEPER-2511


Project: http://git-wip-us.apache.org/repos/asf/zookeeper/repo
Commit: http://git-wip-us.apache.org/repos/asf/zookeeper/commit/df1e50bb
Tree: http://git-wip-us.apache.org/repos/asf/zookeeper/tree/df1e50bb
Diff: http://git-wip-us.apache.org/repos/asf/zookeeper/diff/df1e50bb

Branch: refs/heads/branch-3.5
Commit: df1e50bbd7d826ca28705bf9e5cdfc883b0bb4b4
Parents: 3cdaeed
Author: Abraham Fine <[email protected]>
Authored: Mon Jan 9 13:49:46 2017 -0800
Committer: Michael Han <[email protected]>
Committed: Mon Jan 9 13:49:46 2017 -0800

----------------------------------------------------------------------
 src/java/main/org/apache/zookeeper/ZooKeeper.java      | 13 ++++++++++++-
 .../test/org/apache/zookeeper/test/ClientTest.java     | 11 +++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zookeeper/blob/df1e50bb/src/java/main/org/apache/zookeeper/ZooKeeper.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/ZooKeeper.java 
b/src/java/main/org/apache/zookeeper/ZooKeeper.java
index f1b8f7b..23c4e40 100644
--- a/src/java/main/org/apache/zookeeper/ZooKeeper.java
+++ b/src/java/main/org/apache/zookeeper/ZooKeeper.java
@@ -127,7 +127,15 @@ import org.slf4j.LoggerFactory;
  * EventNone and state sKeeperStateDisconnected.
  *
  */
-public class ZooKeeper {
+/*
+ * We suppress the "try" warning here because the close() method's signature
+ * allows it to throw InterruptedException which is strongly advised against
+ * by AutoCloseable (see: 
http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html#close()).
+ * close() will never throw an InterruptedException but the exception remains 
in the
+ * signature for backwards compatibility purposes.
+*/
+@SuppressWarnings("try")
+public class ZooKeeper implements AutoCloseable {
 
     /**
      * @deprecated Use {@link ZKClientConfig#ZOOKEEPER_CLIENT_CNXN_SOCKET}
@@ -1285,6 +1293,9 @@ public class ZooKeeper {
      * invalid. All the ephemeral nodes in the ZooKeeper server associated with
      * the session will be removed. The watches left on those nodes (and on
      * their parents) will be triggered.
+     * <p>
+     * Added in 3.5.3: <a 
href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html";>try-with-resources</a>
+     * may be used instead of calling close directly.
      *
      * @throws InterruptedException
      */

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/df1e50bb/src/java/test/org/apache/zookeeper/test/ClientTest.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/test/ClientTest.java 
b/src/java/test/org/apache/zookeeper/test/ClientTest.java
index 62ae827..d33223d 100644
--- a/src/java/test/org/apache/zookeeper/test/ClientTest.java
+++ b/src/java/test/org/apache/zookeeper/test/ClientTest.java
@@ -844,4 +844,15 @@ public class ClientTest extends ClientBase {
         Assert.assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue());
         zk.testableWaitForShutdown(CONNECTION_TIMEOUT);
     }
+
+    @Test
+    public void testTryWithResources() throws Exception {
+        ZooKeeper zooKeeper;
+        try (ZooKeeper zk = createClient()) {
+            zooKeeper = zk;
+            Assert.assertTrue(zooKeeper.getState().isAlive());
+        }
+
+        Assert.assertFalse(zooKeeper.getState().isAlive());
+    }
 }

Reply via email to