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

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

ZooKeeper个人笔记之节点的监听的更多相关文章

  1. Zookeeper开源客户端Curator之事件监听详解

    Curator对Zookeeper典型场景之事件监听进行封装,提供了使用参考.这篇博文笔者带领大家了解一下Curator的实现方式. 引入依赖 对于Curator封装Zookeeper的典型场景使用都 ...

  2. Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)

    8.1.常用命令 启动ZK服务 bin/zkServer.sh start 查看ZK服务状态 bin/zkServer.sh status 停止ZK服务 bin/zkServer.sh stop 重启 ...

  3. ZooKeeper(3.4.5) 使用Curator监听事件

    转载:http://www.mamicode.com/info-detail-494364.html 标签: ZooKeeper原生的API支持通过注册Watcher来进行事件监听,但是Watcher ...

  4. zookeeper curator使用caches实现各种监听

    1.篇首语 curator是zookeeper的一个高级api开发包.封装了zookeeper众多的recipes,并且实现了一些新的recipes原语,最重要的是基于zookeeper提供的各种机制 ...

  5. 如何在cocos中为节点添加监听事件

    一般在监听键盘事件时,可是采用以下方式来监听键盘事件: 以及记得定义取消监听的函数(这个摧毁函数会自己调用吗?): 同时这里还有一种传统的监听方式: 但是cocos官方的文档建议我们不要使用这种方式, ...

  6. angularJS1笔记-(2)-$watch方法监听变量的变化

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. Zookeeper 学习笔记之 节点个数

    zookeeper的节点配置的个数推荐是奇数个这是为什么呢? 选举机制 两种情况无法选出leader: 整个集群只有2台服务器(注意不是只剩2台,而是集群的总节点数为2) 整个集群超过半数机器挂掉. ...

  8. socket.io笔记二之事件监听回调函数接收一个客户端的回调函数

    //服务端 socket.on('test', function (name, fn) { console.log(name) //输出yes fn('woot'); }); //客户端 socket ...

  9. jQuery-mobile 学习笔记之三(事件监听)

    续上 触摸事件 - 当用户触摸屏幕时触发(敲击和滑动) 滚动事件 - 当上下滚动时触发 方向事件 - 当设备垂直或水平旋转时触发 页面事件 - 当页面被显示.隐藏.创建.载入以及/或卸载时触发 一.初 ...

随机推荐

  1. 22章、Java集合框架习题

    1.描述Java集合框架.列出接口.便利抽象类和具体类. Java集合框架支持2种容器:(1) 集合(Collection),存储元素集合 (2)图(Map),存储键值对.

  2. maven project中,在main方法上右键Run as Java Application时,提示错误:找不到或无法加载主类XXX.XXXX.XXX

    新建了一个maven project项目,经过一大堆的修改操作之后,突然发现在main方法上右键运行时,竟然提示:错误:找不到或无法加载主类xxx.xxx.xxx可能原因1.eclipse出问题了,在 ...

  3. nyoj 71 独木舟上的旅行(贪心专题)

    独木舟上的旅行 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...

  4. npapi插件开发流程与实例

    近期做NPAPI插件,网上的介绍还是比较多,但就是没有一个完整的例子,FQ也没找到,难得NPAPI要走向陌路了?就不去深究额,先解决目前遇到的问题. 现状:已有Activex(仅兼容IE32/64位浏 ...

  5. PHP 5.4 已废弃 magic_quotes_gpc,PHP安全转义函数详解(addslashes 、htmlspecialchars、htmlentities、mysql_real_escape_string、strip_tags)

    1. addslashes() addslashes()对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如果D ...

  6. 关于Azure带宽的测试

    以前见客户经常会碰到一些客户问我们你们Azure的带宽是多少,每次回答这个问题我们只能含糊地告诉客户一个大概数值,这样就会留给客户一个认为我们很不专业的印象,其实站在客户的角度我们也能理解,连这样的一 ...

  7. 用JS获取地址栏参数的方法

    采用正则表达式获取地址栏参数: function GetQueryString(name) {      var reg = new RegExp("(^|&)"+ nam ...

  8. C和指针 第八章 习题

    8.5矩阵运算,A是一个x行,y列矩阵,B是y行z列矩阵,把A和B相乘,结果是另外一个x行z列矩阵,每个位置的值由下公式决定,编写函数: #include <stdio.h> void m ...

  9. Android 一些基本组件的使用

    Dialog 基本用法 ,带自定义view AlertDialog dialog = new AlertDialog.Builder(context).setTitle("写入信息" ...

  10. 十一. 一步步破解JEB 2.0demo版一

    字符串解密算法还愿 jeb.jar为核心功能,所以主要分析这个 1. jar转dex在使用jeb分析 Android\sdk\build-tools\23.0.3 dx.bat --dex --out ...