ZooKeerper学习之Watcher
ZooKeeper为我们提供了用于监视结点变化的Watcher机方法制:
1、可以注册Watcher的方法:getData()、exists()、getChildren()。我们可以通过查看ZooKeeper API看到
getData方法:
void getData(String path, boolean watch, AsyncCallback.DataCallback cb, Object ctx)
byte[] getData(String path, boolean watch, Stat stat)
void getData(String path, Watcher watcher, AsyncCallback.DataCallback cb, Object ctx)
byte[] getData(String path, Watcher watcher, Stat stat)
exists方法:
Stat exists(String path, boolean watch)
Return the stat of the node of the given path.
void exists(String path, boolean watch, AsyncCallback.StatCallback cb, Object ctx)
The Asynchronous version of exists.
Stat exists(String path, Watcher watcher)
Return the stat of the node of the given path.
void exists(String path, Watcher watcher, AsyncCallback.StatCallback cb, Object ctx)
The Asynchronous version of exists.
getChildren方法:
List<String> getChildren(String path, boolean watch)
Return the list of the children of the node of the given path.
void getChildren(String path, boolean watch, AsyncCallback.Children2Callback cb, Object ctx)
The Asynchronous version of getChildren.
void getChildren(String path, boolean watch, AsyncCallback.ChildrenCallback cb, Object ctx)
The Asynchronous version of getChildren.
List<String> getChildren(String path, boolean watch, Stat stat)
For the given znode path return the stat and children list.
List<String> getChildren(String path, Watcher watcher)
Return the list of the children of the node of the given path.
void getChildren(String path, Watcher watcher, AsyncCallback.Children2Callback cb, Object ctx)
The Asynchronous version of getChildren.
void getChildren(String path, Watcher watcher, AsyncCallback.ChildrenCallback cb, Object ctx)
The Asynchronous version of getChildren.
List<String> getChildren(String path, Watcher watcher, Stat stat)
For the given znode path return the stat and children list.
2、可以触发Watcher的方法有:create()、delete()、setData()。通过查看API:
create方法:
String create(String path, byte[] data, List<ACL> acl, CreateMode createMode)
Create a node with the given path.
void create(String path, byte[] data, List<ACL> acl, CreateMode createMode, AsyncCallback.StringCallback cb, Object ctx)
The Asynchronous version of create.
delete方法:
void delete(String path, int version)
Delete the node with the given path.
void delete(String path, int version, AsyncCallback.VoidCallback cb, Object ctx)
The Asynchronous version of delete.
setData方法:
Stat setData(String path, byte[] data, int version)
Set the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions).
void setData(String path, byte[] data, int version, AsyncCallback.StatCallback cb, Object ctx)
The Asynchronous version of setData.
3、一个Watcher实例是一个回调函数,被调用一次以后就会被移除,如果还需观察结点数据的变化,则还需再次注册Watcher。
4、当我们New ZooKeeper时,注册的Watcher被称为default Watcher,它不是一次性的,只会对client的连接状态变化做出反应。当发生CONNECTIONLOSS之后,只要在session_timeout时间之内能够再次连上(即不发生SESSION_EXPIRED),那么这个连接注册的watcher依然有效。
5、同一个客户端,对某一个节点注册了多次watcher,那么只会收到一次通知。
6、一些操作对应的事件类型:
|
Event for "/path" |
Event for "/path/child" |
|
|
Create("/path",...) |
EventType.NodeCreated |
/ |
|
Delete("/path",...) |
EventType.Deleted |
/ |
|
setData("/path") |
EventType.NodeDataChanged |
/ |
|
Create("/path/child") |
EventType.NodeChildrenChanged |
EventType.NodeCreated |
|
Delete("/path/child") |
EventType.NodeChildrenChanged |
EventType.NodeDeleted |
|
setData("/path/child") |
/ |
EventType.NodeDataChanged |
7、事件类型与Watcher的对应关系:
|
event for "/path" |
Default watcher |
Exists("/path") |
getData("/path") |
getChildren("/path") |
|
EventType.None |
√ |
√ |
√ |
√ |
|
EventType.NodeCreated |
√ |
√ |
||
|
EventType.NodeDeleted |
√ |
√ |
||
|
EventType.NodeDataChanged |
√ |
√ |
||
|
EventType.NodeChildrenChanged |
√ |
一个小例子:
package org.apache.zookeeper.test; import java.io.IOException; import org.apache.log4j.PropertyConfigurator;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper; public class Demo { public static void main(String[] args) throws IOException, KeeperException, InterruptedException { // 创建一个与服务器的连接
ZooKeeper zk = new ZooKeeper("192.168.11.201:2181", 6000, new Watcher() {
// 监控所有被触发的事件
/**
* EventType:
* None
* NodeCreated
* NodeChildrenChanged
* NodeDataChanged
* NodeDeleted
*/
public void process(WatchedEvent event) {
System.out.println("EVENT:" + event.getType());
}
}); // 查看根节点
System.out.println("ls / => " + zk.getChildren("/", true)); // 创建一个目录节点
if (zk.exists("/node", true) == null) {
zk.create("/node", "this is a znode".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("create /node this is a znode");
// 查看/node节点数据
System.out.println("get /node => " + new String(zk.getData("/node", false, null)));
// 查看根节点
System.out.println("ls / => " + zk.getChildren("/", true));
} // 创建一个子目录节点
if (zk.exists("/node/app1", true) == null) {
zk.create("/node/app1", "this is app1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("create /node/app1 app1");
// 查看node节点
System.out.println("ls /node => " + zk.getChildren("/node", true));
} // 修改节点数据
if (zk.exists("/node", true) != null) {
zk.setData("/node", "the data is changed".getBytes(), -1);
// 查看/node节点数据
System.out.println("get /node => " + new String(zk.getData("/node", false, null)));
} // 删除节点
if (zk.exists("/node/app1", true) != null) {
zk.delete("/node/app1", -1);
zk.delete("/node", -1);
// 查看根节点
System.out.println("ls / => " + zk.getChildren("/", true));
} // 关闭连接
zk.close();
}
}
ZooKeerper学习之Watcher的更多相关文章
- vue.js 源代码学习笔记 ----- $watcher
/* @flow */ import { queueWatcher } from './scheduler' import Dep, { pushTarget, popTarget } from '. ...
- Zookeeper学习之Watcher事件类型和ZK状态
1.Zookeepe Watcherr的事件类型和ZK状态. zookeeper:Watcher.ZK状态,事件类型(一)zookeeper有watch事件,是一次性触发的,当watch监视的数据发 ...
- Zookeeper 系列(三)Zookeeper API
Zookeeper 系列(三)Zookeeper API 本节首先介绍 Zookeeper 的 Shell 命令,再对 Java 操作 Zookeeper 的三种方式进行讲解,本节先介绍 Zookee ...
- zookeeper原理与实践(一)----zookeeper的基本功能
我们现在围绕两个问题来学习zookeeper: 什么是zookeeper? zookeeper基础知识 什么是zookeeper: zookeeper是hadoop下面的一个子项目,是一个分布式协调服 ...
- 轻松学习Ionic (三) 安装sass并在webstorm中为scss添加watcher
1. 安装Ruby 最新为 2.1.5版本,不放心的话安装 Ruby 1.9.3-p551 安装过程中注意勾选上第二项!即将Ruby加入到可执行的环境变量中去. 安装结束后在命令行中 ...
- kafka学习笔记:知识点整理
一.为什么需要消息系统 1.解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 2.冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险. ...
- ElasticSearch 5学习(1)——安装Elasticsearch、Kibana和X-Pack
安装准备: 安装Elasticsearch唯一的要求是安装官方新版的Java,包括对应的Jdk. 安装Elasticsearch 首先到官网下载最新版本的Elasticsearch压缩包. 可以使用命 ...
- vue学习笔记
来公司以后就一直在用vue框架,不管是业务代码,还是做vue组件.关于vue有一些点是文档中没有提及的,记录一下以便以后查询- 一.Vue的特点 新一代 Vue.js 框架非常关注如何用极少的外部特性 ...
- hadoop学习笔记:zookeeper学习(上)
在前面的文章里我多次提到zookeeper对于分布式系统开发的重要性,因此对zookeeper的学习是非常必要的.本篇博文主要是讲解zookeeper的安装和zookeeper的一些基本的应用,同时我 ...
随机推荐
- 关于ajax请求,在参数中添加时间戳的必要性
之前做项目的时候,看到别人的前端ajax请求代码中,都会带有一个时间戳类型的参数,当时随便查了一下,是为了防止浏览器缓存的原因,所以也没有进行深究,每次写的时候也习惯性的带一个,最近新项目中,我发现好 ...
- 第一届山东省ACM——Phone Number(java)
Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ...
- html5 Websockets development guidance
1. WebSockets -- full-duplex communication The main HTML5 pillars include Markup, CSS3, and JavaScri ...
- Timequest GUI
Tasks界面 使用Tasks界面可以访问常用命令,例如生成网表建立报告等. 两个常用命令位于Tasks界面中:打开工程和编写SDC文件.其他命令在下面的文件夹中: Netlist Setup Rep ...
- 鼠标上下滑动总是放大缩小页面,按住ctrl+0
鼠标上下滑动总是放大缩小页面,可能是ctrl键失灵了,幸好键盘有两个ctrl键,按住ctrl+0,页面就正常了,吓死宝宝了,~~~~(>_<)~~~~
- Oracle:试图访问正在使用的事务临时表
处理步骤为 1.找到表ID select * from dba_objects where object_name like 'TPT_RPWORPA1_QRY' 2.通过表ID查找正在使用的事务 s ...
- Node.js配合node-http-proxy解决本地开发ajax跨域问题
情景: 前后端分离,本地前端开发调用接口会有跨域问题,一般有以下3种解决方法: 1. 后端接口打包到本地运行(缺点:每次后端更新都要去测试服下一个更新包,还要在本地搭建java运行环境,麻烦) 2. ...
- js_闭包
先从闭包特点解释,应该更好理解.闭包的两个特点:1.作为一个函数变量的一个引用 - 当函数返回时,其处于激活状态.2.一个闭包就是当一个函数返回时,一个没有释放资源的栈区.其实上面两点可以合成一点,就 ...
- caffe_实战之两个简单的例子(物体分类和人脸检测)
一.物体分类: 这里使用的是caffe官网中自带的例子,我这里主要是对代码的解释~ 首先导入一些必要的库: import caffe import numpy as np import matplot ...
- 2015 史考特(Scottrade)开户指南 + 招商银行香港一卡通汇款【图文教程】
最近刚开始炒美股.总的来说分为两步:一是开户,即选一个美股券商开设股票交易账户:二是汇款注资,把人民币换成美元转账到股票交易账户上.上述第一点其实相对简单,美股券商大多都对美国以外的外国人开放申请,且 ...