/
ConnectionPathListener.java
74 lines (63 loc) · 2.42 KB
/
ConnectionPathListener.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
package com.shinemo.mpush.tools.zk.listener.impl;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.shinemo.mpush.tools.Jsons;
import com.shinemo.mpush.tools.zk.PathEnum;
import com.shinemo.mpush.tools.zk.ServerApp;
import com.shinemo.mpush.tools.zk.ZkUtil;
import com.shinemo.mpush.tools.zk.listener.CallBack;
import com.shinemo.mpush.tools.zk.manage.ServerManage;
/**
*注册的应用的发生变化
*
*/
public class ConnectionPathListener implements CallBack{
private static final Logger log = LoggerFactory.getLogger(ConnectionPathListener.class);
private List<ServerApp> appList = new ArrayList<ServerApp>();
@Override
public void handler(CuratorFramework client, TreeCacheEvent event, String path) {
String data = new String(event.getData().getData());
if (Type.NODE_ADDED == event.getType()) {
log.warn("ConnectionPathListener path:" + path + ", node Add"+","+data);
} else if (Type.NODE_REMOVED == event.getType()) {
log.warn("ConnectionPathListener path:" + path + ", node Remove"+","+data);
} else if (Type.NODE_UPDATED == event.getType()) {
log.warn("ConnectionPathListener path:" + path + "," + "node update"+","+data);
} else {
log.warn("ConnectionPathListener other path:" + path + "," + event.getType().name()+","+data);
}
}
@Override
public void initData(ServerManage manage) {
log.warn("start init app data");
getData();
printAppList();
log.warn("end init app data");
}
private void getData(){
//获取机器列表
List<String> rawData = ZkUtil.instance.getChildrenKeys(PathEnum.CONNECTION_SERVER_ALL_HOST.getPath());
List<ServerApp> newAppList = new ArrayList<ServerApp>();
for(String raw:rawData){
String rawApp = ZkUtil.instance.get(PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw));
ServerApp app = Jsons.fromJson(rawApp, ServerApp.class);
newAppList.add(app);
}
appList = newAppList;
}
public List<ServerApp> getAppList(){
return appList;
}
private void printAppList(){
for(ServerApp app:appList){
log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE));
}
}
}