ZooKeeper Java API 간단 한 예제
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class TestZooKeeper {
public static void main(String[] args) {
ZooKeeper zk = null;
try {
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println(" ZooKeeper...");
// ZooKeeper zk
String address = "192.168.1.226:2181";
int sessionTimeout = 3000;
zk = new ZooKeeper(address, sessionTimeout, new Watcher() {
//
public void process(WatchedEvent event) {
if (event.getType() == null || "".equals(event.getType())) {
return;
}
System.out.println(" " + event.getType() + " !");
}
});
System.out.println("ZooKeeper !");
// // register() Watcher
// Watcher watcher = new Watcher() {
//
// @Override
// public void process(WatchedEvent event) {
// // TODO Auto-generated method stub
// System.out.println(" : " + event.getType() + " !");
// }
// };
// zk.register(watcher);
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
// /tmp_root_path
// " /tmp_root_path"
// CreateMode.PERSISTENT
System.out.println(" /tmp_root_path...");
zk.create("/tmp_root_path", " /tmp_root_path".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(" /tmp_root_path !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
// /tmp_root_path/childPath1
// " /tmp_root_path/childPath1"
// CreateMode.PERSISTENT
System.out.println(" /tmp_root_path/childPath1...");
zk.create("/tmp_root_path/childPath1",
" /tmp_root_path/childPath1".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(" /tmp_root_path/childPath1 !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" /tmp_root_path ...");
System.out.println(zk.getChildren("/tmp_root_path", true));
System.out.println(" /tmp_root_path !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" /tmp_root_path/childPath1 ...");
zk.setData("/tmp_root_path/childPath1",
" /tmp_root_path/childPath1".getBytes(), -1);
System.out.println(" /tmp_root_path/childPath1 !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" ...");
System.out.println(zk.exists("/tmp_root_path", true));
System.out.println(" ");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
// /tmp_root_path/childPath2
// " /tmp_root_path/childPath2"
// CreateMode.PERSISTENT
System.out.println(" /tmp_root_path/childPath2...");
zk.create("/tmp_root_path/childPath2",
" /tmp_root_path/childPath2".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(" /tmp_root_path/childPath2 !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
// /tmp_root_path/childPath2
System.out.println(" /tmp_root_path/childPath2 ...");
System.out.println(new String(zk.getData(
"/tmp_root_path/childPath2", true, null)));
System.out.println(" /tmp_root_path/childPath2 !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" /tmp_root_path ...");
System.out.println(zk.getChildren("/tmp_root_path", true));
System.out.println(" /tmp_root_path !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" ...");
System.out.println(zk.exists("/tmp_root_path", true));
System.out.println(" ");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" /tmp_root_path/childPath1...");
zk.delete("/tmp_root_path/childPath1", -1);
System.out.println(" /tmp_root_path/childPath1 !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" ...");
System.out.println(zk.exists("/tmp_root_path", true));
System.out.println(" ");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" /tmp_root_path/childPath2...");
zk.delete("/tmp_root_path/childPath2", -1);
System.out.println(" /tmp_root_path/childPath2 !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" ...");
System.out.println(zk.exists("/tmp_root_path", true));
System.out.println(" ");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" /tmp_root_path...");
zk.delete("/tmp_root_path", -1);
System.out.println(" /tmp_root_path !");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
//
System.out.println(" ...");
System.out.println(zk.exists("/tmp_root_path", true));
System.out.println(" ");
Thread.currentThread().sleep(1000l);
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
} catch (IOException | KeeperException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//
if (zk != null) {
try {
zk.close();
System.out.println(" ZooKeeper !");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
출력 결 과 는 다음 과 같 습 니 다....
...
...
...
ZooKeeper...
ZooKeeper !
None !
...
...
...
...
/tmp_root_path...
/tmp_root_path !
...
...
...
...
/tmp_root_path/childPath1...
/tmp_root_path/childPath1 !
...
...
...
...
/tmp_root_path ...
[childPath1]
/tmp_root_path !
...
...
...
...
/tmp_root_path/childPath1 ...
/tmp_root_path/childPath1 !
...
...
...
...
...
2006956,2006956,1458958753172,1458958753172,0,1,0,0,29,1,2006957
...
...
...
...
/tmp_root_path/childPath2...
NodeChildrenChanged !
/tmp_root_path/childPath2 !
...
...
...
...
/tmp_root_path/childPath2 ...
/tmp_root_path/childPath2
/tmp_root_path/childPath2 !
...
...
...
...
/tmp_root_path ...
[childPath2, childPath1]
/tmp_root_path !
...
...
...
...
...
2006956,2006956,1458958753172,1458958753172,0,2,0,0,29,2,2006959
...
...
...
...
/tmp_root_path/childPath1...
NodeChildrenChanged !
/tmp_root_path/childPath1 !
...
...
...
...
...
2006956,2006956,1458958753172,1458958753172,0,3,0,0,29,1,2006960
...
...
...
...
/tmp_root_path/childPath2...
NodeDeleted !
/tmp_root_path/childPath2 !
...
...
...
...
...
2006956,2006956,1458958753172,1458958753172,0,4,0,0,29,0,2006961
...
...
...
...
/tmp_root_path...
NodeDeleted !
/tmp_root_path !
...
...
...
...
...
null
...
...
...
...
ZooKeeper !
왜 Watcher 가 그런 걸 감시 하 는 지 나중에 분석 해!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.