user1709181
user1709181

Reputation: 115

Apache Curator loses all watches after reconnection

I am using Apache Curator in my project. After CuratorFramework loses connection to the ZooKeeper server, it automatically reconnects, but it seems that it loses all the watches that I have set up, and I am not getting any notifications anymore. Watches work as expected if the connection is not dropped and restarted.

Is this is a Curator bug or am I doing something wrong? The code for setting the watch is as follows:

CuratorFramework framework = CuratorFrameworkFactory.newClient(connectString, SESSION_TIMEOUT_MS, 0, new ExponentialBackoffRetry(CONNECT_TIMEOUT_MS, CONNECT_RETRY_COUNT));
framework.start();
framework.getChildren().usingWatcher(watcher).forPath(path);

thanks,

Upvotes: 4

Views: 2085

Answers (1)

AdamStallard
AdamStallard

Reputation: 1830

Your Watcher should notify you if there is a change in the connection. If so, the EventType on the WatchedEvent will be None, Use that as an opportunity to restart your watches:

@Override
public void process(WatchedEvent event) {
    if (event.getType == Event.EventType.None && event.getState() == Event.KeeperState.SyncConnected){
        framework.getChildren().usingWatcher(this).forPath(path);
...

Upvotes: 5

Related Questions