TestMain.java
3.29 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.yoho.kisjob.demo;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.CuratorFrameworkFactory.Builder;
import org.apache.curator.framework.api.ACLProvider;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.google.common.base.Strings;
import com.yoho.kisjob.common.locator.ServiceLocator;
import com.yoho.kisjob.reg.zk.ZkConfig;
import com.yoho.kisjob.reg.zk.ZkExceptionHandler;
import com.yoho.kisjob.reg.zk.ZkHelper;
public class TestMain {
public static void main(String[] args) {
remove();
//start();
}
public static void start() {
ApplicationContext app = new ClassPathXmlApplicationContext("classpath:META-INF/spring/spring_registry.xml",
"classpath:META-INF/spring/spring_*.xml");
ServiceLocator.initApplicationContext(app);
}
private static void remove() {
ZkConfig config = new ZkConfig();
config.setNamespace("YohoJobs");
config.setServerLists("localhost:2181");
CuratorFramework client = initZkClient(config);
removePath(client, "/JobNodeGroups");
client.close();
}
private static void removePath(CuratorFramework client, String path) {
ZkHelper zkHelper = new ZkHelper(client);
if (!zkHelper.isExisted(path)) {
return;
}
List<String> children = zkHelper.getChildrenKeys(path);
if (children != null && !children.isEmpty()) {
for (String node : children) {
removePath(client, path + "/" + node);
}
}
zkHelper.remove(path);
}
private static CuratorFramework initZkClient(ZkConfig zkConfig) {
Builder builder = CuratorFrameworkFactory
.builder()
.connectString(zkConfig.getServerLists())
.retryPolicy(
new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(),
zkConfig.getMaxSleepTimeMilliseconds())).namespace(zkConfig.getNamespace());
if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
builder.sessionTimeoutMs(zkConfig.getSessionTimeoutMilliseconds());
}
if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
builder.connectionTimeoutMs(zkConfig.getConnectionTimeoutMilliseconds());
}
if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(
new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
@Override
public List<ACL> getAclForPath(final String path) {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
});
}
CuratorFramework client = builder.build();
client.start();
try {
client.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(),
TimeUnit.MILLISECONDS);
if (!client.getZookeeperClient().isConnected()) {
throw new KeeperException.OperationTimeoutException();
}
} catch (Exception ex) {
ZkExceptionHandler.handleException(ex);
}
return client;
}
}