Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
79 lines (57 sloc) 1.93 KB
/**
*
*/
package com.sts.allprogtutorials.zk.utils;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import com.sts.allprogtutorials.zk.leaderelection.nodes.ProcessNode.ProcessNodeWatcher;
/**
* @author Sain Technology Solutions
*
*/
public class ZooKeeperService {
private ZooKeeper zooKeeper;
public ZooKeeperService(final String url, final ProcessNodeWatcher processNodeWatcher) throws IOException {
zooKeeper = new ZooKeeper(url, 3000, processNodeWatcher);
}
public String createNode(final String node, final boolean watch, final boolean ephimeral) {
String createdNodePath = null;
try {
final Stat nodeStat = zooKeeper.exists(node, watch);
if(nodeStat == null) {
createdNodePath = zooKeeper.create(node, new byte[0], Ids.OPEN_ACL_UNSAFE, (ephimeral ? CreateMode.EPHEMERAL_SEQUENTIAL : CreateMode.PERSISTENT));
} else {
createdNodePath = node;
}
} catch (KeeperException | InterruptedException e) {
throw new IllegalStateException(e);
}
return createdNodePath;
}
public boolean watchNode(final String node, final boolean watch) {
boolean watched = false;
try {
final Stat nodeStat = zooKeeper.exists(node, watch);
if(nodeStat != null) {
watched = true;
}
} catch (KeeperException | InterruptedException e) {
throw new IllegalStateException(e);
}
return watched;
}
public List<String> getChildren(final String node, final boolean watch) {
List<String> childNodes = null;
try {
childNodes = zooKeeper.getChildren(node, watch);
} catch (KeeperException | InterruptedException e) {
throw new IllegalStateException(e);
}
return childNodes;
}
}