Пример #1
0
 public static boolean createIfNotExists(
     ZooKeeperConnection zooKeeper, String path, byte[] value, CreateMode createMode)
     throws KeeperException, InterruptedException {
   if (zooKeeper.exists(path, false) == null) {
     try {
       zooKeeper.create(path, value, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
     } catch (KeeperException e) {
       if (e.code() != KeeperException.Code.NODEEXISTS) throw e;
       return false;
     }
     return true;
   }
   return false;
 }
Пример #2
0
 public static boolean createFullPath(
     ZooKeeperConnection zooKeeperConnection,
     String path,
     byte[] value,
     CreateMode createMode,
     boolean ignoreIfExists)
     throws KeeperException, InterruptedException {
   final byte[] empty = new byte[0];
   final String[] nodes = path.split("/");
   final StringBuilder pathBuilder = new StringBuilder();
   for (int i = 1; i < nodes.length - 1; i++) {
     pathBuilder.append("/").append(nodes[i]);
     createIfNotExists(zooKeeperConnection, pathBuilder.toString(), empty, CreateMode.PERSISTENT);
   }
   pathBuilder.append("/").append(nodes[nodes.length - 1]);
   if (ignoreIfExists) {
     return createIfNotExists(zooKeeperConnection, path, value, createMode);
   }
   zooKeeperConnection.create(
       pathBuilder.toString(), value, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
   return true;
 }