1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| package com.bai.javaapi;
import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat;
import java.io.IOException; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit;
public class ApiOperatorDemo implements Watcher { private final static String CONNECTSTRING = "123.57.3.144:2184"; private static CountDownLatch countDownLatch = new CountDownLatch(1); private static ZooKeeper zooKeeper; private static Stat stat = new Stat();
public static void main(String[] args) throws IOException, InterruptedException, KeeperException { zooKeeper = new ZooKeeper(CONNECTSTRING, 5000, new ApiOperatorDemo()); countDownLatch.await(); System.out.println(zooKeeper.getState()); String result = zooKeeper.create("/test", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); zooKeeper.getData("/test", new ApiOperatorDemo(), stat); System.out.println("创建结果:" + result);
TimeUnit.SECONDS.sleep(2);
zooKeeper.setData("/test", "456".getBytes(), -1); TimeUnit.SECONDS.sleep(2);
zooKeeper.setData("/test", "789".getBytes(), -1); TimeUnit.SECONDS.sleep(2);
zooKeeper.delete("/test", -1); TimeUnit.SECONDS.sleep(2);
String re = ""; String fatherNodePath = "/testChild"; String sonNodePath="/test";
stat = zooKeeper.exists(fatherNodePath+sonNodePath, true); System.out.println(stat); if (stat != null) { zooKeeper.delete(fatherNodePath + sonNodePath, -1); zooKeeper.delete(fatherNodePath, -1); } re = zooKeeper.create(fatherNodePath, "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("创建/testChild节点成功---" + re); TimeUnit.SECONDS.sleep(1); re = zooKeeper.create(fatherNodePath + "/test", "456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("创建/testChild/test节点成功---" + re); TimeUnit.SECONDS.sleep(1);
zooKeeper.setData(fatherNodePath + sonNodePath, "789".getBytes(), -1); TimeUnit.SECONDS.sleep(1);
List<String> children=zooKeeper.getChildren(fatherNodePath+sonNodePath,true); System.out.println(children); }
public void process(WatchedEvent watchedEvent) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { if (Event.EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()) { countDownLatch.countDown(); System.out.println(watchedEvent.getState() + "--->" + watchedEvent.getType()); } else { switch (watchedEvent.getType()) { case None: break; case NodeCreated: try { System.out.println("NodeCreated---路径:" + watchedEvent.getPath() + "---值:" + zooKeeper.getData(watchedEvent.getPath(), true, stat)); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } break; case NodeDeleted: System.out.println("NodeDeleted---路径:" + watchedEvent.getPath()); break; case NodeDataChanged: try { System.out.println("NodeDataChanged---路径:" + watchedEvent.getPath() + "---值:" + zooKeeper.getData(watchedEvent.getPath(), true, stat)); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } break; case NodeChildrenChanged: try { System.out.println("NodeChildrenChanged---路径:" + watchedEvent.getPath() + "---值:" + zooKeeper.getData(watchedEvent.getPath(), true, stat)); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } break; } } } } }
|