how to re-register zookeeper watches

Viewed 645

I want to use zookeeper watches to do some tasks(distributed syncs across multiple nodes) in case of any update on znode. I am aware that zookeeper watchers are one-time triggers and I need to re-register the watchers in order to get future updates. But I am not able to do that in my example.

b = zkeeper.getData(path, new Watcher() {
                public void process(WatchedEvent we) {
                    if (we.getState() == Watcher.Event.KeeperState.SyncConnected) {
                        System.out.println("inside get watcher");
                        clear_cache();
                        }
                }
            }, null);

This is working for one -time update on znode, but once I update again, zookeeper doesn't trigger anything.

So, once I receive a trigger, how should I re-register the watcher from triggered event?

Even if I re-register it inside process function of watcher, it is not working properly.

Note: I am using apache-zookeeper java client.

Use Case: Need to sync data across multiple node using trigger. For example: if update request reaches one node, it should immediately inform other nodes as well for update.

0 Answers
Related