ZooKeeper(3.4.5) - 使用 Curator(2.7.0) 监听事件
ZooKeeper原生的API支持通过注册Watcher来进行事件监听,但是Watcher通知是一次性的,因此开发过程中需要反复注册Watcher,比较繁琐。Curator引入了Cache来监听ZooKeeper服务端的事件。Cache对ZooKeeper事件监听进行了封装,能够自动处理反复注册监听,简化了ZooKeeper原生API繁琐的开发过程。
简单的示例:
1 package com.huey.dream.demo; 2 3 import java.util.concurrent.ExecutorService; 4 import java.util.concurrent.Executors; 5 6 import org.apache.curator.framework.CuratorFramework; 7 import org.apache.curator.framework.CuratorFrameworkFactory; 8 import org.apache.curator.framework.recipes.cache.NodeCache; 9 import org.apache.curator.framework.recipes.cache.NodeCacheListener; 10 import org.apache.curator.framework.recipes.cache.PathChildrenCache; 11 import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; 12 import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; 13 import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode; 14 import org.apache.curator.retry.ExponentialBackoffRetry; 15 16 /** 17 * Curator事件监听 18 * @author huey 19 * @version 1.0 20 * @created 2015-3-2 21 */ 22 public class CarutorDemo { 23 24 public static void main(String[] args) throws Exception { 25 CuratorFramework client = CuratorFrameworkFactory.builder() 26 .connectString("192.168.1.109:2181") 27 .sessionTimeoutMs(5000) 28 .connectionTimeoutMs(3000) 29 .retryPolicy(new ExponentialBackoffRetry(1000, 3)) 30 .build(); 31 client.start(); 32 33 client.create() 34 .creatingParentsIfNeeded() 35 .forPath("/zk-huey/cnode", "hello".getBytes()); 36 37 /** 38 * 在注册监听器的时候,如果传入此参数,当事件触发时,逻辑由线程池处理 39 */ 40 ExecutorService pool = Executors.newFixedThreadPool(2); 41 42 /** 43 * 监听数据节点的变化情况 44 */ 45 final NodeCache nodeCache = new NodeCache(client, "/zk-huey/cnode", false); 46 nodeCache.start(true); 47 nodeCache.getListenable().addListener( 48 new NodeCacheListener() { 49 @Override 50 public void nodeChanged() throws Exception { 51 System.out.println("Node data is changed, new data: " + 52 new String(nodeCache.getCurrentData().getData())); 53 } 54 }, 55 pool 56 ); 57 58 /** 59 * 监听子节点的变化情况 60 */ 61 final PathChildrenCache childrenCache = new PathChildrenCache(client, "/zk-huey", true); 62 childrenCache.start(StartMode.POST_INITIALIZED_EVENT); 63 childrenCache.getListenable().addListener( 64 new PathChildrenCacheListener() { 65 @Override 66 public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) 67 throws Exception { 68 switch (event.getType()) { 69 case CHILD_ADDED: 70 System.out.println("CHILD_ADDED: " + event.getData().getPath()); 71 break; 72 case CHILD_REMOVED: 73 System.out.println("CHILD_REMOVED: " + event.getData().getPath()); 74 break; 75 case CHILD_UPDATED: 76 System.out.println("CHILD_UPDATED: " + event.getData().getPath()); 77 break; 78 default: 79 break; 80 } 81 } 82 }, 83 pool 84 ); 85 86 client.setData().forPath("/zk-huey/cnode", "world".getBytes()); 87 88 Thread.sleep(10 * 1000); 89 pool.shutdown(); 90 client.close(); 91 } 92 }
作者:明翼(XGogo)
-------------
公众号:TSparks
微信:shinelife
扫描关注我的微信公众号感谢
-------------