You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by fa...@apache.org on 2017/04/25 03:09:12 UTC
[1/2] curator git commit: [CURATOR-386] Allow listener to be passed
in to PersistentNode
Repository: curator
Updated Branches:
refs/heads/master e8c27f162 -> abaabb5f6
[CURATOR-386] Allow listener to be passed in to PersistentNode
This provides ability to hook into events from PersistentNode when a
node gets created.
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/bec1443c
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/bec1443c
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/bec1443c
Branch: refs/heads/master
Commit: bec1443c9cb04f512df8145fbbaaca7c1c6511b4
Parents: 2672049
Author: Alex Kira <ak...@uber.com>
Authored: Fri Feb 10 13:52:59 2017 -0800
Committer: Alex Kira <ak...@uber.com>
Committed: Mon Mar 20 10:13:20 2017 -0700
----------------------------------------------------------------------
.../framework/recipes/nodes/PersistentNode.java | 42 +++++++++++++++++++-
.../recipes/nodes/PersistentNodeListener.java | 33 +++++++++++++++
2 files changed, 73 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/bec1443c/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
index 5753a93..2a3d5a0 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
@@ -19,6 +19,7 @@
package org.apache.curator.framework.recipes.nodes;
+import com.google.common.base.Function;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.curator.framework.CuratorFramework;
@@ -28,6 +29,7 @@ import org.apache.curator.framework.api.CreateBuilder;
import org.apache.curator.framework.api.CreateModable;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.api.CuratorWatcher;
+import org.apache.curator.framework.listen.ListenerContainer;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.curator.utils.PathUtils;
@@ -69,6 +71,7 @@ public class PersistentNode implements Closeable
private final BackgroundCallback backgroundCallback;
private final boolean useProtection;
private final AtomicReference<CreateModable<ACLBackgroundPathAndBytesable<String>>> createMethod = new AtomicReference<CreateModable<ACLBackgroundPathAndBytesable<String>>>(null);
+ private final ListenerContainer<PersistentNodeListener> listeners = new ListenerContainer<PersistentNodeListener>();
private final CuratorWatcher watcher = new CuratorWatcher()
{
@Override
@@ -117,8 +120,8 @@ public class PersistentNode implements Closeable
{
//Update is ok, mark initialisation as complete if required.
initialisationComplete();
- }
- else if ( event.getResultCode() == KeeperException.Code.NOAUTH.intValue() )
+ }
+ else if ( event.getResultCode() == KeeperException.Code.NOAUTH.intValue() )
{
log.warn("Client does not have authorisation to write node at path {}", event.getPath());
authFailure.set(true);
@@ -235,6 +238,7 @@ public class PersistentNode implements Closeable
else
{
initialisationComplete();
+ notifyListeners();
}
}
else
@@ -303,6 +307,16 @@ public class PersistentNode implements Closeable
}
/**
+ * Returns the listenable
+ *
+ * @return listenable
+ */
+ public ListenerContainer<PersistentNodeListener> getListenable()
+ {
+ return listeners;
+ }
+
+ /**
* Returns the currently set path or null if the node does not exist
*
* @return node path or null
@@ -427,6 +441,30 @@ public class PersistentNode implements Closeable
}
}
+ private void notifyListeners()
+ {
+ final String path = getActualPath();
+ listeners.forEach(
+ new Function<PersistentNodeListener, Void>()
+ {
+ @Override
+ public Void apply(PersistentNodeListener listener)
+ {
+ try
+ {
+ listener.nodeCreated(path);
+ }
+ catch ( Exception e )
+ {
+ ThreadUtils.checkInterrupted(e);
+ log.error("From PersistentNode listener", e);
+ }
+ return null;
+ }
+ }
+ );
+ }
+
private boolean isActive()
{
return (state.get() == State.STARTED);
http://git-wip-us.apache.org/repos/asf/curator/blob/bec1443c/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNodeListener.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNodeListener.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNodeListener.java
new file mode 100644
index 0000000..4131140
--- /dev/null
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNodeListener.java
@@ -0,0 +1,33 @@
+/**
+ * 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.recipes.nodes;
+
+/**
+ * Listener for changes to a PersistentNode
+ */
+public interface PersistentNodeListener {
+ /**
+ * Called on a persistentNode event when node is created
+ *
+ * @param path Path of the znode
+ * @throws Exception errors
+ */
+ void nodeCreated(String path) throws Exception;
+}
[2/2] curator git commit: Merge branch 'CURATOR-386'
Posted by fa...@apache.org.
Merge branch 'CURATOR-386'
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/abaabb5f
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/abaabb5f
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/abaabb5f
Branch: refs/heads/master
Commit: abaabb5f65c2161f77527165a15d2420f6c88219
Parents: e8c27f1 bec1443
Author: Fangmin Lyu <fa...@apache.org>
Authored: Mon Apr 24 20:08:58 2017 -0700
Committer: Fangmin Lyu <fa...@apache.org>
Committed: Mon Apr 24 20:08:58 2017 -0700
----------------------------------------------------------------------
.../framework/recipes/nodes/PersistentNode.java | 42 +++++++++++++++++++-
.../recipes/nodes/PersistentNodeListener.java | 33 +++++++++++++++
2 files changed, 73 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/abaabb5f/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java
----------------------------------------------------------------------