package com.sandul.zookeeper01;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.Test;
public class Demo01Test {
@Test
public void test01() throws Exception{
ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1000, 3, 10);
final CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1", 3000, 1000, retry);
client.start();
System.out.println("连接成功。。。。。。");
final NodeCache nodeCache = new NodeCache(client, "/a");
nodeCache.start(true);
System.out.println(nodeCache.getCurrentData());
nodeCache.getListenable().addListener(new NodeCacheListener() {
public void nodeChanged() throws Exception {
String data = new String(nodeCache.getCurrentData().getData());
System.out.println("数据Watcher:path="+nodeCache.getCurrentData().getPath()+" :data="+data);
}
});
PathChildrenCache childrenCache = new PathChildrenCache(client, "/a", true);
childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
if (pathChildrenCacheEvent.getType()==PathChildrenCacheEvent.Type.CHILD_UPDATED){
System.out.println("子节点更新。。。");
System.out.println("节点:"+pathChildrenCacheEvent.getData().getPath()+" 数据:"+(new String(pathChildrenCacheEvent.getData().getData())));
}else if (pathChildrenCacheEvent.getType()==PathChildrenCacheEvent.Type.CHILD_ADDED){
System.out.println("子节点添加。。。");
System.out.println("节点:"+pathChildrenCacheEvent.getData().getPath()+" 数据:"+(new String(pathChildrenCacheEvent.getData().getData())));
}else if (pathChildrenCacheEvent.getType()==PathChildrenCacheEvent.Type.CHILD_REMOVED){
System.out.println("子节点删除。。。");
System.out.println("节点:"+pathChildrenCacheEvent.getData().getPath()+" 数据:"+(new String(pathChildrenCacheEvent.getData().getData())));
}else if (pathChildrenCacheEvent.getType()==PathChildrenCacheEvent.Type.INITIALIZED){
System.out.println("初始化。。。");
}else if (pathChildrenCacheEvent.getType()==PathChildrenCacheEvent.Type.CONNECTION_RECONNECTED){
System.out.println("重新连接。。。");
}else if (pathChildrenCacheEvent.getType()==PathChildrenCacheEvent.Type.CONNECTION_SUSPENDED){
System.out.println("连接失效。。。");
}else if (pathChildrenCacheEvent.getType()==PathChildrenCacheEvent.Type.CONNECTION_LOST){
System.out.println("连接失效后稍等一会儿执行。。。");
}
}
});
TreeCache treeCache = new TreeCache(client, "/a");
treeCache.start();
System.out.println(treeCache.getCurrentData("/a"));
treeCache.getListenable().addListener(new TreeCacheListener() {
public void childEvent(CuratorFramework curator, TreeCacheEvent event) throws Exception {
if (event.getType()==TreeCacheEvent.Type.NODE_ADDED){
System.out.println("节点添加:"+event.getData().getPath()+" data:"+(new String(event.getData().getData())));
}else if (event.getType()==TreeCacheEvent.Type.NODE_UPDATED){
System.out.println("节点更新:"+event.getData().getPath()+" data:"+(new String(event.getData().getData())));
}else if (event.getType()==TreeCacheEvent.Type.NODE_REMOVED){
System.out.println("节点移除:"+event.getData().getPath()+" data:"+(new String(event.getData().getData())));
}else if (event.getType()==TreeCacheEvent.Type.INITIALIZED){
System.out.println("初始化完成");
}else if (event.getType()==TreeCacheEvent.Type.CONNECTION_SUSPENDED){
System.out.println("连接过时");
}else if (event.getType()==TreeCacheEvent.Type.CONNECTION_RECONNECTED){
System.out.println("重新连接");
}else if (event.getType()==TreeCacheEvent.Type.CONNECTION_LOST){
System.out.println("连接过时一段时间了稍后再试");
}
}
});
Thread.sleep(100000);
nodeCache.close();
childrenCache.close();
treeCache.close();
client.close();
}
}