标签:ati 无法 子节点 path create value out start ddl
通过NodeCache 来监控节点值的变化。
cc.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/ephe_nd");
NodeCache nodeCache = new NodeCache(cc,"/ephe_nd",false);
nodeCache.start(true);
nodeCache.getListenable().addListener(()->{
//listener:ChildData{path='/ephe_nd', stat=122,123,1573801350026,1573801350095,1,0,0,72703122504417311,3,0,122, data=[118, 97, 108]}
System.out.println("listener:"+nodeCache.getCurrentData());
});
cc.setData().forPath("/ephe_nd","val".getBytes());
Thread.sleep(Integer.MAX_VALUE);
还有另外一个类可以监控子节点的变化,就是PathChildrenCache
cc.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/p1/p2/ephe_nd");
PathChildrenCache pathChildrenCache = new PathChildrenCache(cc,"/p1",false);
pathChildrenCache.start();
pathChildrenCache.getListenable().addListener((client,event)->{
//listener:PathChildrenCacheEvent{type=CHILD_ADDED, data=ChildData{path='/p1/p2', stat=128,155,1573802869312,1573803119532,5,11,0,0,2,1,153
, data=null}}
System.out.println("listener:"+event);
});
cc.setData().forPath("/p1/p2/ephe_nd","t1".getBytes());
cc.setData().forPath("/p1/p2","t2".getBytes());
Thread.sleep(Integer.MAX_VALUE);
上面代码可以发现,监控的是p1节点,但是只有p2节点更改才会收到通知,ephe_nd节点变化是没有通知的。所以,只能监控子节点,再深就无法监控了。
标签:ati 无法 子节点 path create value out start ddl
原文地址:https://www.cnblogs.com/june777/p/11866059.html