Repository: zookeeper Updated Branches: refs/heads/master 7c51b01e8 -> c2434883d
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 (cherry picked from commit df1e50bbd7d826ca28705bf9e5cdfc883b0bb4b4) Signed-off-by: Michael Han <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/zookeeper/repo Commit: http://git-wip-us.apache.org/repos/asf/zookeeper/commit/c2434883 Tree: http://git-wip-us.apache.org/repos/asf/zookeeper/tree/c2434883 Diff: http://git-wip-us.apache.org/repos/asf/zookeeper/diff/c2434883 Branch: refs/heads/master Commit: c2434883dcdd85450ac73b7e69f3deed83fa5bf4 Parents: 7c51b01 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:58 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/c2434883/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 cf274a4..d00b0be 100644 --- a/src/java/main/org/apache/zookeeper/ZooKeeper.java +++ b/src/java/main/org/apache/zookeeper/ZooKeeper.java @@ -129,7 +129,15 @@ import java.util.Set; * 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} @@ -1287,6 +1295,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/c2434883/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()); + } }
