您好,登錄后才能下訂單哦!
1.watcher方式一(監聽當前節點發生變更)
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(); //創建節點watcher監聽 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方式二(監聽當前節點的子節點發生變更)
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(); //創建子節點watcher監聽 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);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。