Curator教程(二)Path Cache监听ZNode
2016-12-09 11:59
344 查看
在实际应用开发中,当某个ZNode发生变化后我们需要得到通知并做一些后续处理,Curator Recipes提供了Path Cache 来帮助我们轻松实现watch ZNode。
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.
示例代码如下:
点此下载完整代码:https://github.com/TiFG/zookeeper-samples
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相关文章推荐
- Oracle修改监听端口教程
- Android实战简易教程-第四枪(ScrollView和HorizontalScrollView动态添加控件并提供事件监听)
- Android初级教程_监听EditText输入文字的字数
- 【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合
- Zookeeper系列(十二)Zookeeper开源客户端之Curator的事件监听
- Zookeeper框架Curator之事件监听-yellowcong
- 迷你MVVM框架 avalonjs 学习教程15、属性监听与模块通信
- Android实战简易教程-第三十六枪(监听短信-实现短信验证码自动填入)
- 源码分析Vue.js的监听实现教程
- Zookeeper 客户端API调用示例(基本使用,增删改查znode数据,监听znode,其它案例,其它网络参考资料)
- Android初级教程_监听EditText输入文字的字数
- 【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合
- Zookeeper系列(十三)Zookeeper开源客户端之Curator的事件监听丢失分析
- curator监听zookeeper目录(TreeNode)
- 连接Oracle数据库时报ORA-12541:TNS:无监听程序的图文解决教程
- Android基础教程——横竖切换监听、禁止、总结
- Zookeeper 客户端API调用示例(基本使用,增删改查znode数据,监听znode,其它案例,其它网络参考资料)
- 从零开始学习Node.js系列教程五:服务器监听方法示例
- ZooKeeper客户端Curator(监听篇)
- Android开发教程:监听EditText的变化