I have written methods to retrieve data from zookeeper node without watcher. zookeeper.getData(nodePath, false, null);
The above method call will return the data in the node.
Now I want to fetch data from the node and later when an update occurs in the node, I want the updated data to be returned.
This is the source to interact with zookeeper
public class ZooKeeperOperations {
public enum ZooKeeperResult {
SUCCESS, NO_NODE_EXISTS, NODE_ALREADY_EXISTS, BAD_VERSION, CONNECTION_LOSS, NODE_NOT_EMPTY, FAILURE;
}
static ZooKeeperOperations instance = new ZooKeeperOperations();
private ZooKeeperOperations() {
}
public static ZooKeeperOperations getInstance() {
return instance;
}
private static ZooKeeper zk;
private static final String HOST = "localhost"; // No I18N
private static final String APPLICATION_GROUP_NAME = "AppName"; // No I18N
public ZooKeeper connect(String host) throws IOException, InterruptedException {
zk = new ZooKeeper(host, 5000,new Watcher() {
public void process(WatchedEvent we) {
}
});
return zk;
}
// Method to disconnect from zookeeper server
public void close() {
try {
zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static Stat znode_exists(String path) {
try {
return zk.exists(getAppPath(path), true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
private static String getAppPath(String path) {
return "/" + APPLICATION_GROUP_NAME + "/" + path;
}
public Object[] getNodeData(String path, boolean watch, Stat stat) {
try {
String nodePath = getAppPath(path);
byte[] propertyData = this.connect(HOST).getData(nodePath, watch, stat);
TBLogger.logMessage(Level.SEVERE, TBoxIAMUtil.getUserAPI().getCurrentUserZuid(), CLASSNAME, "getNodeData",
new Object[] { new String(propertyData) }, null);
return new Object[] { ZooKeeperResult.SUCCESS, propertyData };
} catch (ConnectionLossException e) {
return new Object[] { ZooKeeperResult.CONNECTION_LOSS };
} catch (NoNodeException e) {
return new Object[] { ZooKeeperResult.NO_NODE_EXISTS };
} catch (Exception e) {
return new Object[] { ZooKeeperResult.FAILURE };
} finally {
this.close();
}
}
}
Can anyone help me about this, I am totally new to zookeeper :(