您好,登錄后才能下訂單哦!
接著上一篇文章,繼續(xù)分析和Watcher相關(guān)的類(lèi)的源碼。
public Set<Watcher> materialize(Watcher.Event.KeeperState state,
Watcher.Event.EventType type, String path);
該接口,只有一個(gè)方法,需要實(shí)現(xiàn)。該方法表示事件發(fā)生時(shí),返回需要被通知的Watcher集合,可能為空集合。
1、ZKWatchManager是ZooKeeper的內(nèi)部類(lèi),實(shí)現(xiàn)了ClientWatchManager。
2、ZKWatchManager定義了三個(gè)Map鍵值對(duì),鍵為節(jié)點(diǎn)路徑,值為Watcher。分別對(duì)應(yīng)數(shù)據(jù)變化的Watcher、節(jié)點(diǎn)是否存在的Watcher、子節(jié)點(diǎn)變化的Watcher。
static class ZKWatchManager implements ClientWatchManager {
//數(shù)據(jù)變化的watchers
private final Map<String, Set<Watcher>> dataWatches =
new HashMap<String, Set<Watcher>>();
//節(jié)點(diǎn)存在與否的watchers
private final Map<String, Set<Watcher>> existWatches =
new HashMap<String, Set<Watcher>>();
//子節(jié)點(diǎn)變化的watchers
private final Map<String, Set<Watcher>> childWatches =
new HashMap<String, Set<Watcher>>();
3、materialize方法
該方法在事件發(fā)生后,返回需要被通知的Watcher集合。在該方法中,首先會(huì)根據(jù)EventType類(lèi)型確定相應(yīng)的事件類(lèi)型,然后根據(jù)事件類(lèi)型的不同做出相應(yīng)的操作,如針對(duì)None類(lèi)型,即無(wú)任何事件,則首先會(huì)從三個(gè)鍵值對(duì)中刪除clientPath對(duì)應(yīng)的Watcher,然后將剩余的Watcher集合添加至結(jié)果集合;針對(duì)NodeDataChanged和NodeCreated事件而言,其會(huì)從dataWatches和existWatches中刪除clientPath對(duì)應(yīng)的Watcher,然后將剩余的Watcher集合添加至結(jié)果集合。
@Override
public Set<Watcher> materialize(Watcher.Event.KeeperState state,
Watcher.Event.EventType type,
String clientPath)
{
//返回結(jié)果集合
Set<Watcher> result = new HashSet<Watcher>();
switch (type) {//事件類(lèi)型
case None://無(wú)類(lèi)型
//添加默認(rèn)watcher
result.add(defaultWatcher);
//根據(jù)disableAutoWatchReset和Zookeeper的狀態(tài)是否為同步連接判斷是否需要清空
boolean clear = disableAutoWatchReset && state != Watcher.Event.KeeperState.SyncConnected;
//針對(duì)3個(gè)不同的watcherMap進(jìn)行操作
synchronized(dataWatches) {
for(Set<Watcher> ws: dataWatches.values()) {
// 添加至結(jié)果集合
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:// 節(jié)點(diǎn)數(shù)據(jù)變化
case NodeCreated:// 創(chuàng)建節(jié)點(diǎn)
synchronized (dataWatches) {
//移除clientPath對(duì)應(yīng)的Watcher后全部添加至結(jié)果集合
addTo(dataWatches.remove(clientPath), result);
}
synchronized (existWatches) {
//移除clientPath對(duì)應(yīng)的Watcher后全部添加至結(jié)果集合
addTo(existWatches.remove(clientPath), result);
}
break;
case NodeChildrenChanged: // 節(jié)點(diǎn)子節(jié)點(diǎn)變化
synchronized (childWatches) {
// 移除clientPath對(duì)應(yīng)的Watcher后全部添加至結(jié)果集合
addTo(childWatches.remove(clientPath), result);
}
break;
case NodeDeleted:// 刪除節(jié)點(diǎn)
synchronized (dataWatches) {
// 移除clientPath對(duì)應(yīng)的Watcher后全部添加至結(jié)果集合
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對(duì)應(yīng)的Watcher后全部添加至結(jié)果集合
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;
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。