您的位置:首页 > Web前端 > Node.js

Curator教程(二)Path Cache监听ZNode

2016-12-09 11:59 344 查看
在实际应用开发中,当某个ZNode发生变化后我们需要得到通知并做一些后续处理,Curator Recipes提供了Path Cache 来帮助我们轻松实现watch ZNode。

Path Cache

Path Cache可以监控ZNode子结点的变化,例如:add,update,delete。

A Path Cache is used to watch a ZNode. Whenever a child is added, updated or removed, the Path Cache will change its state to contain the current set of children, the children’s data and the children’s state.

The cache must be started by calling start(). Call close() when you are through with the cache.

Maven依赖

<curator.version>2.11.1</curator.version>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>


示例代码如下:

package com.bytebeats.zookeeper.curator.ch2;

import com.bytebeats.zookeeper.curator.CuratorUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;

import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* ${DESCRIPTION}
*
* @author Ricky Fung
* @create 2016-12-10 15:37
*/
public class CuratorPathCacheDemo {

private String path = "/pandora";

public static final Charset CHARSET = Charset.forName("UTF-8");

public static void main(String[] args) {

try{
new CuratorPathCacheDemo().start();
} catch (Exception e){
e.printStackTrace();
}

}

private void start() throws Exception {
CuratorFramework client = CuratorUtils.getCuratorClient();
try{
client.start();

final PathChildrenCache pathChildrenCache = new PathChildrenCache(client, path, true);

pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework,
PathChildrenCacheEvent event) throws Exception {

System.out.println("======== catch children change =======");
System.out.println("update event type:" + event.getType() +
",path:" + event.getData().getPath() + ",data:" + new String(event.getData().getData(), CHARSET));

List<ChildData> childDataList = pathChildrenCache.getCurrentData();
if (childDataList != null && childDataList.size() > 0) {
System.out.println("path all children list:");
for (ChildData childData : childDataList) {
System.out.println("path:" + childData.getPath() + "," + new String(childData.getData(), CHARSET));
}
}
}
});

pathChildrenCache.start();  //must call start();

TimeUnit.MINUTES.sleep(5);

pathChildrenCache.close();

}finally {
if(client!=null)
client.close();
}
}
}


点此下载完整代码:https://github.com/TiFG/zookeeper-samples

参考

Path Cache:http://curator.apache.org/curator-recipes/path-cache.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  curator watch listener