Skip to content

Commit

Permalink
抽取app manage
Browse files Browse the repository at this point in the history
  • Loading branch information
黄志磊 committed Dec 31, 2015
1 parent 8baa691 commit 65b9fd8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
@@ -1,9 +1,6 @@
package com.shinemo.mpush.tools.zk.listener.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
Expand All @@ -14,12 +11,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
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.ServerAppManage;
import com.shinemo.mpush.tools.zk.manage.ServerManage;

/**
Expand All @@ -30,8 +27,6 @@ public class ConnectionPathListener implements CallBack{

private static final Logger log = LoggerFactory.getLogger(ConnectionPathListener.class);

private static Map<String,ServerApp> holder = Maps.newConcurrentMap();

@Override
public void handler(CuratorFramework client, TreeCacheEvent event, String path) {
String data = "";
Expand All @@ -53,7 +48,6 @@ public void handler(CuratorFramework client, TreeCacheEvent event, String path)
public void initData(ServerManage manage) {
log.warn("start init app data");
_initData();
printAppList();
log.warn("end init app data");
}

Expand All @@ -63,37 +57,27 @@ private void _initData(){
for(String raw:rawData){
String fullPath = PathEnum.CONNECTION_SERVER_ALL_HOST.getPathByName(raw);
ServerApp app = getServerApp(fullPath);
holder.put(fullPath, app);
ServerAppManage.instance.addOrUpdate(fullPath, app);
}
}

private void dataRemove(ChildData data){
String path = data.getPath();
holder.remove(path);
printAppList();
ServerAppManage.instance.remove(path);
}

private void dataAddOrUpdate(ChildData data){
String path = data.getPath();
byte[] rawData = data.getData();
ServerApp serverApp = Jsons.fromJson(rawData, ServerApp.class);
holder.put(path, serverApp);
printAppList();
ServerAppManage.instance.addOrUpdate(path, serverApp);
}

private ServerApp getServerApp(String fullPath){
String rawApp = ZkUtil.instance.get(fullPath);
ServerApp app = Jsons.fromJson(rawApp, ServerApp.class);
return app;
}

private void printAppList(){
for(ServerApp app:holder.values()){
log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE));
}
}

public Collection<ServerApp> getAppList() {
return Collections.unmodifiableCollection(holder.values());
}

}
@@ -0,0 +1,51 @@
package com.shinemo.mpush.tools.zk.manage;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.shinemo.mpush.tools.zk.ServerApp;

/**
* 系统中当前可用的app列表
*
*/
public class ServerAppManage {

private static final Logger log = LoggerFactory.getLogger(ServerAppManage.class);

private static Map<String,ServerApp> holder = Maps.newConcurrentMap();

public static final ServerAppManage instance = new ServerAppManage();

private ServerAppManage() {
}

public void addOrUpdate(String fullPath,ServerApp app){
printAppList();
}

public void remove(String fullPath){
printAppList();
}

public void init(){
printAppList();
}

public Collection<ServerApp> getAppList() {
return Collections.unmodifiableCollection(holder.values());
}

private void printAppList(){
for(ServerApp app:holder.values()){
log.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE));
}
}
}
@@ -1,6 +1,5 @@
package com.shinemo.mpush.tools.zk.manage;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.curator.framework.CuratorFramework;
Expand All @@ -12,14 +11,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shinemo.mpush.tools.InetAddressUtil;
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.listener.ListenerDispatcher;
import com.shinemo.mpush.tools.zk.listener.impl.ConnectionPathListener;

public class ServerManage {

Expand Down Expand Up @@ -118,14 +115,4 @@ public ZkUtil getZkUtil(){
return zkUtil;
}

public ListenerDispatcher getDispatcher(){
return dispatcher;
}

public Collection<ServerApp> getServerApp(){
CallBack callback = dispatcher.getListener(PathEnum.CONNECTION_SERVER_ALL_HOST, InetAddressUtil.getInetAddress());
ConnectionPathListener listener = (ConnectionPathListener)callback;
return listener.getAppList();
}

}

0 comments on commit 65b9fd8

Please sign in to comment.