Observing Node Events
ZooKeeper allows you to subscribe to a certain events happening to a node:
Creation of node
Deletion of node
Changes of node data
Adding and deleting of node's children
To do this, one can specify a NodeWatcher
along with any GetRequest
: ExistsRequest
, GetChildrenRequest
and GetDataRequest
.
A watcher will be attached to a given node only if request results in success. Watcher will be triggered when a change occurs to this node only once.
While there is no guarantee that notification will be delivered to the client regardless of the state of connection, delivered events can never be out of order.
Deduplication of watchers
By default, an event will be delivered to a watcher only once, no matter how many times the same watcher instance was passed to client. For example, if we attach the same watcher two times...
...it will be triggered exactly once.
That is because watcher instances are being cached inside a client. Sometimes it might be necessary not to cache a one-time watcher or receive a separate notification for every subscription.
To override caching behaviour, use IgnoreWatchersCache = true
:
The total count of deduplicated watchers is limited by a WatchersCacheCapacity
in ZooKeeperClientSettings
.
Reattaching a Watcher
Any watcher is notified only of a single event. So, once it gets called, it may be necessary to attach a new watcher.
Last updated