If you need to set up a recursive watchers (watch on all nodes), the standard ZooKeeper’s Watcher class will not help much – it is installed on only 1 node (or one-level-forward when you calling getChildren()), and is also a one-time event. This means that after each watch trigger, you need to install a new one.
But don’t have to reinvent the wheel. Use the TreeCache class from the Curator Framework. Despite the “cache” in the class name, the cache can be disabled, and leave only the recursive Listener for all nodes:
TreeCache treeCache = new TreeCache(curator, "/");
treeCache.getListenable().addListener((curatorFramework, event) -> {
// Do what you want
});
treeCache.start();
If you still have any questions, feel free to ask me in the comments under this article, or write me on promark33@gmail.com.