create
public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException
1.不支持遞歸創建節點,比如你要創建/master/a,如果master不存在,你就不能創建a( KeeperException.NoNode)。
2.不可以再ephemeral類型的節點下創建子節點(KeeperException.NoChildrenForEphemerals)。
3.如果指定的節點已經存在,會觸發KeeperException.NodeExists 異常,當然了對於sequential類型的,不會拋出這個異常。
4.數據內容不能超過1M,否則將拋出KeeperException異常。
對於一個節點a的整個生命周期中,可以對如下幾種情況進行監聽:
1.成功創建節點a
2.成功刪除節點a
3.節點a數據內容發生了改變
4.節點a的子列表發生了改變
想要在一個節點創建后,客戶端得到通知,客戶端需要在exists API中注冊watcher。
exists
這個函數很特殊,因為他可以監聽一個尚未存在的節點,這是getData,getChildren不能做到的。exists可以監聽一個節點的生命周期:從無到有,節點數據的變化,從有到無。
在傳遞給exists的watcher里,當path指定的節點被成功創建后,watcher會收到NodeCreated事件通知。當path所指定的節點的數據內容發送了改變后,wather會受到NodeDataChanged事件通知。
public Stat exists(String path, Watcher watcher) throws KeeperException, InterruptedException Return the stat of the node of the given path. Return null if no such a node exists. If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.
返回path指定的節點的stat,如果這個節點不存在,就返回null。當這個節點被創建,數據內容被修改,被刪除,所有對這個節點注冊了watcher的客戶端,都會收到對應的事件通知。
這里最需要注意的就是,exists可以監聽一個未存在的節點,這是他與getData,getChildren本質的區別。
getData
public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException Return the data and the stat of the node of the given path. If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the node. A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
getData也可以監聽一個節點,但是,如果他所要監聽的節點不存在,那么回拋出一個KeeperException異常並且這個異常的錯誤碼是NoNode。
滿足以下兩個條件,watcher才有可能受到事件通知:
1. 什么時候這個wacher被注冊到path指定的幾點上呢?第一你指定了watcher,第二,getChildren沒有拋出任何異常。
2. path指定的節點被成功刪除后,watcher可以收到通知。
path指定的節點的子列表成功的添加或者刪除了新的節點,watcher也會收到通知。
如果path指定的節點不存在,會拋出異常。
getChildren
public List<String> getChildren(String path, boolean watch) throws KeeperException, InterruptedException Return the list of the children of the node of the given path. If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch willbe triggered by a successful operation that deletes the node of the given path or creates/delete a child under the node. The list of children returned is not sorted and no guarantee is provided as to its natural or lexical order. A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
滿足以下兩個條件,watcher才有可能受到事件通知:
1. 什么時候這個wacher被注冊到path指定的幾點上呢?第一你指定了watcher,第二,getChildren沒有拋出任何異常。
2. path指定的節點被成功刪除后,watcher可以收到通知。
path指定的節點的數據內容發生改變,watcher會收到NodeChildrenChanged通知。
如果path指定的節點不存在,會拋出異常。
作者:FrancisWang
郵箱:franciswbs@163.com
出處:http://www.cnblogs.com/francisYoung/
本文地址:http://www.cnblogs.com/francisYoung/p/5224491.html
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。