@Override
public void run() {
try {
Thread.sleep(2000);
logger.error("jobManager:" + jobManager + ", config:" + jobManager.getConfig());
HttpServer httpServer = HttpServer.create(
new InetSocketAddress(jobManager.getConfig().getHttpPort()), 0);
httpServer.createContext(
jobManager.getConfig().getHttpContext(),
new HttpHandler() {
private AtomicInteger checkCnt=new AtomicInteger(0);
private AtomicInteger configCnt=new AtomicInteger(0);
@Override
public void handle(final HttpExchange arg0)throws IOException {
try{
String qu=arg0.getRequestURI().getQuery();
if(qu==null) qu="command=check";
java.util.Map<String, String> quMap=new java.util.HashMap<String, String>();
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(qu, "&");
while (st.hasMoreTokens()) {
String pair = st.nextToken();
int pos = pair.indexOf('=');
if (pos != -1) {
String key = parseName(pair.substring(0, pos), sb);
String val = parseName(pair.substring(pos+1, pair.length()), sb);
quMap.put(key, java.net.URLDecoder.decode(val,"UTF-8"));
}
}
String command=quMap.get(COMMAND);
if(command==null||"".equals(command)) command=COMMAND_CHECK;
logger.warn("http agent node accept command "+command);
java.lang.StringBuilder exetime=new StringBuilder("the command:");
exetime.append(command);
exetime.append(" execute time:");
if(COMMAND_CHECK.equals(command)){
long start=System.currentTimeMillis();
handleCheck(arg0.getRequestBody(),arg0.getResponseBody(),new Callback(){
@Override
public void callback(int code,long len) throws IOException{
arg0.sendResponseHeaders(code, len);
}
});
long end=System.currentTimeMillis();
checkCnt.incrementAndGet();
exetime.append(end-start);
exetime.append(",execute count:");
exetime.append(checkCnt.get());
logger.warn(exetime.toString());
return;
}
if(COMMAND_MANAGE.equals(command)){
long start=System.currentTimeMillis();
String method=quMap.get(METHOD);
String level=quMap.get(LEVEL);
String instance=quMap.get(INSTANCE);
String entry=quMap.get(ENTRY);
String key=quMap.get("key");
String value=quMap.get("value");
handleManage(arg0.getRequestBody(),arg0.getResponseBody(),new Callback(){
@Override
public void callback(int code,long len) throws IOException{
arg0.sendResponseHeaders(code, len);
}
},method,level,instance,entry,key,value);
long end=System.currentTimeMillis();
exetime.append(end-start);
exetime.append(",execute count:");
logger.warn(exetime.toString());
return;
}
if(COMMAND_CONFIG.equals(command)){
long start=System.currentTimeMillis();
String instance=quMap.get(INSTANCE);
String type=quMap.get(TYPE);
String method=quMap.get(METHOD);
handleConfig(arg0.getRequestBody(),arg0.getResponseBody(),new Callback(){
@Override
public void callback(int code,long len) throws IOException{
arg0.sendResponseHeaders(code, len);
}
},type,method,instance);
long end=System.currentTimeMillis();
configCnt.incrementAndGet();
exetime.append(end-start);
exetime.append(",execute count:");
exetime.append(configCnt.get());
logger.warn(exetime.toString());
return;
}
logger.error("http agent node command:"+command+" was not supported");
}catch(Throwable t){
logger.error("http handle error", t);
}
}
});
httpServer.start();
} catch (Throwable e) {
logger.error("nested http server error", e);
}
}