package com.zookeeper; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import java.io.IOException; import java.util.List; public class GetChildSync { private static ZooKeeper zooKeeper; public static void main(String[] args) { try { zooKeeper = new ZooKeeper(“192.168.40.135:2181”, 5000, new Watcher() { public void process(WatchedEvent watchedEvent) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { doSomeThing(); } } }); System.out.println(“state:{}” + zooKeeper.getState()); Thread.sleep(Integer.MAX_VALUE); } catch (IOException e) { System.out.println(“connect zk failed”); } catch (InterruptedException e) { e.printStackTrace(); } } private static void doSomeThing() { List children = null; try { //false表示不需要需要关注子节点的变化 children = zooKeeper.getChildren(“/”, false); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(“children:” + children); } }
运行结果:
如果需要关注子节点的变化:
package com.zookeeper; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import java.io.IOException; import java.util.List; public class GetChildSync { private static ZooKeeper zooKeeper; public static void main(String[] args) { try { zooKeeper = new ZooKeeper(“192.168.40.135:2181”, 5000, new Watcher() { public void process(WatchedEvent watchedEvent) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { if (watchedEvent.getType() == Event.EventType.None && null == watchedEvent.getPath()) { doSomeThing(); } else { if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) { try { System.out.println(zooKeeper.getChildren(watchedEvent.getPath(), true)); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }); System.out.println(“state:{}” + zooKeeper.getState()); Thread.sleep(Integer.MAX_VALUE); } catch (IOException e) { System.out.println(“connect zk failed”); } catch (InterruptedException e) { e.printStackTrace(); } } private static void doSomeThing() { List children = null; try { //false表示不需要需要关注子节点的变化 children = zooKeeper.getChildren(“/”, true); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(“children:” + children); } }
运行结果:
通过服务端给根节点添加一个子节点,就会打印出来:
异步获取:
package com.zookeeper; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import java.io.IOException; import java.util.List; public class GetChildASync { private static ZooKeeper zooKeeper; public static void main(String[] args) { try { zooKeeper = new ZooKeeper(“192.168.40.135:2181”, 5000, new Watcher() { public void process(WatchedEvent watchedEvent) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { if (watchedEvent.getType() == Event.EventType.None && null == watchedEvent.getPath()) { doSomeThing(); } else { if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) { zooKeeper.getChildren(watchedEvent.getPath(), true, new ChildrenClallBack(), “异步获取”); } } } } }); System.out.println(“state:{}” + zooKeeper.getState()); Thread.sleep(Integer.MAX_VALUE); } catch (IOException e) { System.out.println(“connect zk failed”); } catch (InterruptedException e) { e.printStackTrace(); } } static class ChildrenClallBack implements AsyncCallback.Children2Callback { /** @param rc 返回码,如果异步创建成功,返回0 @param path 创建节点的完整路径 @param ctx 异步调用的上下文,。。也就是上面传下来的“异步获取”字符串 @param children @param stat */ public void processResult(int rc, String path, Object ctx, List children, Stat stat) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(“rc=” + rc).append(“\n”); stringBuilder.append(“path=” + path).append(“\n”); stringBuilder.append(“ctx=” + ctx).append(“\n”); stringBuilder.append(“children=” + children).append(“\n”); stringBuilder.append(“stat=” + stat).append(“\n”); System.out.println(stringBuilder.toString()); } } private static void doSomeThing() { //false表示不需要需要关注子节点的变化 zooKeeper.getChildren(“/”, true, new ChildrenClallBack(), “异步获取”); } }
运行结果:
同步获取节点数据:
package com.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
public class GetDataSync {
private static ZooKeeper zooKeeper;
private static Stat stat = new Stat();
public static void main(String[] args) {
try {
zooKeeper = new ZooKeeper(“192.168.40.135:2181”, 5000, new Watcher() {
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
if (watchedEvent.getType() == Event.EventType.None && null == watchedEvent.getPath()) {
doSomeThing();
} else {
if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
try {
System.out.println(new String(zooKeeper.getData(watchedEvent.getPath(), true, stat)));
}文章来源地址https://www.toymoban.com/news/detail-855674.html
catch (KeeperException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
});
System.out.println(“state:{}” + zooKeeper.getState());
Thread.sleep(Integer.MAX_VALUE);
}
catch (IOException e) {
System.out.println(“connect zk failed”);文章来源:https://www.toymoban.com/news/detail-855674.html
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void doSomeThing() {
byte[] data = null;
try {
//false表示不需要需要关注子节点的变化
data = zooKeeper.getData(“/node_5”, true, stat);
}
catch (KeeperException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(“data:” + new String(data));
System.out.println(“stat:” + stat);
}
}
运行结果:
异步获取节点数据:
package com.zookeeper;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
public class GetDataASync {
private static ZooKeeper zooKeeper;
public static void main(String[] args) {
try {
zooKeeper = new ZooKeeper(“192.168.40.135:2181”, 5000, new Watcher() {
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
if (watchedEvent.getType() == Event.EventType.None && null == watchedEvent.getPath()) {
doSomeThing();
} else {
if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
zooKeeper.getData(watchedEvent.getPath(), true, new DataClallBack(), “异步获取节点数据”);
}
}
}
}
});
Thread.sleep(Integer.MAX_VALUE);
}
catch (IOException e) {
System.out.println(“connect zk failed”);
}
到了这里,关于ZooKeeper Java API 使用教程 - 同步与异步操作示例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!