Curator Cache
1.Curator Cache 與原生ZooKeeper Wacher區(qū)別
? ?原生的ZooKeeper Wacher是一次性的:一個(gè)Wacher一旦觸發(fā)就會(huì)被移出,如果你想要反復(fù)使用Wacher,就要在Wacher被移除后重新注冊,使用起來很麻煩。使用Curator Cache 可以反復(fù)使用Wacher了。
2.Curator Cache 和Curator Wacher區(qū)別
?
2.相關(guān)類
Curator Cache主要提供了一下三組類,分別用于實(shí)現(xiàn)對節(jié)點(diǎn)的監(jiān)聽,子節(jié)點(diǎn)的監(jiān)聽和二者的混合:
? 1.NodeCache,NodeCacheListener,ChildData,節(jié)點(diǎn)創(chuàng)建,節(jié)點(diǎn)數(shù)據(jù)內(nèi)容變更,不能監(jiān)聽節(jié)點(diǎn)刪除。
? 2.PathChildrenCache,PathChildrenCacheListener,PathChildrenCacheEvent監(jiān)聽指定節(jié)點(diǎn)的子節(jié)點(diǎn)的變更包括添加,刪除,子節(jié)點(diǎn)數(shù)據(jù)數(shù)據(jù)變更這三類。
? 3.TreeCache,TreeCacheListener,TreeCacheEvent,TreeCacheSelector
?
3.節(jié)點(diǎn)監(jiān)聽
package cn.francis.maven.hello.ZooKeeper;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.TreeCache; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; import org.apache.curator.utils.CloseableUtils; import org.apache.zookeeper.CreateMode;public class NodeCacheDemo {public static void main(String[]args) throws Exception{TestingServer server=null;CuratorFramework client=null;NodeCache nodeCache=null;String path="/francis/nodecache/a";try{ server=new TestingServer();client= CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000,3));client.start();path=client.create().creatingParentsIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL).forPath(path,"init".getBytes());nodeCache=new NodeCache(client,path,false);nodeCache.start();addListener(nodeCache);client.setData().forPath(path,"hello".getBytes());Thread.sleep(1000);client.delete().deletingChildrenIfNeeded().forPath(path);Thread.sleep(Integer.MAX_VALUE);}catch(Exception e){e.printStackTrace();}finally{//這里因?yàn)槭菧y試,沒有加他們。//CloseableUtils.closeQuietly(nodeCache);/// CloseableUtils.closeQuietly(client);// CloseableUtils.closeQuietly(server); } }public static void addListener(NodeCache nodeCache){//監(jiān)聽類型:節(jié)點(diǎn)是否存在,節(jié)點(diǎn)數(shù)據(jù)內(nèi)容改變,不監(jiān)聽節(jié)點(diǎn)刪除。nodeCache.getListenable().addListener(new NodeCacheListener(){@Overridepublic void nodeChanged() throws Exception {// TODO Auto-generated method stubif(nodeCache.getCurrentData()!=null)System.out.println("path:"+nodeCache.getCurrentData().getPath()+",data:"+new String(nodeCache.getCurrentData().getData()));}});} }
?
在上面的代碼中首先創(chuàng)建了一個(gè)節(jié)點(diǎn),然后創(chuàng)建用這個(gè)節(jié)點(diǎn)路徑來創(chuàng)建NodeCache,啟動(dòng)NodeCache,添加NodeCacheListener。然后調(diào)用setData來修改節(jié)點(diǎn)數(shù)據(jù)。上面的代碼輸入如下:
path:/francis/nodecache/_c_d5be73ca-592c-4eda-b7c4-c8ec60ef39a8-a,data:hello
?
子節(jié)點(diǎn)監(jiān)聽:
?PathChildrenCache的構(gòu)造函數(shù)如下:
public PathChildrenCache(CuratorFramework client,String path,boolean cacheData) Parameters: client - the client path - path to watch cacheData - if true, node contents are cached in addition to the stat
其中最主要的是cacheData,如果為true,那么當(dāng)對子節(jié)點(diǎn)調(diào)用setData時(shí),PathChildrenCache會(huì)受到這個(gè)CHILD_UPDATED事件。
下面看一下demo:
package cn.francis.maven.hello.ZooKeeper;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.PathChildrenCache.StartMode; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; import org.apache.curator.framework.recipes.cache.TreeCache; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; import org.apache.curator.utils.CloseableUtils; import org.apache.curator.utils.ZKPaths; import org.apache.hadoop.mapred.join.Parser.TType; import org.apache.zookeeper.CreateMode;public class NodeCacheDemo {public static void main(String[]args) throws Exception{TestingServer server=null;CuratorFramework client=null;NodeCache nodeCache=null;String path="/francis/nodecache/b";try{ server=new TestingServer();client= CuratorFrameworkFactory.newClient(server.getConnectString(),new ExponentialBackoffRetry(1000,3));client.start();//這里將第三個(gè)參數(shù)cacheData設(shè)置為true,這樣當(dāng)對子節(jié)點(diǎn)調(diào)用setData時(shí),會(huì)受到CHILDREN_UPDATE通知。PathChildrenCache childrenCache=new PathChildrenCache(client,path,true);
childrenCache.start(StartMode.POST_INITIALIZED_EVENT);childrenCache.getListenable().addListener(new PathChildrenCacheListener(){@Overridepublic void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {// TODO Auto-generated method stubif(event.getType()==Type.INITIALIZED){System.out.println("create"+event.getData().getPath()); }else if(event.getType()==Type.CHILD_ADDED){System.out.println("create"+event.getData().getPath()); }else if(event.getType()==Type.CHILD_REMOVED){System.out.println("remove:"+event.getData().getPath());}else if(event.getType()==Type.CHILD_UPDATED){//System.out.println("update:"+event.getData().getPath());System.out.println("update:"+new String(event.getData().getData()));}}});//創(chuàng)建父節(jié)點(diǎn)System.out.println(client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path,"init".getBytes()));Thread.sleep(1000);//創(chuàng)建子節(jié)點(diǎn)String childPath1=ZKPaths.makePath(path, "a");childPath1=client.create().withMode(CreateMode.PERSISTENT).forPath(childPath1,"1".getBytes());Thread.sleep(1000);//對子節(jié)點(diǎn)賦值client.setData().forPath(childPath1,"aaa".getBytes());Thread.sleep(1000);//刪除子節(jié)點(diǎn) client.delete().forPath(childPath1);client.delete().deletingChildrenIfNeeded().forPath("/francis");Thread.sleep(2000);}catch(Exception e){e.printStackTrace();}finally{ CloseableUtils.closeQuietly(nodeCache);CloseableUtils.closeQuietly(client);CloseableUtils.closeQuietly(server);} }
?
?
3.TreeNodeCache
? ?TreeNodeCache將NodeCache和PathChildrenCache功能結(jié)合到一起了。他不僅可以對子節(jié)點(diǎn)和父節(jié)點(diǎn)同時(shí)進(jìn)行監(jiān)聽。如下:
?
package cn.francis.maven.hello.ZooKeeper;import java.util.Map;import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.ChildData; 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.PathChildrenCache.StartMode; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; import org.apache.curator.framework.recipes.cache.TreeCache; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; import org.apache.curator.utils.CloseableUtils; import org.apache.curator.utils.ZKPaths; import org.apache.hadoop.mapred.join.Parser.TType; import org.apache.zookeeper.CreateMode;public class NodeCacheDemo {public static void main(String[]args) throws Exception{TestingServer server=null;CuratorFramework client=null;NodeCache nodeCache=null;String path="/francis/nodecache/b";try{ server=new TestingServer();client= CuratorFrameworkFactory.newClient(server.getConnectString(),new ExponentialBackoffRetry(1000,3));client.start();TreeCache treeNodeCache=new TreeCache(client,path);treeNodeCache.start();treeNodeCache.getListenable().addListener(new TreeCacheListener(){@Overridepublic void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {// TODO Auto-generated method stubswitch(event.getType()){case NODE_ADDED:System.out.println("added:"+event.getData().getPath());break;case NODE_UPDATED:System.out.println("updated:"+event.getData().getPath());break;case NODE_REMOVED: System.out.println("removed:"+event.getData().getPath());break;default:System.out.println("other:"+event.getType());}}});//創(chuàng)建父節(jié)點(diǎn)client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path,"init".getBytes());Thread.sleep(1000);//創(chuàng)建子節(jié)點(diǎn)String childPath1=ZKPaths.makePath(path, "a");childPath1=client.create().withMode(CreateMode.PERSISTENT).forPath(childPath1,"1".getBytes());Thread.sleep(1000);//對子節(jié)點(diǎn)賦值client.setData().forPath(childPath1,"aaa".getBytes());Thread.sleep(1000);//對子節(jié)點(diǎn)賦值client.setData().forPath(path,"aaa".getBytes());Thread.sleep(1000);//刪除子節(jié)點(diǎn) client.delete().forPath(childPath1);client.delete().deletingChildrenIfNeeded().forPath("/francis");Thread.sleep(2000);}catch(Exception e){e.printStackTrace();}finally{//這里因?yàn)槭菧y試,沒有加他們。 CloseableUtils.closeQuietly(nodeCache);CloseableUtils.closeQuietly(client);CloseableUtils.closeQuietly(server);} } }
?
輸出如下:
other:INITIALIZED
added:/francis/nodecache/b
added:/francis/nodecache/b/a
updated:/francis/nodecache/b/a
updated:/francis/nodecache/b
removed:/francis/nodecache/b/a
removed:/francis/nodecache/b
轉(zhuǎn)載于:https://www.cnblogs.com/francisYoung/p/5483777.html
總結(jié)
以上是生活随笔為你收集整理的Curator Cache的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C#公共帮助类】10年代码,最全的系统
- 下一篇: [BZOJ1602] [Usaco200