|
|
package com.monitor.javaserver.client;
|
|
|
|
|
|
import com.model.JavaApiInfo;
|
|
|
import com.model.MObjectInfo;
|
|
|
import com.monitor.cmdb.service.IJavaApiInfoService;
|
|
|
import com.monitor.cmdb.service.IMObjectInfoService;
|
|
|
import com.monitor.javaserver.common.JavaApiStatics;
|
|
|
import com.monitor.javaserver.common.JavaApiTask;
|
|
|
import com.monitor.javaserver.common.JavaApiThreadFactory;
|
|
|
import com.monitor.javaserver.handle.IJavaApiHadnler;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
/**
|
|
|
* Created by fruwei on 2016/6/19.
|
|
|
*/
|
|
|
@Component
|
|
|
public class JavaApiClient {
|
|
|
Logger log = LoggerFactory.getLogger(JavaApiTask.class);
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private IJavaApiInfoService javaApiInfoService;
|
|
|
|
|
|
@Autowired
|
|
|
private IMObjectInfoService mObjectInfoService;
|
|
|
|
|
|
@Autowired
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
@Autowired
|
|
|
@Qualifier("influxDBJavaApiHandler")
|
|
|
private IJavaApiHadnler javaApiHadnler;
|
|
|
|
|
|
private Map<String, JavaApiInfo> javaApimap;
|
|
|
|
|
|
private MultiValueMap<Integer, MObjectInfo> mObjInfoMap;
|
|
|
|
|
|
private ExecutorService executorService;
|
|
|
|
|
|
private CompletionService<JavaApiStatics> completionService;
|
|
|
|
|
|
private long startTime;
|
|
|
private long endTime;
|
|
|
|
|
|
|
|
|
public void initClient() {
|
|
|
executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
|
|
|
new JavaApiThreadFactory());
|
|
|
|
|
|
completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
|
|
|
|
|
|
javaApimap = new ConcurrentHashMap<String, JavaApiInfo>();
|
|
|
mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectInfo>();
|
|
|
}
|
|
|
|
|
|
public void initApiMonitor() {
|
|
|
List<JavaApiInfo> apiInfos = javaApiInfoService.queryJavaApiInfo();
|
|
|
|
|
|
for (JavaApiInfo javaApiInfo : apiInfos) {
|
|
|
javaApimap.put(javaApiInfo.getApiName(), javaApiInfo);
|
|
|
}
|
|
|
|
|
|
//TODO 需要删选java服务
|
|
|
List<MObjectInfo> mObjectInfos = mObjectInfoService.queryMObjectsInfo();
|
|
|
|
|
|
for (MObjectInfo mObjInfo : mObjectInfos) {
|
|
|
mObjInfoMap.add(mObjInfo.getMoTypeId(), mObjInfo);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
startTime=System.currentTimeMillis();
|
|
|
int tastNum = 0;
|
|
|
for (JavaApiInfo javaApiInfo : javaApimap.values()) {
|
|
|
|
|
|
List<MObjectInfo> mObjectInfos = mObjInfoMap.get(javaApiInfo.getServiceType());
|
|
|
if (mObjectInfos == null) {
|
|
|
log.warn("type {} has no service object", javaApiInfo.getServiceType());
|
|
|
continue;
|
|
|
}
|
|
|
for (MObjectInfo mObjectInfo : mObjectInfos) {
|
|
|
completionService.submit(new JavaApiTask(restTemplate, javaApiInfo, mObjectInfo));
|
|
|
tastNum++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//wait for all rep end...
|
|
|
for (int i = 0; i < tastNum; i++) {
|
|
|
try {
|
|
|
Future<JavaApiStatics> rep = completionService.take();
|
|
|
handleResult(rep.get());
|
|
|
} catch (InterruptedException e) {
|
|
|
log.warn("completionService get rep interrupted ", e);
|
|
|
} catch (ExecutionException e) {
|
|
|
log.warn("completionService get rep failed ", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//close 线程池
|
|
|
executorService.shutdownNow();
|
|
|
endTime=System.currentTimeMillis();
|
|
|
}
|
|
|
|
|
|
|
|
|
public void handleResult(JavaApiStatics rep) {
|
|
|
javaApiHadnler.handler(rep);
|
|
|
}
|
|
|
|
|
|
|
|
|
public void init4Test() {
|
|
|
executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
|
|
|
new JavaApiThreadFactory());
|
|
|
|
|
|
completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
|
|
|
|
|
|
javaApimap = new ConcurrentHashMap<String, JavaApiInfo>();
|
|
|
mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectInfo>();
|
|
|
|
|
|
MObjectInfo mObj1 = new MObjectInfo();
|
|
|
mObj1.setMoId(1);
|
|
|
mObj1.setMoHostIp("192.168.102.205");
|
|
|
mObj1.setMoTags("8080");
|
|
|
mObj1.setMoName("gateway");
|
|
|
mObj1.setMoTypeId(1);
|
|
|
|
|
|
mObjInfoMap.add(1, mObj1);
|
|
|
|
|
|
// MObjectInfo mObj2 = new MObjectInfo();
|
|
|
// mObj2.setMoId(1);
|
|
|
// mObj2.setMoHostIp("192.168.102.207");
|
|
|
// mObj2.setMoTags("8080");
|
|
|
// mObj2.setMoName("gateway");
|
|
|
// mObj2.setMoTypeId(1);
|
|
|
//
|
|
|
// mObjInfoMap.add(1, mObj2);
|
|
|
|
|
|
int uid = 8041886;
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
JavaApiInfo javaApiInfo = new JavaApiInfo();
|
|
|
javaApiInfo.setApiName("order.get_" + i);
|
|
|
javaApiInfo.setApiReqMethod(0);
|
|
|
javaApiInfo.setApiToggle(0);
|
|
|
javaApiInfo.setApiUrl("/gateway/?debug=XYZ&method=app.Shopping.listCoupon&uid=" + (uid++));
|
|
|
javaApiInfo.setServiceType(1);
|
|
|
javaApimap.put("order.get_" + i, javaApiInfo);
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|