Question

I am new to ZK so not sure if this is true but during my testing i found the following:

   ZooKeeper zooKeeper1 = new ZooKeeper(
                hosts, // ZooKeeper service hosts
                5000,  // Session timeout in milliseconds
        // Anonymous Watcher Object
      watcher);

where watcher is a Watcher instance.

Now if I do this:

zooKeeper1.exists("/test13", watcher);
zooKeeper1.create("/test13", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);

then even defined under watcher object is not triggered

But if do this:

zooKeeper1.exists("/test13", watcher);
zooKeeper1.create("/test13", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

then even defined under watcher object is triggered.

Basically if I use Sequntial as CreateMode it the event is not triggered.

Was it helpful?

Solution

The problem is really very simple,
When you use EPHEMERAL_SEQUENTIAL mode to create the Znode then the node which is created is not having the same name as a serial number is added, something like /zoo/duck0000000027 when I created with name /zoo/duck
but on the other hand when you create the node CreateMode.EPHEMERAL then the node is created with same name as specified.

In EPHEMERAL_SEQUENTIAL scenario if you want to add watcher, then add the watcher after creating then node something like,

String createdPath = zooKeeper1.create("/test13", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
zooKeeper1.exists(createdPath, watcher);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top