org.apache.twill.zookeeper.ZKClient.getChildren()方法的使用及代码示例

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

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

ZKClient.getChildren介绍

[英]Gets the list of children nodes under the given path. Same as calling #getChildren(String,org.apache.zookeeper.Watcher).
[中]获取给定路径下的子节点列表。与调用#getChildren(String,org.apache.zookeeper.Watcher)相同。

代码示例

代码示例来源:origin: apache/twill

@Override
 public OperationFuture<NodeChildren> exec(String path, Watcher watcher) {
  return zkClient.getChildren(path, watcher);
 }
}, path, callback, cancelled);

代码示例来源:origin: org.apache.twill/twill-zookeeper

@Override
 public OperationFuture<NodeChildren> exec(String path, Watcher watcher) {
  return zkClient.getChildren(path, watcher);
 }
}, path, callback, cancelled);

代码示例来源:origin: apache/twill

@Override
public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) {
 return delegate.getChildren(path, watcher);
}

代码示例来源:origin: org.apache.twill/twill-zookeeper

@Override
public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) {
 return delegate.getChildren(path, watcher);
}

代码示例来源:origin: org.apache.twill/twill-zookeeper

@Override
public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) {
 return client.getChildren(path, watcher);
}

代码示例来源:origin: apache/twill

@Override
public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) {
 return client.getChildren(path, watcher);
}

代码示例来源:origin: caskdata/coopr

/**
 * Acts as {@link ZKClient#getChildren(String, Watcher)} if node exists.
 * Otherwise sets {@code null} in returned future.
 */
public static ListenableFuture<NodeChildren> getChildrenOrNull(final ZKClient zkClient,
                         final String path,
                         @Nullable Watcher watcher) {
 return ignoreError(zkClient.getChildren(path, watcher), KeeperException.NoNodeException.class);
}

代码示例来源:origin: apache/twill

/**
 * Watches for messages that are sent through ZK messages node.
 */
private void watchMessages() {
 final String messagesPath = getZKPath("messages");
 Futures.addCallback(zkClient.getChildren(messagesPath, new Watcher() {
  @Override
  public void process(WatchedEvent event) {
   if (event.getType() == Event.EventType.NodeChildrenChanged && isRunning()) {
    watchMessages();
   }
  }
 }), new FutureCallback<NodeChildren>() {
  @Override
  public void onSuccess(NodeChildren result) {
   // Sort by the name, which is the messageId. Assumption is that message ids is ordered by time.
   List<String> messages = Lists.newArrayList(result.getChildren());
   Collections.sort(messages);
   for (String messageId : messages) {
    processMessage(messagesPath + "/" + messageId, messageId);
   }
  }
  @Override
  public void onFailure(Throwable t) {
   // TODO: what could be done besides just logging?
   LOG.error("Failed to watch messages.", t);
  }
 }, Threads.SAME_THREAD_EXECUTOR);
}

代码示例来源:origin: org.apache.twill/twill-core

/**
 * Watches for messages that are sent through ZK messages node.
 */
private void watchMessages() {
 final String messagesPath = getZKPath("messages");
 Futures.addCallback(zkClient.getChildren(messagesPath, new Watcher() {
  @Override
  public void process(WatchedEvent event) {
   if (event.getType() == Event.EventType.NodeChildrenChanged && isRunning()) {
    watchMessages();
   }
  }
 }), new FutureCallback<NodeChildren>() {
  @Override
  public void onSuccess(NodeChildren result) {
   // Sort by the name, which is the messageId. Assumption is that message ids is ordered by time.
   List<String> messages = Lists.newArrayList(result.getChildren());
   Collections.sort(messages);
   for (String messageId : messages) {
    processMessage(messagesPath + "/" + messageId, messageId);
   }
  }
  @Override
  public void onFailure(Throwable t) {
   // TODO: what could be done besides just logging?
   LOG.error("Failed to watch messages.", t);
  }
 }, Threads.SAME_THREAD_EXECUTOR);
}

代码示例来源:origin: caskdata/cdap

zkClient.getChildren(CoordinationConstants.REQUIREMENTS_PATH, watcher),
wrapCallback(new FutureCallback<NodeChildren>() {
 @Override

代码示例来源:origin: org.apache.twill/twill-yarn

/**
  * Returns the list of children node under the given path.
  *
  * @param path path to get children
  * @return the list of children or empty list if the path doesn't exist.
  * @throws Exception if failed to get children
  */
 private List<String> getChildren(String path) throws Exception {
  try {
   return zkClient.getChildren(path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getChildren();
  } catch (ExecutionException e) {
   if (e.getCause() instanceof KeeperException.NoNodeException) {
    // If the node doesn't exists, return an empty list
    return Collections.emptyList();
   }
   throw e;
  }
 }
}

代码示例来源:origin: caskdata/coopr

private void clearInternal() {
 if (currentView.size() > 0) {
  currentView = Collections.emptyMap();
  NodeChildren nodeChildren = Futures.getUnchecked(zkClient.getChildren(ENTRIES_PATH));
  List<ListenableFuture<String>> deleteFutures = Lists.newArrayList();
  for (String node : nodeChildren.getChildren()) {
   deleteFutures.add(ZKClientExt.delete(zkClient, getNodePath(node), true));
  }
  Futures.getUnchecked(Futures.allAsList(deleteFutures));
 }
}

代码示例来源:origin: apache/twill

/**
  * Returns the list of children node under the given path.
  *
  * @param path path to get children
  * @return the list of children or empty list if the path doesn't exist.
  * @throws Exception if failed to get children
  */
 private List<String> getChildren(String path) throws Exception {
  try {
   return zkClient.getChildren(path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getChildren();
  } catch (ExecutionException e) {
   if (e.getCause() instanceof KeeperException.NoNodeException) {
    // If the node doesn't exists, return an empty list
    return Collections.emptyList();
   }
   throw e;
  }
 }
}

代码示例来源:origin: caskdata/coopr

public void clear() {
 currentView.set(Collections.<String, T>emptyMap());
 // Hint: again, we can try to make removal more efficient by cleaning only when in-mem collection cleaned smth,
 //       but then we may face races...
 NodeChildren nodeChildren = Futures.getUnchecked(zkClient.getChildren(""));
 List<ListenableFuture<String>> deleteFutures = Lists.newArrayList();
 for (String node : nodeChildren.getChildren()) {
  deleteFutures.add(ZKClientExt.delete(zkClient, getNodePath(node), true));
 }
 Futures.getUnchecked(Futures.allAsList(deleteFutures));
}

代码示例来源:origin: caskdata/coopr

public void clear() {
 currentView.set(Collections.<T>emptyList());
 // Hint: again, we can try to make removal more efficient by cleaning only when in-mem collection cleaned smth,
 //       but then we may face races...
 NodeChildren nodeChildren = Futures.getUnchecked(zkClient.getChildren(""));
 List<ListenableFuture<String>> deleteFutures = Lists.newArrayList();
 for (String node : nodeChildren.getChildren()) {
  deleteFutures.add(ZKClientExt.delete(zkClient, getNodePath(node), true));
 }
 Futures.getUnchecked(Futures.allAsList(deleteFutures));
}

代码示例来源:origin: org.apache.twill/twill-zookeeper

@Override
public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) {
 return relayFuture(delegate.getChildren(getNamespacedPath(path), watcher), this.<NodeChildren>createFuture(path));
}

代码示例来源:origin: apache/twill

@Override
public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) {
 return relayFuture(delegate.getChildren(getNamespacedPath(path), watcher), this.<NodeChildren>createFuture(path));
}

代码示例来源:origin: caskdata/cdap

/**
 * Fetches the latest participants from ZK. This method will block until it fetched all participants information.
 * Note that the map returned is only a snapshot of the leader election information in ZK, which only reflects
 * the states in ZK at the time when the snapshot was taken.
 *
 * @return An immutable {@link SortedMap} ordered by the participant ID with the smallest key in the map
 *         as the current leader
 * @throws InterruptedException if the caller thread is interrupted while waiting for the participants information
 *                              to be available
 * @throws Exception if failed to fetch information from ZK
 */
public SortedMap<Integer, Participant> fetchCurrentParticipants() throws Exception {
 try {
  NodeChildren nodeChildren = zkClient.getChildren(leaderElectionPath).get();
  ConcurrentNavigableMap<Integer, Participant> result = new ConcurrentSkipListMap<>();
  SettableFuture<CountDownLatch> completion = SettableFuture.create();
  childrenUpdated(nodeChildren, result, completion);
  completion.get().await();
  return Collections.unmodifiableSortedMap(result);
 } catch (ExecutionException e) {
  // If the election path doesn't exists, that means there is no participant
  Throwable cause = e.getCause();
  if (cause instanceof KeeperException.NoNodeException) {
   return ImmutableSortedMap.of();
  }
  Throwables.propagateIfPossible(cause, Exception.class);
  // Shouldn't reach here as we propagate any Exception/RuntimeException/Error already
  return ImmutableSortedMap.of();
 }
}

代码示例来源:origin: co.cask.cdap/cdap-common

/**
 * Fetches the latest participants from ZK. This method will block until it fetched all participants information.
 * Note that the map returned is only a snapshot of the leader election information in ZK, which only reflects
 * the states in ZK at the time when the snapshot was taken.
 *
 * @return An immutable {@link SortedMap} ordered by the participant ID with the smallest key in the map
 *         as the current leader
 * @throws InterruptedException if the caller thread is interrupted while waiting for the participants information
 *                              to be available
 * @throws Exception if failed to fetch information from ZK
 */
public SortedMap<Integer, Participant> fetchCurrentParticipants() throws Exception {
 try {
  NodeChildren nodeChildren = zkClient.getChildren(leaderElectionPath).get();
  ConcurrentNavigableMap<Integer, Participant> result = new ConcurrentSkipListMap<>();
  SettableFuture<CountDownLatch> completion = SettableFuture.create();
  childrenUpdated(nodeChildren, result, completion);
  completion.get().await();
  return Collections.unmodifiableSortedMap(result);
 } catch (ExecutionException e) {
  // If the election path doesn't exists, that means there is no participant
  Throwable cause = e.getCause();
  if (cause instanceof KeeperException.NoNodeException) {
   return ImmutableSortedMap.of();
  }
  Throwables.propagateIfPossible(cause, Exception.class);
  // Shouldn't reach here as we propagate any Exception/RuntimeException/Error already
  return ImmutableSortedMap.of();
 }
}

代码示例来源:origin: caskdata/coopr

@Override
protected void startUp() throws Exception {
 Futures.getUnchecked(ZKClientExt.ensureExists(zkClient, queueType.getPath()));
 refreshQueues(Futures.getUnchecked(zkClient.getChildren(queueType.getPath())));
 ZKOperations.watchChildren(zkClient, queueType.getPath(), new ZKOperations.ChildrenCallback() {
  @Override
  public void updated(NodeChildren nodeChildren) {
   refreshQueues(nodeChildren);
  }
 });
}

相关文章