zookeeper Java基本操作
4300 ワード
package com.zookeeper.example;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class ZookeeperTest implements Watcher{
private static Logger LOGGER = LogManager.getLogger(ZookeeperTest. class);
private static ZooKeeper zoo;
private static final CountDownLatch CONNECTEDSIGNAL = new CountDownLatch(1);
public ZookeeperTest() {
try {
if(zoo == null){
zoo = new ZooKeeper("192.168.10.138:2181", 2000, this);
}
} catch (IOException e) {
LOGGER.error("zk :", e)
}
}
/**
*
* @throws InterruptedException
*/
public void close() throws InterruptedException {
zoo.close();
}
/**
* OPEN_ACL_UNSAFE ,
* PERSISTENT
* PERSISTENT_SEQUENTIAL
* EPHEMERAL
* EPHEMERAL_SEQUENTIAL
* @param path
* @param date
* @throws KeeperException
* @throws InterruptedException
* @throws IOException
*/
public void createNone (String path, String date) throws Exception {
if(exitNode(path, false)){
LOGGER.error(" ");
return;
}
zoo = new ZooKeeper("192.168.10.138:2181", 10000, null);
zoo.create(path, date.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
/**
*
* @param path
* @param watcher
* @return
* @throws Exception
*/
public boolean exitNode(String path, boolean watcher) throws Exception {
Stat stat = zoo.exists(path, watcher);
if (stat == null) {
return false;
}
return true;
}
/**
*
* @param path
* @param watcher ,
* @return
* @throws Exception
*/
public String getData(String path, boolean watcher) throws Exception {
if(!exitNode(path, false)){
LOGGER.warn(" ");
return "";
}
//stat
byte[] stat = zoo.getData(path, watcher, null);
return new String(stat,"UTF-8");
}
/**
* Watcher Server,
* @param watchedEvent
*/
@Override
public void process(WatchedEvent watchedEvent) {
LOGGER.warn(" :" + watchedEvent.getState() + watchedEvent.getPath());
if ( Event.KeeperState.SyncConnected == watchedEvent.getState() ) {
CONNECTEDSIGNAL.countDown();
}
}
/**
*
* @param path
* @param data
* @throws Exception
*/
public void setData(String path, String data) throws Exception {
Stat stat = zoo.setData(path, data.getBytes(), zoo.exists(path,true).getVersion());
LOGGER.warn(" ");
}
/**
*
* @param path
* @return
* @throws Exception
*/
public List getChildren(String path) throws Exception {
List children = zoo.getChildren(path, null);
return children;
}
/**
*
* @param path
* @throws Exception
*/
public void deleteNode(String path) throws Exception {
zoo.delete(path, zoo.exists(path,true).getVersion());
}
public static void main(String[] args) throws Exception {
ZookeeperTest zookeeperTest = new ZookeeperTest();
zookeeperTest.createNone("/mytest/cNode", "asdasdasd");
zookeeperTest.exitNode("/mytest/cNode",true);
String data = zookeeperTest.getData("/mytest/cNode",true);
zookeeperTest.createNone("/mytest/cNode/ss", "asdasdasd");
zookeeperTest.setData("/mytest/cNode","142554");
List children = zookeeperTest.getChildren("/mytest");
zookeeperTest.deleteNode("/mytest/cNode");
boolean flag = zookeeperTest.exitNode("/mytest/cNode",true);
zookeeperTest.close();
}
}