packagezookeeper_demo;importjava.util.List;importjava.util.concurrent.CountDownLatch;importorg.apache.zookeeper.CreateMode;importorg.apache.zookeeper.KeeperException;importorg.apache.zookeeper.WatchedEvent;importorg.apache.zookeeper.Watcher;importorg.apache.zookeeper.Watcher.Event.KeeperState;importorg.apache.zookeeper.ZooDefs.Ids;importorg.apache.zookeeper.ZooKeeper;importorg.apache.zookeeper.data.Stat;public class BaseZookeeper implementsWatcher{privateZooKeeper zookeeper;/*** 超时时间*/
private static final int SESSION_TIME_OUT = 2000;private CountDownLatch countDownLatch = new CountDownLatch(1);public voidprocess(WatchedEvent event) {if (event.getState() ==KeeperState.SyncConnected) {
System.out.println("Watch received event");
countDownLatch.countDown();
}
}/**连接zookeeper
*@paramhost
*@throwsException*/
public void connectZookeeper(String host) throwsException{
zookeeper= new ZooKeeper(host, SESSION_TIME_OUT, this);
countDownLatch.await();
System.out.println("zookeeper connection success");
}/*** 创建节点
*@parampath
*@paramdata
*@throwsException*/
public String createNode(String path,String data) throwsException{return this.zookeeper.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}/*** 获取路径下所有子节点
*@parampath
*@return*@throwsKeeperException
*@throwsInterruptedException*/
public List getChildren(String path) throwsKeeperException, InterruptedException{
List children = zookeeper.getChildren(path, false);returnchildren;
}/*** 获取节点上面的数据
*@parampath 路径
*@return*@throwsKeeperException
*@throwsInterruptedException*/
public String getData(String path) throwsKeeperException, InterruptedException{byte[] data = zookeeper.getData(path, false, null);if (data == null) {return "";
}return newString(data);
}/*** 设置节点信息
*@parampath 路径
*@paramdata 数据
*@return*@throwsKeeperException
*@throwsInterruptedException*/
public Stat setData(String path,String data) throwsKeeperException, InterruptedException{
Stat stat= zookeeper.setData(path, data.getBytes(), -1);returnstat;
}/*** 删除节点
*@parampath
*@throwsInterruptedException
*@throwsKeeperException*/
public void deleteNode(String path) throwsInterruptedException, KeeperException{
zookeeper.delete(path,-1);
}/*** 获取创建时间
*@parampath
*@return*@throwsKeeperException
*@throwsInterruptedException*/
public String getCTime(String path) throwsKeeperException, InterruptedException{
Stat stat= zookeeper.exists(path, false);returnString.valueOf(stat.getCtime());
}/*** 获取某个路径下孩子的数量
*@parampath
*@return*@throwsKeeperException
*@throwsInterruptedException*/
public Integer getChildrenNum(String path) throwsKeeperException, InterruptedException{int childenNum = zookeeper.getChildren(path, false).size();returnchildenNum;
}/*** 关闭连接
*@throwsInterruptedException*/
public void closeConnection() throwsInterruptedException{if (zookeeper != null) {
zookeeper.close();
}
}public static void main(String[] args) throwsException {
BaseZookeeper zookeeper= newBaseZookeeper();
zookeeper.connectZookeeper("192.168.126.128:2181");
List children = zookeeper.getChildren("/");
System.out.println(children);
}
}