/** * Create a client instancclientAppPathExiste * @param hosts host strings: zk01:2181,zk02:2181,zk03:2181 * @param namespace path root, such as app name */ public static ZkClient newClient(String hosts, String namespace){ return newClient(hosts, namespace, DEFAULT_RETRY_STRATEGY); }
/** * Create an ephemeral path * @param path path * @return the path created */ public String createEphemeral(String path) { return createEphemeral(path, (byte[]) null); }
@Override protected void doShutdown() { if (watcher != null){ watcher.stop(); } } }
/** * new a watcher of path child * @param path the parent path * @param listener a listener * NOTE: * Only watch first level children, not recursive */ public ChildWatcher newChildWatcher(String path, ChildListener listener) { return newChildWatcher(path, listener, Boolean.TRUE); }
/** * Acquire the leadership * @param leaderPath the leader node path * @param listener the leader listener * @return the leadership */ public Leader acquireLeader(String leaderPath, LeaderListener listener){ return acquireLeader(null, leaderPath, listener); }
@Override public void destroy() throws Exception { client.shutdown(); } }
/** * Create a client instance * @param hosts host strings: zk01:2181,zk02:2181,zk03:2181 * @param namespace path root, such as app name * @param retryStrategy client retry strategy */ public static ZkClient newClient(String hosts, String namespace, ExponentialBackoffRetry retryStrategy){ ZkClient zc = new ZkClient(hosts, namespace, retryStrategy); zc.start(); return zc; }
/** * Create an persistent path * @param path path * @return the path created */ public String create(String path) { return create(path, (byte[])null); }
/** * new a node watcher * @param nodePath the node path * @return the node watcher */ public NodeWatcher newNodeWatcher(String nodePath){ return newNodeWatcher(nodePath, null); }
/** * Acquire the leadership * @param id identify of the current participant * @param leaderPath the leader node path * @param listener the leader listener * @return the leadership */ public Leader acquireLeader(String id, String leaderPath, LeaderListener listener){ return new Leader(client, id, leaderPath, listener); }
private void start() { if (started){ return; } doStart(); }
/** * lock the path * @param path the path */ public Lock newLock(String path) { return new Lock(client, path); }
/** * Get the current leader host * @return the current leader host */ public String getLeader(){ return leader.getLeader(); }
/** * Create a node if not exists * @param path path * @return return true if create */ public Boolean createIfNotExists(String path) { return createIfNotExists(path, (byte[])null); }
/** * new a node watcher * @param nodePath the node path * @param listener the node listener * @return the node watcher */ public NodeWatcher newNodeWatcher(String nodePath, NodeListener listener){ return new NodeWatcher(client, nodePath, listener); }
/** * Is the leader or not * @return return true if I'm the leader, or false */ public Boolean isLeader(){ return leader.isLeader(); }
/** * new a watcher of path child * @param path the parent path * @param listener a listener * @param cacheChildData cache child or not * * <p>NOTE: * Only watch first level children, not recursive * </p> * @return the child watcher */ public ChildWatcher newChildWatcher(String path, ChildListener listener, Boolean cacheChildData) { return new ChildWatcher(client, path, cacheChildData, listener); }
AntaresZkAgent(AbstractAntaresClient client, String zkServers, String namespace){ this.client = client; this.zk = ZkClient.newClient(zkServers, namespace); }
@Override public void doShutdown() { watcher.stop(); } }
@Override public void doShutdown() { watcher.stop(); }