ZooKeeper(3.4.5) - 使用 Curator(2.7.0) 监听事件
ZooKeeper原生的API支持通过注册Watcher来进行事件监听,但是Watcher通知是一次性的,因此开发过程中需要反复注册Watcher,比较繁琐。Curator引入了Cache来监听ZooKeeper服务端的事件。Cache对ZooKeeper事件监听进行了封装,能够自动处理反复注册监听,简化了ZooKeeper原生API繁琐的开发过程。
简单的示例:
package com.huey.dream.demo; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
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 org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.apache.curator.retry.ExponentialBackoffRetry; /**
* Curator事件监听
* @author huey
* @version 1.0
* @created 2015-3-2
*/
public class CarutorDemo { public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.1.109:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(3000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
client.start(); client.create()
.creatingParentsIfNeeded()
.forPath("/zk-huey/cnode", "hello".getBytes()); /**
* 在注册监听器的时候,如果传入此参数,当事件触发时,逻辑由线程池处理
*/
ExecutorService pool = Executors.newFixedThreadPool(2); /**
* 监听数据节点的变化情况
*/
final NodeCache nodeCache = new NodeCache(client, "/zk-huey/cnode", false);
nodeCache.start(true);
nodeCache.getListenable().addListener(
new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("Node data is changed, new data: " +
new String(nodeCache.getCurrentData().getData()));
}
},
pool
); /**
* 监听子节点的变化情况
*/
final PathChildrenCache childrenCache = new PathChildrenCache(client, "/zk-huey", true);
childrenCache.start(StartMode.POST_INITIALIZED_EVENT);
childrenCache.getListenable().addListener(
new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event)
throws Exception {
switch (event.getType()) {
case CHILD_ADDED:
System.out.println("CHILD_ADDED: " + event.getData().getPath());
break;
case CHILD_REMOVED:
System.out.println("CHILD_REMOVED: " + event.getData().getPath());
break;
case CHILD_UPDATED:
System.out.println("CHILD_UPDATED: " + event.getData().getPath());
break;
default:
break;
}
}
},
pool
); client.setData().forPath("/zk-huey/cnode", "world".getBytes()); Thread.sleep(10 * 1000);
pool.shutdown();
client.close();
}
}
ZooKeeper(3.4.5) - 使用 Curator(2.7.0) 监听事件的更多相关文章
- zookeeper 监听事件 PathChildrenCacheListener
zookeeper 监听事件 PathChildrenCacheListener PathChildrenCacheListener一次父节点注册,监听每次子节点操作,不监听自身和查询. 1.测试类: ...
- zookeeper 监听事件 NodeCacheListener
zookeeper 监听事件 NodeCacheListener NodeCacheListener一次注册,每次监听,但是监听不到操作类型,不知道是增加?删除?还是修改? 1.测试类: packag ...
- zookeeper 监听事件 CuratorWatcher
zookeeper 监听事件 CuratorWatcher CuratorWatcher一次注册只监听一次,不监听查询. 1.监听测试类 package com.qy.learn.zk.curator ...
- ZooKeeper(3.4.5) 使用Curator监听事件
转载:http://www.mamicode.com/info-detail-494364.html 标签: ZooKeeper原生的API支持通过注册Watcher来进行事件监听,但是Watcher ...
- Zookeeper数据类型、节点类型、角色、watcher监听机制
1.Zookeeper数据类型:层次化目录结构+少量数据 Zookeeper包含层次化的目录结构,每个Znode都有唯一的路径标识,Znode可以包含数据和子节点. 其中Znode数据可以有多个版本, ...
- 使用zookeeper作为分布式锁以及设计一种通知监听模式
1.创建实例/** * 初始化单例的便捷方法 */ public static void init() { getInstance(); } /** * 获取单例 * @return */ publi ...
- ZooKeeper监听机制
前言:Zookeeper的监听机制很多人都踩过坑,感觉实现了watcher 接口,后面节点的变化都会一一推送过来,然而并非如此. Watch机制官方声明:一个Watch事件是一个一次性的触发器,当被设 ...
- Zookeeper 系列(五)Curator API
Zookeeper 系列(五)Curator API 一.Curator 使用 Curator 框架中使用链式编程风格,易读性更强,使用工程方法创建连接对象使用. (1) CuratorFramewo ...
- Zookeeper Curator 事件监听 - 秒懂
目录 写在前面 1.1. Curator 事件监听 1.1.1. Watcher 标准的事件处理器 1.1.2. NodeCache 节点缓存的监听 1.1.3. PathChildrenCache ...
随机推荐
- DNN学习
DNN(DotNetNuke)是一个免费.开源.可扩展的内容管理系统,可广泛用于商务网站.企业内网和外网网站.在线内容发布网站.DotNetNuke是微软第一次向开源说"Yes"的 ...
- Android权威编程指南读书笔记(1-2章)
第一章 Android应用初体验 1.4用户界面设计 <?xml version="1.0" encoding="utf-8"?> ADT21开发版 ...
- Install PhoneGap
To Install, ensure that you have NodeJS installed, then open your commandline and run the following: ...
- A Tour of Go Exercise: Maps
Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Tes ...
- iOS7滑动返回
[转载请注明出处] iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势.支持此手势的是UINavigationController中新增的属性 interactivePopGestu ...
- gmt学习资源
1 http://seisman.info/ http://examples.gmt-china.org/ http://docs.gmt-china.org http://modules.gmt-c ...
- Objective-C中的Block(闭包) (轉載)
来源: 伯乐在线 - 青玉伏案 链接:http://ios.jobbole.com/83229/ 学习OC有接触到一个新词Block(个人感觉又是一个牛气冲天的词),但不是新的概念,不是新的东西.学过 ...
- SQLite使用教程5 分离数据库
http://www.runoob.com/sqlite/sqlite-detach-database.html SQLite 分离数据库 SQLite的 DETACH DTABASE 语句是用来把命 ...
- iOS开发-控件设置
一.用户界面控件共有三种基本模式:动态.静态(非动态)和被动 动态:点击它们时会发生事情——通常是出发一段自己编写的时间代码. 被动:仅用于存储用户输入的值,以备后续使用.这些控件不会触发任何操作方法 ...
- Http协议网络对时工具
2009-09-17 11:52:31 专业的网络对时软件大多採用NTP协议来获取专业时间server时间对时,也有採用SNTP协议的,本来也想做个SNTP或NTP协议的对时工具自己用,尽管我手上也有 ...