import org.apache.zookeeper.*;
public class ConfigWatcher implements Watcher {
private ZooKeeper zooKeeper;
private String configPath;
public ConfigWatcher(String hosts, String configPath) throws Exception {
this.configPath = configPath;
zooKeeper = new ZooKeeper(hosts, 2000, this);
}
public void displayConfig() throws Exception {
byte[] data = zooKeeper.getData(configPath, this, null);
String configValue = new String(data);
System.out.println("Current configuration value: " + configValue);
}
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeDataChanged) {
try {
displayConfig();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
String hosts = "localhost:2181";
String configPath = "/myapp/config";
ConfigWatcher configWatcher = new ConfigWatcher(hosts, configPath);
configWatcher.displayConfig();
Thread.sleep(Long.MAX_VALUE);
}
}