Curator Watch事件监听
-
Zookeeper提供了三种事件监听:
- NodeCache: 给指定节点设置监听器
- PathChildrenCache: 监听指定节点下的所有子节点
- TreeCache: 监听指定节点以及该节点下的所有子节点
import org.apache.curator.RetryPolicy;
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.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CuratorWatcherTest {
private CuratorFramework clientBuild;
/**
* Before 所有Test测试方法执行时先执行该方法
* 通过Curator操作zookeeper
* 建立连接
* namespace -- 默认所有操作都是在test节点下操作
*/
@Before
public void testConnectBuild(){
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
clientBuild = CuratorFrameworkFactory.builder()
.connectString("192.168.192.137:2181")
.sessionTimeoutMs(60000)
.connectionTimeoutMs(15000)
.retryPolicy(retryPolicy)
.namespace("test")
.build();
clientBuild.start();
}
/**
* 所有Test测试方法执行完成后执行该方法
*/
@After
public void close(){
if (clientBuild != null){
clientBuild.close();
}
}
/**
* NodeCache: 给指定节点设置监听器
* NodeCache(CuratorFramework client, String path, boolean dataIsCompressed)
* dataIsCompressed: 如果true,表示传递的数据会进行压缩,传递速度快,但取数据时需要把压缩的数据进行转换,默认为false
*/
@Test
public void testNodeCache() throws Exception {
// 创建NodeCache对象
NodeCache nodeCache = new NodeCache(clientBuild,"/app1");
// 注册监听
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("发生变化"); // 增删改都会发生变化
// 获取节点修改后的数据
byte[] data = nodeCache.getCurrentData().getData();
System.out.println(new String(data));
}
});
// 开启监听 如果设置为true,加载缓冲数据
nodeCache.start(true);
// 因为为单元测试,需要保证方法一直运行
while (true){
}
}
/**
* PathChildrenCache: 监听指定节点下的所有子节点
* @throws Exception
*/
@Test
public void testPathChildrenCache() throws Exception{
// 创建监听对象
PathChildrenCache pathChildrenCache = new PathChildrenCache(clientBuild,"/app2",true);
// 注册监听
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
System.out.println("子节点发生变化"); // 子节点增删改都会发生变化
System.out.println(pathChildrenCacheEvent);
// 获取客户端对子节点的修改操作,同时获取子节点修改后的数据信息
// 1. 获取当前的操作类型
PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();
// 2. 判断是否为修改操作 PathChildrenCacheEvent.Type.CHILD_UPDATED
if (type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
System.out.println("子节点发生了修改操作");
// 3. 获取子节点修改后的数据信息
byte[] data = pathChildrenCacheEvent.getData().getData();
System.out.println("子节点修改后的数据信息:" + new String(data));
}
}
});
// 开启监听
pathChildrenCache.start();
while (true){
}
}
/**
* TreeCache: 监听指定节点以及该节点下的所有子节点
* @throws Exception
*/
@Test
public void testTreeCache() throws Exception{
TreeCache treeCache = new TreeCache(clientBuild,"/app2");
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
System.out.println("节点发生了变化");
System.out.println(treeCacheEvent);
}
});
treeCache.start();
while (true){
}
}
}