/
PaasZooClient.java
48 lines (40 loc) · 1.13 KB
/
PaasZooClient.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
package com.sap.zookeeper;
import java.util.List;
import org.apache.zookeeper.KeeperException;
public class PaasZooClient extends ConnectionWatcher {
public List<String> getInstances(String serviceName) {
String path = "/PAAS/" + serviceName;
try {
if (zk.exists(path, false) != null) {
return zk.getChildren(path, false);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
public void removeInstances(String serviceName, int numInstances) {
List<String> instances = getInstances(serviceName);
int deleted = 0;
if (instances != null) {
for (String instance : instances) {
try {
String path = String.format("/PAAS/%s/%s", serviceName, instance);
zk.delete(path, -1);
deleted++;
System.out.println("Removed " + instance);
if (deleted == numInstances) {
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
}
}
}
System.out.println(String.format("%d service instances removed", deleted));
}
}