接著上一篇文章,繼續(xù)分析和Watcher相關(guān)的類的源碼。
站在用戶的角度思考問題,與客戶深入溝通,找到農(nóng)安網(wǎng)站設(shè)計(jì)與農(nóng)安網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋農(nóng)安地區(qū)。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)部類,實(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類型確定相應(yīng)的事件類型,然后根據(jù)事件類型的不同做出相應(yīng)的操作,如針對(duì)None類型,即無任何事件,則首先會(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) {//事件類型
case None://無類型
//添加默認(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;
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁題目:zookeeper(9)源碼分析-事件監(jiān)聽Watcher(2)-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://jinyejixie.com/article0/gppoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、搜索引擎優(yōu)化、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容