ZooKeeper provides a distributed data publish / subscribe functionality, a typical publish / subscribe model system defines the relationship of one to many subscription, allowing more subscribers with when listening a topic object, when the object itself the subject status when the change will notify all subscribers so that they can make the appropriate treatment.
ZooKeeper, the introduction of a mechanism to implement this Watcher distributed notification. ZooKeeper Watcher allows clients to register a listener to the server, the server when events triggered the Watcher, it will send an event notification to specify the client to achieve a distributed notification.
Many types of trigger events, such as: nodes create, delete nodes, change node, sub-node and changes.
Overall Watcher can be summarized as the following three processes: client registers Watcher to the server, the server Watcher trigger event occurs, the client callback Watcher get a trigger event
1, Watch mechanism Features
Disposable trigger
Event trigger monitor, a watcher event will be sent to the client set up to listen, this effect is a one-time, follow-up recurrence of similar incidents will not trigger again.
Event package
ZooKeeper use WatchedEvent objects to encapsulate and deliver the service side event.
WatchedEvent contains three basic attributes of each event:
Notifications (keeperState) , event type (EventType) and node path (path)
event sent asynchronously
watcher notification event is sent from the server to the client is asynchronous.
To register again trigger
watch mechanism Zookeeper, you must first go to the client server registered listener, this event will trigger the sending monitoring, notification to the client.
2, the notification status and event types
Event types represent the same meaning in different notification states vary, the following table lists the common status and event notification type.
Where the connection status events (type = None, path = null ) does not require client registration , client as long as necessary to deal directly on the line.
. 3, S Hell client settings watcher
Set the node monitor data changes:
Change the end node data by another client:
At this point set the node listens notified: