public void createPersistent(String path, String data) { try { client.create(path, data, CreateMode.PERSISTENT); } catch (ZkNodeExistsException e) { } }
/** * Create an ephemeral node. * * @param path * @param data * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createEphemeral(final String path, final Object data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { create(path, data, CreateMode.EPHEMERAL); }
/** * Create an ephemeral node. * * @param path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createEphemeral(final String path) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { create(path, null, CreateMode.EPHEMERAL); }
/** * Create an ephemeral node and set its ACL. * * @param path * @param acl * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createEphemeral(final String path, final List<ACL> acl) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { create(path, null, acl, CreateMode.EPHEMERAL); }
/** * Create an ephemeral node. * * @param path * @param data * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createEphemeral(final String path, final Object data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { create(path, data, CreateMode.EPHEMERAL); }
/** * Create a persistent node. * * @param path * @param data * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createPersistent(String path, Object data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { create(path, data, CreateMode.PERSISTENT); }
/** * Create a persistent node. * * @param path * @param data * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createPersistent(String path, Object data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { create(path, data, CreateMode.PERSISTENT); }
/** * Create a persistent, sequental node. * * @param path * @param data * @return create node's path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public String createPersistentSequential(String path, Object data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { return create(path, data, CreateMode.PERSISTENT_SEQUENTIAL); }
/** * Create an ephemeral node. * * @param path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createEphemeral(final String path) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { create(path, null, CreateMode.EPHEMERAL); }
/** * Create a persistent node. * * @param path * @param data * @param acl * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createPersistent(String path, Object data, List<ACL> acl) { create(path, data, acl, CreateMode.PERSISTENT); }
/** * Create an ephemeral node. * * @param path * @param data * @param acl * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public void createEphemeral(final String path, final Object data, final List<ACL> acl) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { create(path, data, acl, CreateMode.EPHEMERAL); }
/** * Create a persistent, sequental node. * * @param path * @param data * @return create node's path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public String createPersistentSequential(String path, Object data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { return create(path, data, CreateMode.PERSISTENT_SEQUENTIAL); }
/** * Create a persistent, sequential node and set its ACL. * * @param path * @param acl * @param data * @return create node's path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public String createPersistentSequential(String path, Object data, List<ACL> acl) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { return create(path, data, acl, CreateMode.PERSISTENT_SEQUENTIAL); }
/** * Create an ephemeral, sequential node with ACL. * * @param path * @param data * @param acl * @return created path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public String createEphemeralSequential(final String path, final Object data, final List<ACL> acl) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { return create(path, data, acl, CreateMode.EPHEMERAL_SEQUENTIAL); }
/** * Create an ephemeral, sequential node. * * @param path * @param data * @return created path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public String createEphemeralSequential(final String path, final Object data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { return create(path, data, CreateMode.EPHEMERAL_SEQUENTIAL); }
/** * Create an ephemeral, sequential node. * * @param path * @param data * @return created path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public String createEphemeralSequential(final String path, final Object data) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { return create(path, data, CreateMode.EPHEMERAL_SEQUENTIAL); }
/** * Create a node. * * @param path * @param data * @param mode * @return create node's path * @throws ZkInterruptedException * if operation was interrupted, or a required reconnection got interrupted * @throws IllegalArgumentException * if called from anything except the ZooKeeper event thread * @throws ZkException * if any ZooKeeper exception occurred * @throws RuntimeException * if any other exception occurs */ public String create(final String path, Object data, final CreateMode mode) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException { return create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode); }
@Override public Zk create(String path, byte[] data, List<ACL> acls, CreateMode createMode, Handler<AsyncResult<Void>> handler) { workerPool().executeBlocking( future -> { try { zookeeper.create(path, data == null ? new byte[0] : data, acls, createMode); future.complete(); } catch (Throwable t) { future.fail(t); } }, handler); return this; }
public static void register() { try { ZookeeperInfo zookeeperInfo = Global.getInstance().getZookeeperInfo(); zc = new ZkClient(zookeeperInfo.getAddr(), zookeeperInfo.getSessionTimeOut(), zookeeperInfo.getConnectionTimeOut()); StringBuffer stringBuffer = new StringBuffer("/krpc"); if (!zc.exists(stringBuffer.toString())) { zc.create(stringBuffer.toString(), "", CreateMode.PERSISTENT); log.info("创建根节点krpc"); } stringBuffer.append("/").append(Global.getInstance().getServiceName()); if (!zc.exists(stringBuffer.toString())) { zc.create(stringBuffer.toString(), "", CreateMode.PERSISTENT); log.info("创建{}服务节点", Global.getInstance().getServiceName()); } stringBuffer.append("/").append(Global.getInstance().getIp()).append(":").append(Global.getInstance().getPort()); zc.create(stringBuffer.toString(), Global.getInstance().getServiceName(), CreateMode.EPHEMERAL); } catch (Exception e) { log.error("register error!", e); } }
public static void main(String[] args) throws InterruptedException { String zkServers = "localhost:2181"; ZkClient zkClient = new ZkClient(zkServers, 10000, 10000); System.out.println("connected ok"); String nodePath = "/testData"; zkClient.subscribeDataChanges(nodePath, new ZKDataListener()); if (zkClient.exists(nodePath)) { zkClient.delete(nodePath); } zkClient.create(nodePath, "hello", CreateMode.EPHEMERAL); zkClient.writeData(nodePath, "world"); zkClient.delete(nodePath); Thread.sleep(3000); } }