Zookeeper中有 watch时间,是一次性出发的,当watch监视到数据变化时,就通知该watch的client,及watcher,watcher注册一次,只能监听一次的操作,这点很重要。
事件类型
EventType.NodeCreate 节点创建
EventType.NodeDataChange 节点数据改变
EventType.NodeChildrenChange 节点的子节点变更
EventType.NodeDeleted 节点 删除
连接类型
KeeperState.Disconnected 连接诶失败
KeeperState.AuthFailed 授权失败
KeeperState.Expired 连接超时
KeeperState.SyncConnected连接成功
案例
Watcher 需要继承Watcher 接口,复写process方法,来处理监听事件,watcher的事件,也就是process方法,只触发一次,不会一直监听。每次如果想监听create、setData、delete等事件,如果想监听节点的 NodeChildrenChange 、NodeCreate 等事件前,需要自己添加一个监听器,可以使用exists、getData等方法,先给他一个watcher,监听,但是watcher 只执行一次。。。。
package com.yellowcong.zookeeper;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
/**
* 创建日期:2017年10月14日 <br/>
* 创建用户:yellowcong <br/>
* 功能描述:
*/
public class DemoWatcher implements Watcher {
// 连接到ZK服务,多个可以用逗号分割写
// private static final String CONNECT_PATH =
// "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
private static final String CONNECT_PATH = "192.168.66.110:2181,192.168.66.110:2182,192.168.66.110:2183";
// 超时时间
private static final int SESSION_TIME_OUT = 20000;
private static final CountDownLatch countDownLatch = new CountDownLatch(1);
private ZooKeeper zk = null;
//用来计数,判断调用了几次watcher方法
private static final AtomicInteger cnt = new AtomicInteger(0);
//设定编码
private static final Charset CHARSET = Charset.forName("UTF-8");
public static void main(String[] args) throws Exception {
DemoWatcher demo = new DemoWatcher();
//获取连接
demo.createConnection(CONNECT_PATH, SESSION_TIME_OUT);
//睡眠
Thread.sleep(1000);
//使用默认的watcher, watcher只执行一次,不是一直执行 ,exists 没有触发事件
Stat stat = demo.zk.exists("/watcher", true);
if(stat == null){
String path = demo.zk.create("/watcher", //节点
"watcher".getBytes(CHARSET), //节点内容
Ids.OPEN_ACL_UNSAFE, //所有热都可以访问
CreateMode.EPHEMERAL); //临时节点
System.out.println(path);
}
//更新了数据,而且设定了监听watch
demo.zk.exists("/watcher", true);
demo.zk.setData("/watcher", "update".getBytes(), -1);
}
/**
* 设定watcher处理的方法
*/
public void process(WatchedEvent event) {
if(event == null){
return ;
}
KeeperState state = event.getState() ;
EventType type = event.getType();
//受影响的节点
String path = event.getPath();
System.out.println("---------------------Watcher调用次数\t"+cnt.incrementAndGet()+"---------------------");
System.out.println("path\t"+path);
System.out.println("状态\t"+state);
System.out.println("事件类型\t"+type);
//判断是否连接了
if(state == KeeperState.SyncConnected){
switch (event.getType()) {
case None:
countDownLatch.countDown();
System.out.println("事件\t连接成功");
break;
case NodeCreated:
System.out.println("事件\t节点创建");
break;
case NodeChildrenChanged:
System.out.println("事件\t节点创建");
break;
case NodeDeleted:
System.out.println("事件\t节点删除");
break;
case NodeDataChanged:
System.out.println("事件\t节点变更");
break;
default:
break;
}
}
System.out.println("--------------------------------------------------");
}
public void createConnection(String path,int timeOut) {
try {
//关闭连接
this.closeConnection();
//获取连接,设定监听的Watch
zk = new ZooKeeper(path, timeOut, this);
//等待
countDownLatch.await();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 关闭连接
* 创建日期:2017年10月14日<br/>
* 创建用户:yellowcong<br/>
* 功能描述:
*/
public void closeConnection(){
try {
if(zk != null){
zk.close();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
打印结果
Watcher事件,只执行一次,而且监听操作如果没有触发事件,就会得等到下一个事件,直到触发,这个操作,比较的麻烦,而且不好控制
---------------------Watcher调用次数 1---------------------
path null
状态 SyncConnected
事件类型 None
事件 连接成功
--------------------------------------------------
---------------------Watcher调用次数 2---------------------
path /watcher
状态 SyncConnected
事件类型 NodeCreated
事件 节点创建
--------------------------------------------------
/watcher
---------------------Watcher调用次数 3---------------------
path /watcher
状态 SyncConnected
事件类型 NodeDataChanged
事件 节点变更
--------------------------------------------------