public NodeMonitor() { childListener = new IZkChildListener() { public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { if (currentChilds != null) { initNodes(currentChilds); } } }; List<String> childs = zookeeper.subscribeChildChanges(ArbitrateConstants.NODE_NID_ROOT, childListener); if (childs == null) { // 如果为null,代表系统节点为初始化 try { zookeeper.createPersistent(ArbitrateConstants.NODE_NID_ROOT, true); } catch (ZkNodeExistsException e) { // ignore } childs = zookeeper.getChildren(ArbitrateConstants.NODE_NID_ROOT); } initNodes(childs); // syncNodes();// 开始监视node节点的变化 MonitorScheduler.register(this); }
public void destory() { listeners.clear(); zookeeper.unsubscribeChildChanges(ArbitrateConstants.NODE_NID_ROOT, childListener); MonitorScheduler.unRegister(this); }