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

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

本文整理了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

  1. static void tryCleanupZookeeper(String zkConnect, String groupId) {
  2. try {
  3. String dir = "/consumers/" + groupId;
  4. ZkClient zk = new ZkClient(zkConnect, 30 * 1000, 30 * 1000);
  5. zk.deleteRecursive(dir);
  6. zk.close();
  7. } catch (ZkInterruptedException e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. }

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

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

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

  1. public static void deletePath(ZkClient zkClient, String path) {
  2. try {
  3. zkClient.delete(path);
  4. } catch (ZkNoNodeException e) {
  5. }
  6. }

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

  1. public static void updatePersistentPath(ZkClient zkClient, String path, String data) {
  2. try {
  3. zkClient.writeData(path, Utils.getBytes(data));
  4. } catch (ZkNoNodeException e) {
  5. createParentPath(zkClient, path);
  6. try {
  7. zkClient.createPersistent(path, Utils.getBytes(data));
  8. } catch (ZkNodeExistsException e2) {
  9. zkClient.writeData(path, Utils.getBytes(data));
  10. }
  11. }
  12. }

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

  1. public boolean deleteRecursive(String path) {
  2. List<String> children;
  3. try {
  4. children = getChildren(path, false);
  5. } catch (ZkNoNodeException e) {
  6. return true;
  7. }
  8. if (children != null){
  9. for (String subPath : children) {
  10. if (!deleteRecursive(path + "/" + subPath)) {
  11. return false;
  12. }
  13. }
  14. }
  15. return delete(path);
  16. }

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

  1. @Override
  2. public List<String> call() throws Exception {
  3. exists(path, true);
  4. try {
  5. return getChildren(path, true);
  6. } catch (ZkNoNodeException e) {
  7. // ignore, the "exists" watch will listen for the parent node to appear
  8. }
  9. return null;
  10. }
  11. });

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

  1. public synchronized void connect(final long maxMsToWaitUntilConnected, Watcher watcher) {
  2. if (_eventThread != null) {
  3. return;
  4. }
  5. boolean started = false;
  6. try {
  7. getEventLock().lockInterruptibly();
  8. setShutdownTrigger(false);
  9. _eventThread = new ZkEventThread(_connection.getServers());
  10. _eventThread.start();
  11. _connection.connect(watcher);
  12. LOG.debug("Awaiting connection to Zookeeper server: " + maxMsToWaitUntilConnected);
  13. if (!waitUntilConnected(maxMsToWaitUntilConnected, TimeUnit.MILLISECONDS)) {
  14. throw new ZkTimeoutException(String.format(
  15. "Unable to connect to zookeeper server[%s] within timeout %dms", _connection.getServers(), maxMsToWaitUntilConnected));
  16. }
  17. started = true;
  18. } catch (InterruptedException e) {
  19. States state = _connection.getZookeeperState();
  20. throw new IllegalStateException("Not connected with zookeeper server yet. Current state is " + state);
  21. } finally {
  22. getEventLock().unlock();
  23. // we should close the zookeeper instance, otherwise it would keep
  24. // on trying to connect
  25. if (!started) {
  26. close();
  27. }
  28. }
  29. }

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

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

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

  1. @Override
  2. public void run() throws Exception {
  3. // reinstall watch
  4. exists(path, true);
  5. try {
  6. byte[] data = readData(path, null, true);
  7. listener.handleDataChange(path, data);
  8. } catch (ZkNoNodeException e) {
  9. listener.handleDataDeleted(path);
  10. }
  11. }
  12. });

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

  1. public void cas(String path, DataUpdater updater) {
  2. Stat stat = new Stat();
  3. boolean retry;
  4. do {
  5. retry = false;
  6. try {
  7. byte[] oldData = readData(path, stat);
  8. byte[] newData = updater.update(oldData);
  9. writeData(path, newData, stat.getVersion());
  10. } catch (ZkBadVersionException e) {
  11. retry = true;
  12. }
  13. } while (retry);
  14. }

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

  1. public boolean waitUntilExists(String path, TimeUnit timeUnit, long time) throws ZkInterruptedException {
  2. Date timeout = new Date(System.currentTimeMillis() + timeUnit.toMillis(time));
  3. LOG.debug("Waiting until znode '" + path + "' becomes available.");
  4. if (exists(path)) {
  5. return true;
  6. }
  7. acquireEventLock();
  8. try {
  9. while (!exists(path, true)) {
  10. boolean gotSignal = getEventLock().getZNodeEventCondition().awaitUntil(timeout);
  11. if (!gotSignal) {
  12. return false;
  13. }
  14. }
  15. return true;
  16. } catch (InterruptedException e) {
  17. throw new ZkInterruptedException(e);
  18. } finally {
  19. getEventLock().unlock();
  20. }
  21. }

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

  1. public void close() {
  2. this.zkClient.close();
  3. }

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

  1. public ZKBrokerPartitionInfo(ZKConfig zkConfig, Callback callback) {
  2. this.zkConfig = zkConfig;
  3. this.callback = callback;
  4. //
  5. this.zkClient = new ZkClient(zkConfig.getZkConnect(), //
  6. zkConfig.getZkSessionTimeoutMs(), //
  7. zkConfig.getZkConnectionTimeoutMs());
  8. //
  9. this.allBrokers = getZKBrokerInfo();
  10. this.topicBrokerPartitions = getZKTopicPartitionInfo(this.allBrokers);
  11. //use just the brokerTopicsListener for all watchers
  12. this.brokerTopicsListener = new BrokerTopicsListener(this.topicBrokerPartitions, this.allBrokers);
  13. //register listener for change of topics to keep topicsBrokerPartitions updated
  14. zkClient.subscribeChildChanges(ZkUtils.BrokerTopicsPath, brokerTopicsListener);
  15. //register listener for change of brokers for each topic to keep topicsBrokerPartitions updated
  16. for (String topic : this.topicBrokerPartitions.keySet()) {
  17. zkClient.subscribeChildChanges(ZkUtils.BrokerTopicsPath + "/" + topic, this.brokerTopicsListener);
  18. }
  19. // register listener for new broker
  20. zkClient.subscribeChildChanges(ZkUtils.BrokerIdsPath, this.brokerTopicsListener);
  21. //
  22. // register listener for session expired event
  23. zkClient.subscribeStateChanges(new ZKSessionExpirationListener());
  24. }

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

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

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

  1. public boolean exists(final String path) {
  2. return exists(path, hasListeners(path));
  3. }

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

  1. public List<String> getChildren(String path) {
  2. return getChildren(path, hasListeners(path));
  3. }

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

  1. public void createPersistent(String path, boolean createParents) {
  2. try {
  3. create(path, null, CreateMode.PERSISTENT);
  4. } catch (ZkNodeExistsException e) {
  5. if (!createParents) {
  6. throw e;
  7. }
  8. } catch (ZkNoNodeException e) {
  9. if (!createParents) {
  10. throw e;
  11. }
  12. String parentDir = path.substring(0, path.lastIndexOf('/'));
  13. createPersistent(parentDir, createParents);
  14. createPersistent(path, createParents);
  15. }
  16. }

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

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

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

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

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

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

相关文章