真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

zookeeper(9)源碼分析-事件監(jiān)聽Watcher(2)

接著上一篇文章,繼續(xù)分析和Watcher相關(guān)的類的源碼。

創(chuàng)新互聯(lián)建站專注于企業(yè)營銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、成安網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)商城開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為成安等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

ClientWatchManager

public Set materialize(Watcher.Event.KeeperState state,
        Watcher.Event.EventType type, String path);

該接口,只有一個方法,需要實(shí)現(xiàn)。該方法表示事件發(fā)生時,返回需要被通知的Watcher集合,可能為空集合。

ZKWatchManager

1、ZKWatchManager是ZooKeeper的內(nèi)部類,實(shí)現(xiàn)了ClientWatchManager。
2、ZKWatchManager定義了三個Map鍵值對,鍵為節(jié)點(diǎn)路徑,值為Watcher。分別對應(yīng)數(shù)據(jù)變化的Watcher、節(jié)點(diǎn)是否存在的Watcher、子節(jié)點(diǎn)變化的Watcher。

static class ZKWatchManager implements ClientWatchManager {
        //數(shù)據(jù)變化的watchers
        private final Map> dataWatches =
            new HashMap>();
        //節(jié)點(diǎn)存在與否的watchers
        private final Map> existWatches =
            new HashMap>();
        //子節(jié)點(diǎn)變化的watchers
        private final Map> childWatches =
            new HashMap>();

3、materialize方法

該方法在事件發(fā)生后,返回需要被通知的Watcher集合。在該方法中,首先會根據(jù)EventType類型確定相應(yīng)的事件類型,然后根據(jù)事件類型的不同做出相應(yīng)的操作,如針對None類型,即無任何事件,則首先會從三個鍵值對中刪除clientPath對應(yīng)的Watcher,然后將剩余的Watcher集合添加至結(jié)果集合;針對NodeDataChanged和NodeCreated事件而言,其會從dataWatches和existWatches中刪除clientPath對應(yīng)的Watcher,然后將剩余的Watcher集合添加至結(jié)果集合。

@Override
        public Set materialize(Watcher.Event.KeeperState state,
                                        Watcher.Event.EventType type,
                                        String clientPath)
        {
            //返回結(jié)果集合
            Set result = new HashSet();

            switch (type) {//事件類型
            case None://無類型
                //添加默認(rèn)watcher
                result.add(defaultWatcher);
                //根據(jù)disableAutoWatchReset和Zookeeper的狀態(tài)是否為同步連接判斷是否需要清空
                boolean clear = disableAutoWatchReset && state != Watcher.Event.KeeperState.SyncConnected;
                //針對3個不同的watcherMap進(jìn)行操作
                synchronized(dataWatches) {
                    for(Set ws: dataWatches.values()) {
                        // 添加至結(jié)果集合
                        result.addAll(ws);
                    }
                    if (clear) { // 是否需要清空
                        dataWatches.clear();
                    }
                }

                synchronized(existWatches) {
                    for(Set ws: existWatches.values()) {
                        result.addAll(ws);
                    }
                    if (clear) {
                        existWatches.clear();
                    }
                }

                synchronized(childWatches) {
                    for(Set 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對應(yīng)的Watcher后全部添加至結(jié)果集合
                    addTo(dataWatches.remove(clientPath), result);
                }
                synchronized (existWatches) {
                    //移除clientPath對應(yīng)的Watcher后全部添加至結(jié)果集合
                    addTo(existWatches.remove(clientPath), result);
                }
                break;
            case NodeChildrenChanged: // 節(jié)點(diǎn)子節(jié)點(diǎn)變化
                synchronized (childWatches) {
                    // 移除clientPath對應(yīng)的Watcher后全部添加至結(jié)果集合
                    addTo(childWatches.remove(clientPath), result);
                }
                break;
            case NodeDeleted:// 刪除節(jié)點(diǎn)
                synchronized (dataWatches) {
                    // 移除clientPath對應(yīng)的Watcher后全部添加至結(jié)果集合
                    addTo(dataWatches.remove(clientPath), result);
                }
                // XXX This shouldn't be needed, but just in case
                synchronized (existWatches) {
                    Set 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對應(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;
        }
    }

新聞標(biāo)題:zookeeper(9)源碼分析-事件監(jiān)聽Watcher(2)
本文路徑:http://weahome.cn/article/ijjcsh.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部