http://curator.apache.org/curator-recipes/path-cache.html
Description
A Path Cache is used to watch a ZNode. Whenever a child is added, updated or removed, the Path Cache will change its state to contain the current set of children, the children's data and the children's state.
Participating Classes
- PathChildrenCache
- PathChildrenCacheEvent
- PathChildrenCacheListener
- ChildData
Usage
Creating a PathChildrenCache
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
General Usage
The cache must be started by calling start(). Call close() when you are through with the cache.
There are two versions of start(). The no-arg version gives default behavior. The other version takes an enumeration that allows you to control how the initial cache is warmed:
public enum StartMode
{
/**
* cache will _not_ be primed. i.e. it will start empty and you will receive
* events for all nodes added, etc.
*/
NORMAL,
/**
* rebuild() will be called before this method returns in
* order to get an initial view of the node.
*/
BUILD_INITIAL_CACHE,
/**
* After cache is primed with initial values (in the background) a
* PathChildrenCacheEvent.Type.INITIALIZED event will be posted
*/
POST_INITIALIZED_EVENT
}
At any time, call getCurrentData() to get the current state of the cache. You can also register to be notified when a change occurs by calling getListenable() and then:
public void addListener(PathChildrenCacheListener listener)
Add a change listener
Parameters:
listener - the listener
例子
监听子节点新建、修改、删除
* create -e /child-cache-node/one 1 (可以)
* create -e /child-cache-node/two 2 (可以)
* create -e /child-cache-node/three 3 (可以)
* set /child-cache-node/one 11 (可以)
* delete /child-cache-node/one (可以)
* create /child-cache-node/three/t3 33 (孙子节点监听不到)
/**
* A Path Cache is used to watch a ZNode. Whenever a child is added, updated or removed, the Path Cache will change its state to contain the current set of children, the children's data and the children's state
* 监听子节点的的新增、修改、删除
*
* @author kq
*
* example:
* create /child-cache-node (这个不会监听到,启动时候会自动创建)
* create -e /child-cache-node/one 1 (可以)
* create -e /child-cache-node/two 2 (可以)
* create -e /child-cache-node/three 3 (可以)
* set /child-cache-node/one 11 (可以)
*
* delete /child-cache-node/one (可以)
*
* create /child-cache-node/three/t3 33 (孙子节点监听不到)
*
* 注意:
* 启动的时候,自动会创建/child-cache-node,但如果这个时候把/child-cache-node节点删除了,这个时候调用create -e /child-cache-node/one 1 都是监听不到的
*
* @see http://curator.apache.org/curator-recipes/path-cache.html
*/
public class PathCacheDemo extends Config {
public static void main(String[] args) throws Exception {
// 1.Connect to zk
CuratorFramework client = CuratorFrameworkFactory.newClient(ZK_SERVERS, new RetryNTimes(10, 5000));
client.start();
// CuratorFramework client = getClient();
final PathChildrenCache cache = new PathChildrenCache(client, CHILD_CACHE_NODE, true);
cache.start();
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curator, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()) {
case CHILD_ADDED:
System.out.println("ChildNodeListenerDemo add:" + event.getData());
break;
case CHILD_UPDATED:
System.out.println("ChildNodeListenerDemo update:" + event.getData());
break;
case CHILD_REMOVED:
System.out.println("ChildNodeListenerDemo remove:" + event.getData());
break;
default:
System.out.println("ChildNodeListenerDemo default:" + event.getData());
break;
}
}
});
System.out.println("-------------------------------------");
Thread.sleep(Integer.MAX_VALUE);
}
}
注意
启动的时候,自动会创建/child-cache-node,但如果这个时候把/child-cache-node节点删除了,这个时候调用create -e /child-cache-node/one 1 都是监听不到的