<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 一、ZKClient的基本使用 public class ZkClientBase { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; /** session超時時間 */ static final int SESSION_OUTTIME = 5000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000); //1. create and delete方法 zkc.createEphemeral("/temp"); zkc.createPersistent("/super/c1", true); Thread.sleep(10000); zkc.delete("/temp"); zkc.deleteRecursive("/super"); //2. 設置path和data 并且讀取子節點和每個節點的內容 zkc.createPersistent("/super", "1234"); zkc.createPersistent("/super/c1", "c1內容"); zkc.createPersistent("/super/c2", "c2內容"); List<String> list = zkc.getChildren("/super"); for(String p : list){ System.out.println(p); String rp = "/super/" + p; String data = zkc.readData(rp); System.out.println("節點為:" + rp + ",內容為: " + data); } 3. 更新和判斷節點是否存在 zkc.writeData("/super/c1", "新內容"); System.out.println(zkc.readData("/super/c1")); System.out.println(zkc.exists("/super/c1")); 4.遞歸刪除/super內容 zkc.deleteRecursive("/super"); } } ## 二、zkClient使用 subscribeChildChanges subscribeChildChanges 方法 參數一:path 路徑 參數二、時間了IZKChildListener接口的類(如:實例化IZkChildListener類) 只需要重寫其handleChildChange(String parentPath,List<String> currentChilds) 其中parentPath為最新的子節點列表(相對路徑) IZkChildListener事件說明針對于下面三個時間觸發 新增子節點、減少子節點、刪除節點 ### 代碼實現 public class ZkClientWatcher1 { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; /** session超時時間 */ static final int SESSION_OUTTIME = 5000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000); //對父節點添加監聽子節點變化。 zkc.subscribeChildChanges("/super", new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { System.out.println("parentPath: " + parentPath); System.out.println("currentChilds: " + currentChilds); } }); Thread.sleep(3000); zkc.createPersistent("/super"); Thread.sleep(1000); zkc.createPersistent("/super" + "/" + "c1", "c1內容"); Thread.sleep(1000); zkc.createPersistent("/super" + "/" + "c2", "c2內容"); Thread.sleep(1000); zkc.delete("/super/c2"); Thread.sleep(1000); zkc.deleteRecursive("/super"); Thread.sleep(Integer.MAX_VALUE); } } ### 注意 WatchedEvent : 包含三部分:通知狀態、時間類型、節點路徑,有了這個路徑就可獲取到 不監聽數據的更新 ## 三、subscribeDataChanges public class ZkClientWatcher2 { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; /** session超時時間 */ static final int SESSION_OUTTIME = 5000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000); zkc.createPersistent("/super", "1234"); //對父節點添加監聽子節點變化。 zkc.subscribeDataChanges("/super", new IZkDataListener() { @Override public void handleDataDeleted(String path) throws Exception { System.out.println("刪除的節點為:" + path); } @Override public void handleDataChange(String path, Object data) throws Exception { System.out.println("變更的節點為:" + path + ", 變更內容為:" + data); } }); Thread.sleep(3000); zkc.writeData("/super", "456", -1); Thread.sleep(1000); zkc.delete("/super"); Thread.sleep(Integer.MAX_VALUE); } }
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看