me.hao0.antares.common.zk.ZkClient类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(134)

本文整理了Java中me.hao0.antares.common.zk.ZkClient类的一些代码示例,展示了ZkClient类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZkClient类的具体详情如下:
包路径:me.hao0.antares.common.zk.ZkClient
类名称:ZkClient

ZkClient介绍

[英]Author: haolin Email : haolin.h0@gmail.com
[中]作者:haolin电子邮件:haolin。h0@gmail.com

代码示例

代码示例来源:origin: ihaolin/antares

/**
 * Delete the node recursively if the path exists
 * @param path the node path
 */
public void deleteRecursivelyIfExists(String path){
  try {
    if(checkExists(path)){
      deleteRecursively(path);
    }
  } catch (Exception e){
    handleConnectionLoss(e);
    throw new ZkException(e);
  }
}

代码示例来源:origin: ihaolin/antares

/**
 * Create an ephemeral path
 * @param path path
 * @return the path created
 */
public String createEphemeral(String path) {
  return createEphemeral(path, (byte[]) null);
}

代码示例来源:origin: ihaolin/antares

private void getServersOnce() {
  List<String> servers = zk.gets(ZkPaths.SERVERS);
  if (servers.isEmpty()){
    log.warn("there are no available servers, please check the environment.");
    return;
  }
  for (String server: servers){
    client.addHttpServer(server);
  }
}

代码示例来源:origin: ihaolin/antares

/**
 * Delete the node if the node exists
 * @param path node path
 */
public void deleteIfExists(String path) {
  try {
    if(checkExists(path)){
      delete(path);
    }
  } catch (Exception e){
    handleConnectionLoss(e);
    throw new ZkException(e);
  }
}

代码示例来源:origin: ihaolin/antares

/**
 * Create an persistent path
 * @param path path
 * @param data string data
 * @return the path created
 */
public String create(String path, String data){
  try {
    return create(path, data.getBytes("UTF-8"));
  } catch (Exception e) {
    handleConnectionLoss(e);
    throw new ZkException(e);
  }
}

代码示例来源:origin: ihaolin/antares

@Override
  public void run() {
    String server = serverHost.get();
    // mkdirs /cluster/servers if necessary
    zk.client().mkdirs(ZkPaths.SERVERS);
    // register the server node
    String serverPath = ZkPaths.pathOfServer(server);
    if (!zk.client().checkExists(serverPath)){
      String result = zk.client().createEphemeral(ZkPaths.pathOfServer(server));
      Logs.info("server({}) registered: {}", server, result);
    }
  }
}, 1, 5, TimeUnit.SECONDS);

代码示例来源:origin: ihaolin/antares

/**
 * Update the job fire time info
 * @param appName the app name
 * @param jobClass the job class
 * @param jobFireTime the job fire time
 * @return return true if update successfully, or false
 */
public Boolean updateJobFireTime(String appName, String jobClass, JobFireTime jobFireTime) {
  String jobFireTimeNode = ZkPaths.pathOfJobFireTime(appName, jobClass);
  zk.client().mkdirs(jobFireTimeNode);
  return zk.client().update(jobFireTimeNode, JSON.toJSONString(jobFireTime));
}

代码示例来源:origin: ihaolin/antares

@Override
  public void run() {
    ZkClient zk = client.getZk();
    // register period, prevent client disconnects unexpected
    String appClientPath = ZkPaths.pathOfAppClient(client.getAppName(), Systems.hostPid());
    if (!zk.checkExists(appClientPath)){
      zk.createEphemeral(appClientPath);
    }
  }
}, 1, 10, TimeUnit.SECONDS);

代码示例来源:origin: ihaolin/antares

public AppClientCluster(AntaresZkClient zk, String appName){
  this.appName = appName;
  this.zk = zk;
  // get alive clients once
  String appClientsPath = ZkPaths.pathOfAppClients(appName);
  zk.client().mkdirs(appClientsPath);
  List<String> clients = zk.client().gets(appClientsPath);
  if (!CollectionUtil.isNullOrEmpty(clients)){
    alives.addAll(clients);
  }
}

代码示例来源:origin: ihaolin/antares

/**
 * Get the job scheduler
 * @param appName the app name
 * @param jobClass the job class
 * @return the job scheduler
 */
public String getJobScheduler(String appName, String jobClass) {
  String jobSchedulerNode = ZkPaths.pathOfJobScheduler(appName, jobClass);
  if (!zk.client().checkExists(jobSchedulerNode)){
    return null;
  }
  return zk.client().getString(jobSchedulerNode);
}

代码示例来源:origin: ihaolin/antares

public Boolean update(String path){
  return update(path, (byte[])null);
}

代码示例来源:origin: ihaolin/antares

/**
 * 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);
}

代码示例来源:origin: ihaolin/antares

/**
 * Get the children of the path
 * @param path the path
 * @return the children of the path
 */
public List<String> gets(String path){
  try {
    if (!checkExists(path)){
      return Collections.emptyList();
    }
    return client.getChildren().forPath(path);
  } catch (Exception e) {
    handleConnectionLoss(e);
    throw new ZkException(e);
  }
}

代码示例来源:origin: ihaolin/antares

/**
 * Get the job fire time info
 * @param appName the app name
 * @param jobClass the job class
 * @return the job fire time info
 */
public JobFireTime getJobFireTime(String appName, String jobClass){
  String jobFireTimeNode = ZkPaths.pathOfJobFireTime(appName, jobClass);
  if (!zk.client().checkExists(jobFireTimeNode)){
    return null;
  }
  return zk.client().getJson(jobFireTimeNode, JobFireTime.class);
}

代码示例来源:origin: ihaolin/antares

/**
 * Get the job state
 * @param appName the app name
 * @param jobClass the job class
 * @return the job state
 */
public JobState getJobState(String appName, String jobClass) {
  String jobStateNode = ZkPaths.pathOfJobState(appName, jobClass);
  if (!zk.client().checkExists(jobStateNode)){
    return JobState.STOPPED;
  }
  return JobState.from(zk.client().getInteger(jobStateNode));
}

代码示例来源:origin: ihaolin/antares

/**
 * Try to wait server to start
 * @param server the server
 * @return return true if server started, or false
 */
private Boolean tryWaitServerStart(String server) {
  Sleeps.sleep(serverFailoverWaitTime);
  // check server register?
  String serverPath = ZkPaths.pathOfServer(server);
  return zk.client().checkExists(serverPath);
}

代码示例来源:origin: ihaolin/antares

@Override
public Response<List<ServerInfo>> listServers() {
  try {
    List<String> servers = zk.client().gets(ZkPaths.SERVERS);
    if (CollectionUtil.isNullOrEmpty(servers)){
      return Response.ok(Collections.<ServerInfo>emptyList());
    }
    String leader = zk.client().getString(ZkPaths.LEADER);
    List<ServerInfo> serverInfos = Lists.newArrayListWithExpectedSize(servers.size());
    ServerInfo serverInfo;
    for (String server: servers){
      serverInfo = new ServerInfo();
      if (Objects.equal(server, leader)){
        serverInfo.setLeader(true);
      }
      serverInfo.setServer(server);
      serverInfo.setJobCount(jobServerDao.countJobsByServer(server).intValue());
      serverInfos.add(serverInfo);
    }
    return Response.ok(serverInfos);
  } catch (Exception e){
    Logs.error("failed to list servers, cause: {}", Throwables.getStackTraceAsString(e));
    return Response.notOk("server.list.failed");
  }
}

代码示例来源:origin: ihaolin/antares

/**
 * 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);
}

代码示例来源:origin: ihaolin/antares

@Override
public void doStart() {
  String jobInstancesLockPath = Lock.PREFIX + ZkPaths.JOB_INSTANCES;
  zk.client().mkdirs(jobInstancesLockPath);
  emptyChildCleaner = new ChildReaper(zk.client().client(), jobInstancesLockPath, Reaper.Mode.REAP_INDEFINITELY);
  try {
    String serversFailover = Lock.PREFIX + ZkPaths.SERVER_FAILOVER;
    zk.client().mkdirs(serversFailover);
    emptyChildCleaner.addPath(serversFailover);
    emptyChildCleaner.start();
  } catch (Exception e) {
    throw new ZkException(e);
  }
}

代码示例来源:origin: ihaolin/antares

/**
 * Make the job instances node
 * @param appName the app name
 * @param jobClass the job class
 * @return return true if make successfully, or false
 */
public Boolean mkJobInstances(String appName, String jobClass) {
  return zk.client().mkdirs(ZkPaths.pathOfJobInstances(appName, jobClass));
}

相关文章