ZooKeeper Client 使用ZKWatchManager和WatchRegistration两个类管理Client端Watch的注册和触发
- ZKWatchManager:储存已经注册的Watch、查找事件触发的Watch
- WatchRegistration:注册Watch到ZKWatchManager中
ZKWatchManager与WatchRegistration关系图:
ZKWatchManager如何存储Client端注册的Watch?
private final Map<String, Set<Watcher>> dataWatches =
new HashMap<String, Set<Watcher>>();
private final Map<String, Set<Watcher>> existWatches =
new HashMap<String, Set<Watcher>>();
private final Map<String, Set<Watcher>> childWatches =
new HashMap<String, Set<Watcher>>();
private volatile Watcher defaultWatcher;
它有3个
Map
<String
,
Set
<Watcher
>>集合,dataWatches对应的是与Node数据变动的Watch,existWatches对应的是Node创建和删除的Watch,childWatches对应的是Node的子Node的变化的Watch。defaultWatcher用于存储创建ZooKeeper对象时传入的Watch。
ZKWatchManager如何查找事件触发的watches?
public Set<Watcher> materialize(Watcher.Event.KeeperState state,
Watcher.Event.Eve