import org.apache.zookeeper.*;
import org.apache.zookeeper.Watcher.Event;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
public class DataWatcher implements Watcher {
private static final String ZOOKEEPER_ADDRESS = "127.0.0.1:2181";
private static final int SESSION_TIMEOUT = 3000;
private ZooKeeper zooKeeper;
public DataWatcher() throws IOException {
zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, this);
}
public void watchData(String path) {
try {
byte[] data = zooKeeper.getData(path, this, null);
System.out.println("Data: " + new String(data));
Stat stat = zooKeeper.exists(path, this);
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
System.out.println("Data changed");
watchData(watchedEvent.getPath());
}
}
public static void main(String[] args) throws IOException, InterruptedException {
DataWatcher dataWatcher = new DataWatcher();
dataWatcher.watchData("/myData");
Thread.sleep(Long.MAX_VALUE);
}
}
import org.apache.zookeeper.*;
import org.apache.zookeeper.Watcher.Event;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
public class EventWatcher implements Watcher {
private static final String ZOOKEEPER_ADDRESS = "127.0.0.1:2181";
private static final int SESSION_TIMEOUT = 3000;
private ZooKeeper zooKeeper;
public EventWatcher() throws IOException {
zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, this);
}
public void watchEvents() {
try {
zooKeeper.getChildren("/", this);
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("Event: " + watchedEvent.getType());
watchEvents();
}
public static void main(String[] args) throws IOException, InterruptedException {
EventWatcher eventWatcher = new EventWatcher();
eventWatcher.watchEvents();
Thread.sleep(Long.MAX_VALUE);
}
}
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.ZooDefs.Ids;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ACLExample {
private static final String ZOOKEEPER_ADDRESS = "127.0.0.1:2181";
private static final int SESSION_TIMEOUT = 3000;
private ZooKeeper zooKeeper;
public ACLExample() throws IOException {
zooKeeper = new ZooKeeper(ZOOKEEPER_ADDRESS, SESSION_TIMEOUT, null);
}
public void createNode(String path, byte[] data, List<ACL> aclList) {
try {
zooKeeper.create(path, data, aclList, CreateMode.PERSISTENT);
e.printStackTrace();
}
}
public List<ACL> createACL() {
List<ACL> aclList = new ArrayList<>();
Id id = new Id("digest", "username:password");
ACL acl = new ACL(ZooDefs.Perms.ALL, id);
aclList.add(acl);
return aclList;
}
public static void main(String[] args) throws IOException {
ACLExample aclExample = new ACLExample();
List<ACL> aclList = aclExample.createACL();
aclExample.createNode("/myNode", "data".getBytes(), aclList);
}
}