1.watcher方式一(監(jiān)聽當(dāng)前節(jié)點(diǎn)發(fā)生變更)
成都創(chuàng)新互聯(lián)一直通過網(wǎng)站建設(shè)和網(wǎng)站營(yíng)銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實(shí)效"的一站式服務(wù),以成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、移動(dòng)互聯(lián)產(chǎn)品、全網(wǎng)整合營(yíng)銷推廣服務(wù)為核心業(yè)務(wù)。十多年網(wǎng)站制作的經(jīng)驗(yàn),使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標(biāo)準(zhǔn)網(wǎng)站,不但價(jià)格便宜而且實(shí)用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡(jiǎn)單易用,維護(hù)方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。
String address = "mini1:2181,mini2:2181,mini3:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3)); client.start(); //創(chuàng)建節(jié)點(diǎn)watcher監(jiān)聽 final NodeCache cache = new NodeCache(client, "/super", false); cache.start(true); cache.getListenable().addListener(new NodeCacheListener() { public void nodeChanged() throws Exception { System.out.println(cache.getCurrentData().getPath()); System.out.println(cache.getCurrentData().getStat()); } });
2.watcher方式二(監(jiān)聽當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn)發(fā)生變更)
String address = "mini1:2181,mini2:2181,mini3:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3)); client.start(); //創(chuàng)建子節(jié)點(diǎn)watcher監(jiān)聽 PathChildrenCache cache = new PathChildrenCache(client, "/super", true); cache.start(StartMode.POST_INITIALIZED_EVENT); cache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework cf, PathChildrenCacheEvent event) throws Exception { System.out.println(event.getType()); } }); Thread.sleep(Long.MAX_VALUE);