码迷,mamicode.com
首页 > Web开发 > 详细

Apache Curator Tree Cache Watcher

时间:2015-02-27 17:04:35      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:zookeeper   curator   path   cache   watcher   

可以监控某一路径下的子结点(所有子结节,不管有多少层子结点)变化。
比NodeCache方便的是,可以监听一群结点,而不用一个节点一个节点的去设置监听
1.run TreeListener
2.run TLTest

package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;

public class TLTest {
    public static void main(String[] args) throws Exception {
        TLClientCreate.main(null);
        Thread.sleep(2000);
        TLClientUpdate.main(null);
        Thread.sleep(2000);

        TLClientCreateSub.main(null);
        Thread.sleep(2000);
        TLClientUpdateSub.main(null);
        Thread.sleep(2000);
        TLClientDeleteSub.main(null);

        Thread.sleep(2000);
        TLClientDelete.main(null);
    }
}
package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.EnsurePath;

import java.util.List;
import java.util.Map;

public class TreeListener {
    public static final String C_PATH = "/TestTree";
    public static final String CHARSET = "UTF-8";

    public static void main(String[] args) {
        try {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        String zookeeperConnectionString = "127.0.0.1:2181";
                        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                        CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                        client.start();

                        final TreeCache treeCache = new TreeCache(client, C_PATH);
                        treeCache.getListenable().addListener(new TreeCacheListener() {
                            @Override
                            public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
                                System.out.println("================== catch tree change ==================");
                                if(event.getData() == null){
                                    System.out.println("===init," + event.getType());
                                    return;
                                }

                                if(event.getData().getData() == null){
                                    System.out.println("===delete," + event.getType() + "," + event.getData().getPath());
                                }else{
                                    System.out.println("===update or add," + event.getType() + "," + event.getData().getPath() + "," + new String(event.getData().getData(), TreeListener.CHARSET));
                                }
                            }
                        });
                        treeCache.start();

                        Thread.sleep(Integer.MAX_VALUE);
                        client.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}




package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class TLClientCreate {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(TreeListener.C_PATH);
                    if (stat == null) {
                        client.create().withMode(CreateMode.PERSISTENT).forPath(TreeListener.C_PATH, "-1".getBytes(NodeListener.CHARSET));
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class TLClientCreateSub {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(TreeListener.C_PATH + "/sub1");
                    if (stat == null) {
                        client.create().withMode(CreateMode.PERSISTENT).forPath(TreeListener.C_PATH + "/sub1", "-1".getBytes(NodeListener.CHARSET));
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

public class TLClientDelete {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(TreeListener.C_PATH);
                    if (stat != null) {
                        client.delete().deletingChildrenIfNeeded().forPath(TreeListener.C_PATH);
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class TLClientDeleteSub {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(TreeListener.C_PATH + "/sub1");
                    if (stat != null) {
                        client.delete().forPath(TreeListener.C_PATH + "/sub1");
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class TLClientUpdate {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(TreeListener.C_PATH);
                    if (stat != null) {
                        client.setData().forPath(TreeListener.C_PATH, "64".getBytes(NodeListener.CHARSET));
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

public class TLClientUpdateSub {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(TreeListener.C_PATH + "/sub1");
                    if (stat != null) {
                        client.setData().forPath(TreeListener.C_PATH + "/sub1", "128".getBytes(TreeListener.CHARSET));
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
























Apache Curator Tree Cache Watcher

标签:zookeeper   curator   path   cache   watcher   

原文地址:http://blog.csdn.net/collonn/article/details/43969121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!