/** * @Description: 初始化 * * @throws IOException * @throws InterruptedException * @return void * @author liaoqiqi * @date 2013-6-14 */ private void initInternal(String hosts, String defaultPrefixString) throws IOException, InterruptedException { store = new ResilientActiveKeyValueStore(); store.connect(hosts); LOGGER.info("zoo prefix: " + defaultPrefixString); // 新建父目录 makeDir(defaultPrefixString, ZooUtils.getIp()); }
/** * Zoo的新建目录 * * @param dir */ public void makeDir(String dir, String data) { try { boolean deafult_path_exist = store.exists(dir); if (!deafult_path_exist) { LOGGER.info("create: " + dir); this.writePersistentUrl(dir, data); } else { } } catch (KeeperException e) { LOGGER.error("cannot create path: " + dir, e); } catch (Exception e) { LOGGER.error("cannot create path: " + dir, e); } }
/** * @Description: 带状态信息的读取数据 * * @param path * @param watcher * @param stat * @return * @throws InterruptedException * @throws KeeperException * @return String * @author liaoqiqi * @date 2013-6-17 */ public String read(String path, Watcher watcher, Stat stat) throws InterruptedException, KeeperException { return store.read(path, watcher, stat); }
/** * @Description: 删除结点 * * @param path * @return void * @author liaoqiqi * @date 2013-6-17 */ public void deleteNode(String path) { store.deleteNode(path); }
/* * 路径是否存在 */ public boolean exists(String path) throws Exception { return store.exists(path); }
/* * 生成一个临时结点 */ public String createEphemeralNode(String path, String value, CreateMode createMode) throws Exception { return store.createEphemeralNode(path, value, createMode); }
/* * 返回zk */ public ZooKeeper getZk() { return store.getZk(); }
/** * @Description: 读结点数据 * * @return * @return List<String> * @author liaoqiqi * @date 2013-6-14 */ public String readUrl(String url, Watcher watcher) throws Exception { return store.read(url, watcher, null); }
/** * @Description: 写持久化结点,没有则新建,存在则进行更新 * * @return * @return List<String> * @author liaoqiqi * @date 2013-6-14 */ public void writePersistentUrl(String url, String value) throws Exception { store.write(url, value); }
/** * @Description: 获取子孩子 列表 * * @return * @return List<String> * @author liaoqiqi * @date 2013-6-14 */ public List<String> getRootChildren() { return store.getRootChildren(); }
/** * @Description: 应用程序必须调用它来释放zookeeper资源 * * @return void * @author liaoqiqi * @date 2013-6-14 */ public void release() throws InterruptedException { store.close(); }