[CURATOR-386] Allow listener to be passed in to PersistentNode to notify for node creation events
I think it would be useful to allow a listener to be passed in to the PersistentNode that would notify when the new node is created. This is useful as some cases such as disconnect / reconnect or when an ephemeral node is deleted and recreated by PersistentNode. In this case, I would like to be able to listen to these even so I can do something like issue a watch on the node.
For example:
```
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;
}
```
I have a code change implementing this and can issue a pull request for this.
Originally reported by akira989, imported from: Allow listener to be passed in to PersistentNode to notify for node creation events
- status: Open
- priority: Minor
- resolution: Unresolved
- imported: 2025-01-21
GitHub user akira opened a pull request:
https://github.com/apache/curator/pull/198
CURATOR-386 Allow listener to be passed in to PersistentNode
This provides ability to hook into events from PersistentNode when a
node gets created. PersistentNode would then notify any registered listeners that the
node was created, along with the path created (useful when protection is used).
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/akira/curator CURATOR-386
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/curator/pull/198.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #198
commit 8bb0cc86343eca7e2b38bccda39ab28d6c19113c
Author: Alex Kira
Date: 2017-02-10T21:52:59Z
CURATOR-386 Allow listener to be passed in to PersistentNode
This provides ability to hook into events from PersistentNode when a
node gets created.
Github user akira commented on the issue:
https://github.com/apache/curator/pull/198
Ran tests locally:
```
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Apache Curator ..................................... SUCCESS [ 3.114 s]
[INFO] Curator Testing .................................... SUCCESS [ 8.040 s]
[INFO] Curator Client ..................................... SUCCESS [ 37.217 s]
[INFO] Curator Framework .................................. SUCCESS [09:36 min]
[INFO] Curator Recipes .................................... SUCCESS [29:43 min]
[INFO] Curator Service Discovery .......................... SUCCESS [01:26 min]
[INFO] Curator Examples ................................... SUCCESS [ 2.376 s]
[INFO] Curator Service Discovery Server ................... SUCCESS [ 10.766 s]
[INFO] Curator RPC Proxy .................................. SUCCESS [ 13.581 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
```
Github user lvfangmin commented on a diff in the pull request:
https://github.com/apache/curator/pull/198#discussion_r105545812
— Diff: curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java —
@@ -117,6 +120,7 @@ public void processResult(CuratorFramework client, CuratorEvent event)
{
//Update is ok, mark initialisation as complete if required.
initialisationComplete();
+notifyListeners();
— End diff –
the listener is supposed to listen on the node create event, this is the place where we updated the node value, not create.
Github user akira commented on a diff in the pull request:
https://github.com/apache/curator/pull/198#discussion_r106834514
— Diff: curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java —
@@ -117,6 +120,7 @@ public void processResult(CuratorFramework client, CuratorEvent event)
{
//Update is ok, mark initialisation as complete if required.
initialisationComplete();
+notifyListeners();
— End diff –
@lvfangmin I looked at this - and it looks to me like this is the callback where the result of node creation comes back (telling us whether it was successful, node already exists, etc). This is in the `processBackgroundCallback` method, and where we know that the result code comes back as `false` for `KeeperException.Code.NODEEXISTS` and `true` for `KeeperException.Code.OK`. This means that we just created the node, and seemingly the place to notify listeners.
The callback for updating the node value seems to be `setDataCallback`, which I did not put any code for notifying listeners.
To me this looked like the place to put this, but definitely let me know if you thought of a better place and I can investigate that.
Github user akira commented on the issue:
https://github.com/apache/curator/pull/198
@lvfangmin I updated the PR based on your feedback - moved the notifyListeners to the `processBackgroundCallback` callback (I squashed the commit with the previous one).
Github user lvfangmin commented on the issue:
https://github.com/apache/curator/pull/198
Thanks @akira, the new patch looks good to me.
Github user akira commented on the issue:
https://github.com/apache/curator/pull/198
@lvfangmin thanks, when do you think it will be merged?
Github user lvfangmin commented on the issue:
https://github.com/apache/curator/pull/198
@akira sorry for the lately reply, I was waiting for another LGTM, the tests look good on my Mac, I'm going to merge the code now.
Github user asfgit closed the pull request at: