Apache Curator Node Cache Watcher

來源:互聯網
上載者:User

標籤:zookeeper   curator   node   cache   watcher   

只能監聽某一路徑本身的add,delete,update

1.run NodeListener

2.run NLTest


package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;public class NLTest {    public static void main(String[] args) throws Exception {        NLClientCreate.main(null);        Thread.sleep(1000 * 2);        NLClientUpdate.main(null);        Thread.sleep(1000 * 2);        NLClientDelete.main(null);    }}


package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;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;public class NodeListener {    public static final String C_PATH = "/TestNode";    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 NodeCache nodeCache = new NodeCache(client, C_PATH);                        nodeCache.getListenable().addListener(new NodeCacheListener() {                            @Override                            public void nodeChanged() throws Exception {                                System.out.println("================== catch node data change ==================");                                ChildData childData = nodeCache.getCurrentData();                                if(childData == null){                                    System.out.println("===delete, path=" + C_PATH + ", childData=" + childData);                                }else{                                    System.out.println("===update or add, path=" + C_PATH + ", childData=" + new String(childData.getData(), CHARSET));                                }                            }                        });                        nodeCache.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.NodeCache;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;import java.util.Random;public class NLClientCreate {    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(NodeListener.C_PATH);                    if (stat == null) {                        client.create().withMode(CreateMode.PERSISTENT).forPath(NodeListener.C_PATH, "-1".getBytes(NodeListener.CHARSET));                    }                    client.close();                } catch (Exception e) {                    e.printStackTrace();                }            }        }).start();    }}


package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;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;import java.util.Random;public class NLClientUpdate {    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(NodeListener.C_PATH);                    if (stat != null) {                        client.setData().forPath(NodeListener.C_PATH, "128".getBytes(NodeListener.CHARSET));                    }                    client.close();                } catch (Exception e) {                    e.printStackTrace();                }            }        }).start();    }}


package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;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;import java.util.Random;public class NLClientDelete {    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(NodeListener.C_PATH);                    if (stat != null) {                        client.delete().forPath(NodeListener.C_PATH);                    }                    client.close();                } catch (Exception e) {                    e.printStackTrace();                }            }        }).start();    }}











Apache Curator Node Cache Watcher

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.