Authored by qinchao

增加一个测试类

package com.monitor.nginxsync.service;
import com.google.common.base.Preconditions;
import com.monitor.common.ProjectConstant;
import com.monitor.model.response.BaseResponse;
import com.monitor.nginxsync.constant.Constants;
import com.monitor.nginxsync.model.CmdTaskInfo;
... ... @@ -8,6 +9,9 @@ import com.monitor.nginxsync.model.LogMsgResp;
import com.monitor.nginxsync.model.TaskInfo;
import com.monitor.nginxsync.nio.model.LogMsg;
import com.monitor.nginxsync.nio.service.LogService;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -19,6 +23,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayOutputStream;
/**
* Created by yoho on 2016/8/23.
... ... @@ -38,6 +44,40 @@ public class NginxSyncService {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/test")
public BaseResponse test() {
BaseResponse baseResponse=new BaseResponse();
String out="";
String error ="";
String command = "ssh master@"+ ProjectConstant.DEPLOY_IP+" "+"pwd";
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
//命令行处理
CommandLine commandline = CommandLine.parse(command);
//进行执行体
DefaultExecutor exec= new DefaultExecutor();
exec.setExitValues(null);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
exec.setStreamHandler(streamHandler);
exec.execute(commandline);//执行
out =outputStream.toString("gbk");
error =errorStream.toString("gbk");
DEBUG.info("nginxsync test out {} ,error {}",out,error);
} catch (Exception e) {
DEBUG.info("nginxsync exception {}",e);
baseResponse.setCode(201);
baseResponse.setMessage("error happens "+e.getCause());
return baseResponse;
}
baseResponse.setData("out is "+out+" ; error is "+error);
return baseResponse;
}
@RequestMapping(value = "/pushtask")
public BaseResponse pushTask(@RequestBody TaskInfo taskInfo) {
DEBUG.info("Recive nginx sync task {}", taskInfo);
... ...