ZooKeeper学习笔记-5---ZooKeeper编程-2

来源:互联网 发布:下载短信软件 编辑:程序博客网 时间:2024/04/29 04:04

1.同步更新节点数据内容

public class ZKTest implements Watcher {    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);    private static ZooKeeper zooKeeper = null;    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {        String path = "/zk-book";        zooKeeper = new ZooKeeper("127.0.0.1:2181",5000,new ZKTest());        connectedSemaphore.await();             zooKeeper.create(path, "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);        Stat stat = zooKeeper.setData(path, "456".getBytes(), -1);        System.out.println(stat.getCzxid() + ", " + stat.getMzxid() + ", " + stat.getVersion());            Stat stat2 = zooKeeper.setData(path, "456".getBytes(), stat.getVersion());        System.out.println(stat2.getCzxid() + ", " + stat2.getMzxid() + ", " + stat2.getVersion());        try {            zooKeeper.setData(path, "456".getBytes(), stat.getVersion());        } catch (KeeperException e) {            System.out.println("Error: " + e.code() + ", " + e.getMessage());        }        Thread.sleep(Integer.MAX_VALUE);    }    @Override    public void process(WatchedEvent event) {        if(KeeperState.SyncConnected == event.getState()){            if (EventType.None == event.getType() && null == event.getPath()) {                connectedSemaphore.countDown();            }        }    }}

输出结果:

246, 247, 1246, 248, 2Error: BADVERSION, KeeperErrorCode = BadVersion for /zk-book

2.异步更新节点数据内容

public class ZKTest implements Watcher {    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);    private static ZooKeeper zooKeeper = null;    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {        String path = "/zk-book";        zooKeeper = new ZooKeeper("127.0.0.1:2181",5000,new ZKTest());        connectedSemaphore.await();         zooKeeper.create(path, "124".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);        zooKeeper.setData(path, "456".getBytes(), -1, new IStatCallBack(), null);        Thread.sleep(Integer.MAX_VALUE);    }    @Override    public void process(WatchedEvent event) {        if(KeeperState.SyncConnected == event.getState()){            if (EventType.None == event.getType() && null == event.getPath()) {                connectedSemaphore.countDown();            }        }    }}class IStatCallBack implements AsyncCallback.StatCallback{    @Override    public void processResult(int rc, String path, Object ctx, Stat stat) {        if (rc == 0) {            System.out.println("SUCCESS");        }    }}

输出结果:

SUCCESS

3.检测节点是否存在

public class ZKTest implements Watcher {    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);    private static ZooKeeper zooKeeper = null;    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {        String path = "/zk-book";        zooKeeper = new ZooKeeper("127.0.0.1:2181",5000,new ZKTest());        connectedSemaphore.await();         zooKeeper.exists(path, true);           zooKeeper.create(path, "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);        zooKeeper.setData(path, "123".getBytes(), -1);        zooKeeper.create(path+"/c1", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);        zooKeeper.delete(path+"/c1", -1);        zooKeeper.delete(path, -1);        Thread.sleep(Integer.MAX_VALUE);    }    @Override    public void process(WatchedEvent event) {        try {            if(KeeperState.SyncConnected == event.getState()){                if (EventType.None == event.getType() && null == event.getPath()) {                    connectedSemaphore.countDown();                }else if (EventType.NodeCreated == event.getType()) {                    System.out.println("Node(" + event.getPath() + ")Created");                    zooKeeper.exists(event.getPath(), true);                }else if (EventType.NodeDeleted == event.getType()) {                    System.out.println("Node(" + event.getPath() + ")Deleted");                    zooKeeper.exists(event.getPath(), true);                }else if (EventType.NodeDataChanged == event.getType()) {                    System.out.println("Node(" + event.getPath() + ")DataChanged");                    zooKeeper.exists(event.getPath(), true);                }            }        } catch (Exception e) {}    }}

输出结果:

Node(/zk-book)CreatedNode(/zk-book)DataChangedNode(/zk-book)Deleted
1 0
原创粉丝点击