You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2014/03/22 14:35:25 UTC

[10/10] git commit: For added safety, install a connection state listener and attempt to recreate the node on RECONNECT

For added safety, install a connection state listener and attempt to recreate the node on RECONNECT


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

Branch: refs/heads/CURATOR-97
Commit: bfdef2ddc1c3f2121256045690b39baafa269151
Parents: b17e6ff
Author: randgalt <ra...@apache.org>
Authored: Sat Mar 22 08:34:41 2014 -0500
Committer: randgalt <ra...@apache.org>
Committed: Sat Mar 22 08:34:41 2014 -0500

----------------------------------------------------------------------
 .../recipes/nodes/PersistentEphemeralNode.java      | 16 ++++++++++++++++
 .../framework/client/TestBackgroundStates.java      |  2 --
 .../recipes/nodes/TestPersistentEphemeralNode.java  |  2 --
 3 files changed, 16 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/bfdef2dd/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
index 71095ba..6b6ae52 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
@@ -25,6 +25,8 @@ import org.apache.curator.framework.api.ACLBackgroundPathAndBytesable;
 import org.apache.curator.framework.api.BackgroundCallback;
 import org.apache.curator.framework.api.CreateModable;
 import org.apache.curator.framework.api.CuratorEvent;
+import org.apache.curator.framework.state.ConnectionState;
+import org.apache.curator.framework.state.ConnectionStateListener;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.WatchedEvent;
@@ -79,6 +81,17 @@ public class PersistentEphemeralNode implements Closeable
             }
         }
     };
+    private final ConnectionStateListener connectionStateListener = new ConnectionStateListener()
+    {
+        @Override
+        public void stateChanged(CuratorFramework client, ConnectionState newState)
+        {
+            if ( newState == ConnectionState.RECONNECTED )
+            {
+                createNode();
+            }
+        }
+    };
 
     private enum State
     {
@@ -226,6 +239,7 @@ public class PersistentEphemeralNode implements Closeable
     {
         Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "Already started");
 
+        client.getConnectionStateListenable().addListener(connectionStateListener);
         createNode();
     }
 
@@ -254,6 +268,8 @@ public class PersistentEphemeralNode implements Closeable
             return;
         }
 
+        client.getConnectionStateListenable().removeListener(connectionStateListener);
+
         try
         {
             deleteNode();

http://git-wip-us.apache.org/repos/asf/curator/blob/bfdef2dd/curator-recipes/src/test/java/org/apache/curator/framework/client/TestBackgroundStates.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/client/TestBackgroundStates.java b/curator-recipes/src/test/java/org/apache/curator/framework/client/TestBackgroundStates.java
index b1c382f..cb186fc 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/client/TestBackgroundStates.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/client/TestBackgroundStates.java
@@ -97,8 +97,6 @@ public class TestBackgroundStates extends BaseClassForTests
     @Test
     public void testConnectionStateListener() throws Exception
     {
-        System.setProperty(DebugUtils.PROPERTY_LOG_EVENTS, "true");
-
         server.close();
 
         Timing timing = new Timing();

http://git-wip-us.apache.org/repos/asf/curator/blob/bfdef2dd/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
index e312081..4ca487a 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
@@ -74,8 +74,6 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
     @Test
     public void testListenersReconnectedIsFast() throws Exception
     {
-        System.setProperty(DebugUtils.PROPERTY_LOG_EVENTS, "true");
-
         server.close();
 
         Timing timing = new Timing();