All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.andyshaox.zk.utils.ZooKeepers Maven / Gradle / Ivy

There is a newer version: 1.0.0.RELEASE
Show newest version
package com.github.andyshaox.zk.utils;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.NodeExistsException;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import com.github.andyshaox.zk.election.ZkMasterElection;
import com.google.common.base.Splitter;

/**
 * 
 * Title:
* Descript:
* Copyright: Copryright(c) Mar 28, 2018
* Encoding: UNIX UTF-8 * @author andy.shao * */ public final class ZooKeepers { private ZooKeepers() {} public static final ZooKeeper connect(String connectString , int sessionTimeOut) throws InterruptedException, IOException{ final CountDownLatch countDownLatch = new CountDownLatch(1); try { return new ZooKeeper(connectString , sessionTimeOut , (event)->{ final KeeperState keeperState = event.getState(); switch(keeperState) { case SyncConnected: countDownLatch.countDown(); break; default: break; } }); } finally { countDownLatch.await(); } } public static final void createPathRecursively(ZooKeeper zk , String rootPath) throws KeeperException, InterruptedException { List pathes = ZkMasterElection.computePaths(Splitter.on('/').omitEmptyStrings().trimResults().splitToList(rootPath)); for(String path : pathes) { Stat stat; try { stat = zk.exists(path , false); if(stat == null) zk.create(path , "".getBytes() , ZooDefs.Ids.OPEN_ACL_UNSAFE , CreateMode.PERSISTENT); } catch(NodeExistsException e) { // do nothing - because of you need ignore the path is already exists } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy