-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzclient5.java
78 lines (68 loc) · 2.33 KB
/
zclient5.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.myfirstjava.program;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
public class zclient5 {
private static byte[] toByteArray (final String i) throws IOException {
return i.getBytes();
}
public static void main(String[] args) throws Exception {
final CountDownLatch connectedSignal = new CountDownLatch(1);
ZooKeeper zoo = new ZooKeeper("localhost:2181",5000,null);
String path = "/clients/client5";
Scanner sc = new Scanner(System.in);
System.out.println("write something here: ");
String txt ;
txt = sc.nextLine();
byte[] ar = toByteArray(txt);
zoo.create(path, ar, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Watcher w = new Watcher() {
public void process(WatchedEvent event) {
try {
for (String s: zoo.getChildren("/clients",this, null)) {
s = "/clients" + "/"+ s;
if (zoo.exists(s,false) != null) {
System.out.println("this client there" + " " + s);
}else {
System.out.println("showing the disconnected clients"+ " " + s);
}
}
for (String s: zoo.getChildren("/clients",this, null)) {
System.out.println(s);
s = "/clients" + "/"+ s;
byte[] b = zoo.getData(s,this, null);
String data = new String(b, "UTF-8");
System.out.println(data);
}
for(String c: zoo.getChildren("/", this,null)) {
c = "/" + "/" + c;
if(zoo.exists("/",false) == null) {
System.out.println("root node does not have any nodes" + c);
}else {
System.out.println("root node is having nodes" + c);
}
}
zoo.getChildren("/clients",this, null);
}catch(Exception e) {
}
}
};
zoo.getChildren("/clients",w, null);
zoo.getChildren("/", w, null);
byte[] bn = zoo.getData(path, w, null);
String data = new String(bn, "UTF-8");
System.out.println(data);
int v = zoo.getSessionTimeout();
for (String s: zoo.getChildren("/clients",w, null)) {
if (v == 1000) {
System.out.println("all clients" + " "+ s);
}
}
connectedSignal.await();
}
}