use of org.apache.curator.framework.recipes.nodes.PersistentNodeListener in project BRFS by zhangnianli.
the class TestZKNode method main.
public static void main(String[] args) throws Exception {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(zk_address, 5 * 1000, 30 * 1000, retryPolicy);
client.start();
client.blockUntilConnected();
PathChildrenCache cache = new PathChildrenCache(client.usingNamespace("test"), "/fileCoordinator/big", true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
System.out.println("---" + event);
ChildData data = event.getData();
if (data != null) {
switch(event.getType()) {
case CHILD_ADDED:
System.out.println("###PATH-" + data.getPath());
break;
default:
break;
}
}
}
});
cache.start();
PersistentNode node = new PersistentNode(client.usingNamespace("test"), CreateMode.EPHEMERAL, false, "/fileCoordinator/temp-1", "node1".getBytes());
node.getListenable().addListener(new PersistentNodeListener() {
@Override
public void nodeCreated(String path) throws Exception {
System.out.println("node1--created:" + path);
}
});
node.start();
PersistentNode node2 = new PersistentNode(client.usingNamespace("test"), CreateMode.EPHEMERAL, false, "/fileCoordinator/temp-1", "node2".getBytes());
node2.getListenable().addListener(new PersistentNodeListener() {
@Override
public void nodeCreated(String path) throws Exception {
System.out.println("node2--created:" + path);
}
});
node2.start();
Thread.sleep(2000);
node2.close();
synchronized (node) {
node.wait();
}
}
Aggregations