gpt4 book ai didi

com.github.zkclient.ZkClient类的使用及代码示例

转载 作者:知者 更新时间:2024-03-13 12:29:58 29 4
gpt4 key购买 nike

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

ZkClient介绍

[英]Zookeeper client

The client is thread-safety
[中]动物园管理员客户
客户端是线程安全的

代码示例

代码示例来源:origin: adyliu/jafka

static void tryCleanupZookeeper(String zkConnect, String groupId) {
    try {
      String dir = "/consumers/" + groupId;
      ZkClient zk = new ZkClient(zkConnect, 30 * 1000, 30 * 1000);
      zk.deleteRecursive(dir);
      zk.close();
    } catch (ZkInterruptedException e) {
      e.printStackTrace();
    }
  }
}

代码示例来源:origin: adyliu/jafka

public static void makeSurePersistentPathExists(ZkClient zkClient, String path) {
  if (!zkClient.exists(path)) {
    zkClient.createPersistent(path, true);
  }
}

代码示例来源:origin: adyliu/jafka

public static void deletePath(ZkClient zkClient, String path) {
  try {
    zkClient.delete(path);
  } catch (ZkNoNodeException e) {
  }
}

代码示例来源:origin: adyliu/jafka

public static void updatePersistentPath(ZkClient zkClient, String path, String data) {
  try {
    zkClient.writeData(path, Utils.getBytes(data));
  } catch (ZkNoNodeException e) {
    createParentPath(zkClient, path);
    try {
      zkClient.createPersistent(path, Utils.getBytes(data));
    } catch (ZkNodeExistsException e2) {
      zkClient.writeData(path, Utils.getBytes(data));
    }
  }
}

代码示例来源:origin: com.github.adyliu/zkclient

public boolean deleteRecursive(String path) {
  List<String> children;
  try {
    children = getChildren(path, false);
  } catch (ZkNoNodeException e) {
    return true;
  }
  if (children != null){
    for (String subPath : children) {
      if (!deleteRecursive(path + "/" + subPath)) {
        return false;
      }
    }
  }
  return delete(path);
}

代码示例来源:origin: com.github.adyliu/zkclient

@Override
  public List<String> call() throws Exception {
    exists(path, true);
    try {
      return getChildren(path, true);
    } catch (ZkNoNodeException e) {
      // ignore, the "exists" watch will listen for the parent node to appear
    }
    return null;
  }
});

代码示例来源:origin: adyliu/zkclient

public synchronized void connect(final long maxMsToWaitUntilConnected, Watcher watcher) {
  if (_eventThread != null) {
    return;
  }
  boolean started = false;
  try {
    getEventLock().lockInterruptibly();
    setShutdownTrigger(false);
    _eventThread = new ZkEventThread(_connection.getServers());
    _eventThread.start();
    _connection.connect(watcher);
    LOG.debug("Awaiting connection to Zookeeper server: " + maxMsToWaitUntilConnected);
    if (!waitUntilConnected(maxMsToWaitUntilConnected, TimeUnit.MILLISECONDS)) {
      throw new ZkTimeoutException(String.format(
          "Unable to connect to zookeeper server[%s] within timeout %dms", _connection.getServers(), maxMsToWaitUntilConnected));
    }
    started = true;
  } catch (InterruptedException e) {
    States state = _connection.getZookeeperState();
    throw new IllegalStateException("Not connected with zookeeper server yet. Current state is " + state);
  } finally {
    getEventLock().unlock();
    // we should close the zookeeper instance, otherwise it would keep
    // on trying to connect
    if (!started) {
      close();
    }
  }
}

代码示例来源:origin: adyliu/jafka

public static String readData(ZkClient zkClient, String path) {
  return Utils.fromBytes(zkClient.readData(path));
}

代码示例来源:origin: com.github.adyliu/zkclient

@Override
  public void run() throws Exception {
    // reinstall watch
    exists(path, true);
    try {
      byte[] data = readData(path, null, true);
      listener.handleDataChange(path, data);
    } catch (ZkNoNodeException e) {
      listener.handleDataDeleted(path);
    }
  }
});

代码示例来源:origin: com.github.adyliu/zkclient

public void cas(String path, DataUpdater updater) {
  Stat stat = new Stat();
  boolean retry;
  do {
    retry = false;
    try {
      byte[] oldData = readData(path, stat);
      byte[] newData = updater.update(oldData);
      writeData(path, newData, stat.getVersion());
    } catch (ZkBadVersionException e) {
      retry = true;
    }
  } while (retry);
}

代码示例来源:origin: com.github.adyliu/zkclient

public boolean waitUntilExists(String path, TimeUnit timeUnit, long time) throws ZkInterruptedException {
  Date timeout = new Date(System.currentTimeMillis() + timeUnit.toMillis(time));
  LOG.debug("Waiting until znode '" + path + "' becomes available.");
  if (exists(path)) {
    return true;
  }
  acquireEventLock();
  try {
    while (!exists(path, true)) {
      boolean gotSignal = getEventLock().getZNodeEventCondition().awaitUntil(timeout);
      if (!gotSignal) {
        return false;
      }
    }
    return true;
  } catch (InterruptedException e) {
    throw new ZkInterruptedException(e);
  } finally {
    getEventLock().unlock();
  }
}

代码示例来源:origin: adyliu/jafka

public void close() {
  this.zkClient.close();
}

代码示例来源:origin: adyliu/jafka

public ZKBrokerPartitionInfo(ZKConfig zkConfig, Callback callback) {
  this.zkConfig = zkConfig;
  this.callback = callback;
  //
  this.zkClient = new ZkClient(zkConfig.getZkConnect(), //
      zkConfig.getZkSessionTimeoutMs(), //
      zkConfig.getZkConnectionTimeoutMs());
  //
  this.allBrokers = getZKBrokerInfo();
  this.topicBrokerPartitions = getZKTopicPartitionInfo(this.allBrokers);
  //use just the brokerTopicsListener for all watchers
  this.brokerTopicsListener = new BrokerTopicsListener(this.topicBrokerPartitions, this.allBrokers);
  //register listener for change of topics to keep topicsBrokerPartitions updated
  zkClient.subscribeChildChanges(ZkUtils.BrokerTopicsPath, brokerTopicsListener);
  //register listener for change of brokers for each topic to keep topicsBrokerPartitions updated
  for (String topic : this.topicBrokerPartitions.keySet()) {
    zkClient.subscribeChildChanges(ZkUtils.BrokerTopicsPath + "/" + topic, this.brokerTopicsListener);
  }
  // register listener for new broker
  zkClient.subscribeChildChanges(ZkUtils.BrokerIdsPath, this.brokerTopicsListener);
  //
  // register listener for session expired event
  zkClient.subscribeStateChanges(new ZKSessionExpirationListener());
}

代码示例来源:origin: com.github.adyliu/zkclient

public byte[] readData(String path, Stat stat) {
  return readData(path, stat, hasListeners(path));
}

代码示例来源:origin: com.github.adyliu/zkclient

public boolean exists(final String path) {
  return exists(path, hasListeners(path));
}

代码示例来源:origin: adyliu/zkclient

public List<String> getChildren(String path) {
  return getChildren(path, hasListeners(path));
}

代码示例来源:origin: com.github.adyliu/zkclient

public void createPersistent(String path, boolean createParents) {
  try {
    create(path, null, CreateMode.PERSISTENT);
  } catch (ZkNodeExistsException e) {
    if (!createParents) {
      throw e;
    }
  } catch (ZkNoNodeException e) {
    if (!createParents) {
      throw e;
    }
    String parentDir = path.substring(0, path.lastIndexOf('/'));
    createPersistent(parentDir, createParents);
    createPersistent(path, createParents);
  }
}

代码示例来源:origin: adyliu/jafka

private void connectZk() {
  logger.debug("Connecting to zookeeper instance at " + config.getZkConnect());
  this.zkClient = new ZkClient(config.getZkConnect(), config.getZkSessionTimeoutMs(),
      config.getZkConnectionTimeoutMs());
  logger.debug("Connected to zookeeper at " + config.getZkConnect());
}

代码示例来源:origin: adyliu/jafka

public void startup() {
  logger.info("connecting to zookeeper: " + config.getZkConnect());
  zkClient = new ZkClient(config.getZkConnect(), config.getZkSessionTimeoutMs(),
      config.getZkConnectionTimeoutMs());
  zkClient.subscribeStateChanges(this);
}

代码示例来源:origin: adyliu/jafka

private static void createParentPath(ZkClient zkClient, String path) {
  String parentDir = path.substring(0, path.lastIndexOf('/'));
  if (parentDir.length() != 0) {
    zkClient.createPersistent(parentDir, true);
  }
}

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com