zookeeper 书上的代码 删除节点为什么不成功

plugin 发布于 2017/03/12 02:00
阅读 369
收藏 0
package com.zk.demo;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Created by xx on 2017/3/12.
 */
public class Exist_API_Sync_Uage implements Watcher {
    private static CountDownLatch samphare = new CountDownLatch(1);
    private static ZooKeeper zk;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        zk = new ZooKeeper("127.0.0.1:2181", 5000, new Exist_API_Sync_Uage());
        samphare.await();

        String path = "/zk-book10";
        zk.exists(path, true);
        zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        zk.setData(path, "123".getBytes(), -1);

        zk.create(path+"/cl", "sss".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.delete(path+"/c1",-1);
        zk.delete(path, -1);

        Thread.sleep(Integer.MAX_VALUE);
    }

    public void process(WatchedEvent watchedEvent) {
        try {
            if (Event.KeeperState.SyncConnected == watchedEvent.getState()) {
                if (Event.EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()) {
                    samphare.countDown();
                } else if (Event.EventType.NodeCreated == watchedEvent.getType()) {
                    System.out.println("node [" + watchedEvent.getPath() + "]created");
                    zk.exists(watchedEvent.getPath(), true);
                } else if (Event.EventType.NodeDeleted == watchedEvent.getType()) {
                    System.out.println("node [" + watchedEvent.getPath() + "]deleted");
                    zk.exists(watchedEvent.getPath(), true);
                } else if(Event.EventType.NodeDataChanged == watchedEvent.getType()) {
                    System.out.println("node [" + watchedEvent.getPath() + "]dataChanged");
                    zk.exists(watchedEvent.getPath(), true);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }


    }
}

为什么zk.delete(path+"/c1",-1)这行会报不存在节点错误

node [/zk-book10]created
node [/zk-book10]dataChanged
Exception in thread "main" org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /zk-book10/c1
	at org.apache.zookeeper.KeeperException.create(KeeperException.java:111)
	at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
	at org.apache.zookeeper.ZooKeeper.delete(ZooKeeper.java:873)
	at com.zk.demo.Exist_API_Sync_Uage.main(Exist_API_Sync_Uage.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

我用zkli查看,ls /zk-book10/ 下是有c1的。

加载中
0
excepiton
excepiton

基本没用过原生的api,一般用curatorframework或者ZkClient

返回顶部
顶部