您好,登錄后才能下訂單哦!
接著上一篇文章,繼續分析和Watcher相關的類的源碼。
public Set<Watcher> materialize(Watcher.Event.KeeperState state,
Watcher.Event.EventType type, String path);
該接口,只有一個方法,需要實現。該方法表示事件發生時,返回需要被通知的Watcher集合,可能為空集合。
1、ZKWatchManager是ZooKeeper的內部類,實現了ClientWatchManager。
2、ZKWatchManager定義了三個Map鍵值對,鍵為節點路徑,值為Watcher。分別對應數據變化的Watcher、節點是否存在的Watcher、子節點變化的Watcher。
static class ZKWatchManager implements ClientWatchManager {
//數據變化的watchers
private final Map<String, Set<Watcher>> dataWatches =
new HashMap<String, Set<Watcher>>();
//節點存在與否的watchers
private final Map<String, Set<Watcher>> existWatches =
new HashMap<String, Set<Watcher>>();
//子節點變化的watchers
private final Map<String, Set<Watcher>> childWatches =
new HashMap<String, Set<Watcher>>();
3、materialize方法
該方法在事件發生后,返回需要被通知的Watcher集合。在該方法中,首先會根據EventType類型確定相應的事件類型,然后根據事件類型的不同做出相應的操作,如針對None類型,即無任何事件,則首先會從三個鍵值對中刪除clientPath對應的Watcher,然后將剩余的Watcher集合添加至結果集合;針對NodeDataChanged和NodeCreated事件而言,其會從dataWatches和existWatches中刪除clientPath對應的Watcher,然后將剩余的Watcher集合添加至結果集合。
@Override
public Set<Watcher> materialize(Watcher.Event.KeeperState state,
Watcher.Event.EventType type,
String clientPath)
{
//返回結果集合
Set<Watcher> result = new HashSet<Watcher>();
switch (type) {//事件類型
case None://無類型
//添加默認watcher
result.add(defaultWatcher);
//根據disableAutoWatchReset和Zookeeper的狀態是否為同步連接判斷是否需要清空
boolean clear = disableAutoWatchReset && state != Watcher.Event.KeeperState.SyncConnected;
//針對3個不同的watcherMap進行操作
synchronized(dataWatches) {
for(Set<Watcher> ws: dataWatches.values()) {
// 添加至結果集合
result.addAll(ws);
}
if (clear) { // 是否需要清空
dataWatches.clear();
}
}
synchronized(existWatches) {
for(Set<Watcher> ws: existWatches.values()) {
result.addAll(ws);
}
if (clear) {
existWatches.clear();
}
}
synchronized(childWatches) {
for(Set<Watcher> ws: childWatches.values()) {
result.addAll(ws);
}
if (clear) {
childWatches.clear();
}
}
return result;
case NodeDataChanged:// 節點數據變化
case NodeCreated:// 創建節點
synchronized (dataWatches) {
//移除clientPath對應的Watcher后全部添加至結果集合
addTo(dataWatches.remove(clientPath), result);
}
synchronized (existWatches) {
//移除clientPath對應的Watcher后全部添加至結果集合
addTo(existWatches.remove(clientPath), result);
}
break;
case NodeChildrenChanged: // 節點子節點變化
synchronized (childWatches) {
// 移除clientPath對應的Watcher后全部添加至結果集合
addTo(childWatches.remove(clientPath), result);
}
break;
case NodeDeleted:// 刪除節點
synchronized (dataWatches) {
// 移除clientPath對應的Watcher后全部添加至結果集合
addTo(dataWatches.remove(clientPath), result);
}
// XXX This shouldn't be needed, but just in case
synchronized (existWatches) {
Set<Watcher> list = existWatches.remove(clientPath);
if (list != null) {
addTo(list, result);
LOG.warn("We are triggering an exists watch for delete! Shouldn't happen!");
}
}
synchronized (childWatches) {
//移除clientPath對應的Watcher后全部添加至結果集合
addTo(childWatches.remove(clientPath), result);
}
break;
default:
String msg = "Unhandled watch event type " + type
+ " with state " + state + " on path " + clientPath;
LOG.error(msg);
throw new RuntimeException(msg);
}
return result;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。