public LinkedHashMap<String, List<String>> listAll() { List<String> groups = zkClient.getChildren(ZkPaths.rootPath()); LinkedHashMap<String, List<String>> result = Maps.newLinkedHashMap(); for (String group : groups) { List<String> cells = zkClient.getChildren(ZkPaths.groupPath(group)); if (cells != null && !cells.isEmpty()) { // 过滤空 group result.put(group, cells); } } return result; }
@SuppressWarnings("unchecked") public T poll() throws Exception { try { List<String> list = zkClient.getChildren(root); if (list.size() == 0) { return null; } Collections.sort( list, new Comparator<String>() { public int compare(String lhs, String rhs) { return getNodeNumber(lhs, Node_NAME).compareTo(getNodeNumber(rhs, Node_NAME)); } }); for (String nodeName : list) { String nodeFullPath = root.concat("/").concat(nodeName); try { T node = (T) zkClient.readData(nodeFullPath); zkClient.delete(nodeFullPath); return node; } catch (ZkNoNodeException e) { // ignore } } return null; } catch (Exception e) { throw ExceptionUtil.convertToRuntimeException(e); } }
public List<String> getChildren(String path) { try { return client.getChildren(path); } catch (ZkNoNodeException e) { return null; } }
@SuppressWarnings("unchecked") public List<T> getElements() { List<String> paths = _zkClient.getChildren(_elementsPath); List<T> elements = new ArrayList<T>(); for (String path : paths) { elements.add((T) _zkClient.readData(path)); } return elements; }
@Export public void clean() { List<String> groups = zkClient.getChildren(ZkPaths.rootPath()); for (String group : groups) { if (zkClient.countChildren(ZkPaths.groupPath(group)) == 0) { zkClient.delete(ZkPaths.groupPath(group)); } } }
private List<String> getConnectedHosts() { List<String> ips = zkClient.getChildren(WATCH_PATH); for (int i = 0; i < ips.size(); i++) { if (!isConnected(ips.get(i))) { ips.remove(i); i--; } } return ips; }
@Export public List<String> listGroups() { List<String> groups = zkClient.getChildren(ZkPaths.rootPath()); List<String> notEmptyGroups = Lists.newArrayList(); for (String group : groups) { if (zkClient.countChildren(ZkPaths.groupPath(group)) > 0) { notEmptyGroups.add(group); } } return notEmptyGroups; }
@SuppressWarnings("unchecked") protected Element<T> getFirstElement() throws InterruptedException { final Object mutex = new Object(); IZkChildListener notifyListener = new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { synchronized (mutex) { mutex.notify(); } } }; try { while (true) { List<String> elementNames; synchronized (mutex) { elementNames = _zkClient.subscribeChildChanges(_elementsPath, notifyListener); while (elementNames == null || elementNames.isEmpty()) { mutex.wait(); elementNames = _zkClient.getChildren(_elementsPath); } } String elementName = getSmallestElement(elementNames); try { String elementPath = getElementPath(elementName); return new Element<T>(elementName, (T) _zkClient.readData(elementPath)); } catch (ZkNoNodeException e) { // somebody else picked up the element first, so we have to // retry with the new first element } } } catch (InterruptedException e) { throw e; } catch (Exception e) { throw ExceptionUtil.convertToRuntimeException(e); } finally { _zkClient.unsubscribeChildChanges(_elementsPath, notifyListener); } }
public boolean isEmpty() { return zkClient.getChildren(root).size() == 0; }
public int size() { return zkClient.getChildren(root).size(); }
public int size() { return _zkClient.getChildren(_elementsPath).size(); }
@Export(paramNames = {"group"}) public List<String> listCellsByGroup(String group) { return zkClient.getChildren(ZkPaths.groupPath(group)); }
public static List<String> getChildren(String path) { return zkClient.getChildren(path); }