All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.ltsopensource.monitor.cmd.MDataSrv Maven / Gradle / Ivy
package com.github.ltsopensource.monitor.cmd;
import com.github.ltsopensource.core.commons.utils.CollectionUtils;
import com.github.ltsopensource.core.domain.monitor.*;
import com.github.ltsopensource.core.exception.LtsRuntimeException;
import com.github.ltsopensource.core.support.SystemClock;
import com.github.ltsopensource.core.support.bean.BeanCopier;
import com.github.ltsopensource.core.support.bean.BeanCopierFactory;
import com.github.ltsopensource.monitor.MonitorAppContext;
import com.github.ltsopensource.monitor.access.domain.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Robert HG ([email protected] ) on 3/11/16.
*/
public class MDataSrv {
private MonitorAppContext appContext;
private static final BeanCopier jobClientMDataBeanCopier
= BeanCopierFactory.createCopier(JobClientMData.class, JobClientMDataPo.class);
private static final BeanCopier jobTrackerMDataBeanCopier
= BeanCopierFactory.createCopier(JobTrackerMData.class, JobTrackerMDataPo.class);
private static final BeanCopier taskTrackerMDataBeanCopier
= BeanCopierFactory.createCopier(TaskTrackerMData.class, TaskTrackerMDataPo.class);
public MDataSrv(MonitorAppContext appContext) {
this.appContext = appContext;
}
public void addMDatas(MNode mNode, List mDatas) {
if (CollectionUtils.isEmpty(mDatas)) {
return;
}
switch (mNode.getNodeType()) {
case JOB_CLIENT:
addJobClientMData(mNode, mDatas);
break;
case JOB_TRACKER:
addJobTrackerMData(mNode, mDatas);
break;
case TASK_TRACKER:
addTaskTrackerMData(mNode, mDatas);
break;
default:
throw new LtsRuntimeException("Unsupport nodeType:" + mNode.getNodeType());
}
}
private void addJobClientMData(MNode mNode, List mDatas) {
List pos = new ArrayList(mDatas.size());
for (MData mData : mDatas) {
JobClientMDataPo po = new JobClientMDataPo();
jobClientMDataBeanCopier.copyProps((JobClientMData) mData, po);
po.setNodeType(mNode.getNodeType());
po.setIdentity(mNode.getIdentity());
po.setNodeGroup(mNode.getNodeGroup());
po.setGmtCreated(SystemClock.now());
pos.add(po);
}
appContext.getJobClientMAccess().insert(pos);
// 添加jvm监控数据
addJvmMData(mNode, mDatas);
}
private void addJobTrackerMData(MNode mNode, List mDatas) {
List pos = new ArrayList(mDatas.size());
for (MData mData : mDatas) {
JobTrackerMDataPo po = new JobTrackerMDataPo();
jobTrackerMDataBeanCopier.copyProps((JobTrackerMData) mData, po);
po.setNodeType(mNode.getNodeType());
po.setIdentity(mNode.getIdentity());
po.setNodeGroup(mNode.getNodeGroup());
po.setGmtCreated(SystemClock.now());
pos.add(po);
}
appContext.getJobTrackerMAccess().insert(pos);
// 添加jvm监控数据
addJvmMData(mNode, mDatas);
}
private void addTaskTrackerMData(MNode mNode, List mDatas) {
List pos = new ArrayList(mDatas.size());
for (MData mData : mDatas) {
TaskTrackerMDataPo po = new TaskTrackerMDataPo();
taskTrackerMDataBeanCopier.copyProps((TaskTrackerMData) mData, po);
po.setNodeType(mNode.getNodeType());
po.setIdentity(mNode.getIdentity());
po.setNodeGroup(mNode.getNodeGroup());
po.setGmtCreated(SystemClock.now());
pos.add(po);
}
appContext.getTaskTrackerMAccess().insert(pos);
// 添加jvm监控数据
addJvmMData(mNode, mDatas);
}
public void addJvmMData(MNode mNode, List mDatas) {
int size = mDatas.size();
List jvmGCDataPos = new ArrayList(size);
List jvmMemoryDataPos = new ArrayList(size);
List jvmThreadDataPos = new ArrayList(size);
for (MData mData : mDatas) {
JvmMData JVMMData = mData.getJvmMData();
Long timestamp = mData.getTimestamp();
// gc
JVMGCDataPo jvmgcDataPo = getDataPo(JVMMData.getGcMap(), JVMGCDataPo.class, mNode, timestamp);
jvmGCDataPos.add(jvmgcDataPo);
// memory
JVMMemoryDataPo jvmMemoryDataPo = getDataPo(JVMMData.getMemoryMap(), JVMMemoryDataPo.class, mNode, timestamp);
jvmMemoryDataPos.add(jvmMemoryDataPo);
// thread
JVMThreadDataPo jvmThreadDataPo = getDataPo(JVMMData.getThreadMap(), JVMThreadDataPo.class, mNode, timestamp);
jvmThreadDataPos.add(jvmThreadDataPo);
}
appContext.getJvmGCAccess().insert(jvmGCDataPos);
appContext.getJvmMemoryAccess().insert(jvmMemoryDataPos);
appContext.getJvmThreadAccess().insert(jvmThreadDataPos);
}
private static final Map CACHED_METHOD_MAP = new ConcurrentHashMap();
static {
cacheMethod(JVMGCDataPo.class);
cacheMethod(JVMMemoryDataPo.class);
cacheMethod(JVMThreadDataPo.class);
}
private static void cacheMethod(Class> clazz) {
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().startsWith("set")) {
CACHED_METHOD_MAP.put(clazz.getSimpleName() + "_" + method.getName().substring(3), method);
}
}
}
/**
* 根据Map得到 持久化对象
*/
private T getDataPo(Map dataMap, Class clazz,
MNode mNode, Long timestamp) {
try {
T data = clazz.newInstance();
if (CollectionUtils.isNotEmpty(dataMap)) {
for (Map.Entry entry : dataMap.entrySet()) {
Method method = CACHED_METHOD_MAP.get(clazz.getSimpleName() + "_" + entry.getKey());
if (method != null) {
String string = String.valueOf(entry.getValue());
Object value = entry.getValue();
Class> parameterType = method.getParameterTypes()[0];
if (parameterType == Long.class || parameterType == long.class) {
value = Long.valueOf(string);
} else if (parameterType == Integer.class || parameterType == int.class) {
value = Integer.valueOf(string);
} else if (parameterType == Float.class || parameterType == float.class) {
value = Float.valueOf(string);
} else if (parameterType == Double.class || parameterType == double.class) {
value = Double.valueOf(string);
} else if (parameterType == Short.class || parameterType == short.class) {
value = Short.valueOf(string);
} else if (parameterType == Boolean.class || parameterType == boolean.class) {
value = Boolean.valueOf(string);
} else if (parameterType == String.class) {
value = string;
}
// TODO others
method.invoke(data, value);
}
}
}
data.setNodeType(mNode.getNodeType());
data.setNodeGroup(mNode.getNodeGroup());
data.setIdentity(mNode.getIdentity());
data.setGmtCreated(SystemClock.now());
data.setTimestamp(timestamp);
return data;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}