use of org.apache.zookeeper.CreateMode in project disgear by yangbutao.
the class ZookeeperClient method makePath.
public void makePath(String path, byte[] data, CreateMode createMode) throws Exception {
boolean failOnExists = true;
boolean retry = true;
if (path.startsWith("/")) {
path = path.substring(1, path.length());
}
String[] paths = path.split("/");
StringBuilder sbPath = new StringBuilder();
for (int i = 0; i < paths.length; i++) {
byte[] bytes = null;
String pathPiece = paths[i];
sbPath.append("/" + pathPiece);
final String currentPath = sbPath.toString();
Object exists = zkClient.exists(currentPath, null);
if (exists == null || ((i == paths.length - 1) && failOnExists)) {
CreateMode mode = CreateMode.PERSISTENT;
if (i == paths.length - 1) {
mode = createMode;
bytes = data;
}
try {
zkClient.create(currentPath, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode);
} catch (NodeExistsException e) {
if (!failOnExists) {
// TODO:
zkClient.setData(currentPath, data, -1);
zkClient.exists(currentPath, null);
return;
}
if (i == paths.length - 1) {
throw e;
}
}
if (i == paths.length - 1) {
zkClient.exists(currentPath, null);
}
} else if (i == paths.length - 1) {
zkClient.setData(currentPath, data, -1);
zkClient.exists(currentPath, null);
}
}
}
Aggregations