Search in sources :

Example 1 with PersistentNodeListener

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();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PersistentNodeListener(org.apache.curator.framework.recipes.nodes.PersistentNodeListener) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) PersistentNode(org.apache.curator.framework.recipes.nodes.PersistentNode) RetryPolicy(org.apache.curator.RetryPolicy)

Aggregations

RetryPolicy (org.apache.curator.RetryPolicy)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 ChildData (org.apache.curator.framework.recipes.cache.ChildData)1 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)1 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)1 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)1 PersistentNode (org.apache.curator.framework.recipes.nodes.PersistentNode)1 PersistentNodeListener (org.apache.curator.framework.recipes.nodes.PersistentNodeListener)1 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)1