zoukankan      html  css  js  c++  java
  • ZooKeeper个人笔记之节点的监听

    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

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    Python正课132 —— Vue 进阶5
    Python正课131 —— Vue 进阶4
    Python正课130 —— Vue 进阶3
    logging模块
    作业20
    suprocess模块
    configparser模块
    hashlib模块
    shutil模块
    序列化模块
  • 原文地址:https://www.cnblogs.com/francisYoung/p/5224491.html
Copyright © 2011-2022 走看看