ZooKeeper(3.4.5) 使用Curator监听事件
来源:互联网 发布:淘宝双十一魔盒怎么用 编辑:程序博客网 时间:2024/06/07 11:44
ZooKeeper(3.4.5) 使用Curator监听事件
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(); }}
阅读全文
0 0
- ZooKeeper(3.4.5) 使用Curator监听事件
- Zookeeper框架Curator之事件监听-yellowcong
- zookeeper Curator 监听
- Curator典型使用场景之事件监听。
- Zookeeper开源客户端Curator之事件监听详解
- Zookeeper系列(十二)Zookeeper开源客户端之Curator的事件监听
- Zookeeper系列(十三)Zookeeper开源客户端之Curator的事件监听丢失分析
- 使用curator操作zookeeper
- Zookeeper学习(Curator使用)
- 使用Curator操作ZooKeeper
- [curator] Netflix Curator 使用 Zookeeper 编程
- ZooKeeper(5)Curator介绍
- ZooKeeper客户端Curator(监听篇)
- curator对Zookeeper节点监听总结
- curator监听zookeeper目录(TreeNode)
- Netflix Curator 使用 Zookeeper 编程
- Zookeeper客户端Curator使用详解
- zookeeper Curator框架简单使用
- Array Challenge
- < 笔记 > Python
- SVN简介
- 菲波那切数列解法
- hdu 3342 Legal or Not
- ZooKeeper(3.4.5) 使用Curator监听事件
- ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be def
- 近期学习总结
- 51nod-1105-第K大的数
- 集成框架spring integration体验
- pandas 多个列,行读取方法
- < 笔记 > Python
- 【二分+数学】bzoj1257: [CQOI2007]余数之和sum
- 【Android】基于Socket的即时聊天(群聊)