linux下tomcat管理

This commit is contained in:
F7575 2019-06-02 23:09:37 +08:00
parent 2e74d1eaef
commit 80ae273ba3
6 changed files with 94 additions and 75 deletions

View File

@ -1,11 +1,16 @@
package cn.keepbx.jpom.common.commander;
import cn.hutool.http.HttpRequest;
import cn.keepbx.jpom.BaseJpomApplication;
import cn.keepbx.jpom.common.commander.impl.LinuxTomcatCommander;
import cn.keepbx.jpom.common.commander.impl.WindowsTomcatCommander;
import cn.keepbx.jpom.model.data.TomcatInfoModel;
import cn.keepbx.jpom.system.JpomRuntimeException;
/**
* tomcat命令执行工具类
* @author LF
*/
public abstract class AbstractTomcatCommander {
private static AbstractTomcatCommander abstractTomcatCommander;
@ -28,5 +33,46 @@ public abstract class AbstractTomcatCommander {
return abstractTomcatCommander;
}
/**
* 执行tomcat命令
* @param tomcatInfoModel tomcat信息
* @param cmd 执行的命令包括start stop
* @return 返回tomcat启动结果
*/
public abstract String execCmd(TomcatInfoModel tomcatInfoModel, String cmd);
/**
* 检查tomcat状态
* @param tomcatInfoModel tomcat信息
* @param cmd 操作命令
* @return 状态结果
*/
protected String getStatus(TomcatInfoModel tomcatInfoModel, String cmd) {
String strReturn = "start".equals(cmd) ? "stopped" : "started";
int i = 0;
while (i < 10) {
int result = 0;
String url = String.format("http://127.0.0.1:%d/", tomcatInfoModel.getPort());
HttpRequest httpRequest = new HttpRequest(url);
httpRequest.setConnectionTimeout(3000); // 设置超时时间为3秒
try {
httpRequest.execute();
result = 1;
} catch (Exception ignored) {}
i++;
if ("start".equals(cmd) && result == 1) {
strReturn = "started";
break;
}
if ("stop".equals(cmd) && result == 0) {
strReturn = "stopped";
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
}
return strReturn;
}
}

View File

@ -1,69 +1,47 @@
package cn.keepbx.jpom.common.commander.impl;
import cn.hutool.http.HttpRequest;
import cn.keepbx.jpom.common.commander.AbstractTomcatCommander;
import cn.keepbx.jpom.model.data.TomcatInfoModel;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
/**
* tomcat的linux管理命令
* @author LF
*/
public class LinuxTomcatCommander extends AbstractTomcatCommander {
@Override
public String execCmd(TomcatInfoModel tomcatInfoModel, String cmd) {
String strReturn = "start".equals(cmd) ? "stopped" : "started";
// 拼接命令
String command = String.format("java -Djava.util.logging.config.file=\"%sconf/logging.properties\" " +
"-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager " +
"-Djdk.tls.ephemeralDHKeySize=2048" +
"-Djava.protocol.handler.pkgs=org.apache.catalina.webresources " +
"-Dignore.endorsed.dirs=\"%s\" " +
"-classpath \"%sbin/bootstrap.jar;%sbin/tomcat-juli.jar\" " +
"-Dcatalina.base=\"%s\" " +
"-Dcatalina.home=\"%s\" " +
"-Djava.io.tmpdir=\"%stemp/\" " +
String command = String.format("java -Djava.util.logging.config.file=%sconf/logging.properties " +
"-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager " +
"-Djdk.tls.ephemeralDHKeySize=2048 " +
"-Djava.protocol.handler.pkgs=org.apache.catalina.webresources " +
"-Dignore.endorsed.dirs=%s " +
"-classpath %sbin/bootstrap.jar:%sbin/tomcat-juli.jar " +
"-Dcatalina.base=%s " +
"-Dcatalina.home=%s " +
"-Djava.io.tmpdir=%stemp/ " +
"org.apache.catalina.startup.Bootstrap %s", tomcatInfoModel.getPath(), tomcatInfoModel.getPath(),
tomcatInfoModel.getPath(), tomcatInfoModel.getPath(), tomcatInfoModel.getPath(),
tomcatInfoModel.getPath(), tomcatInfoModel.getPath(), cmd);
try {
Process process = Runtime.getRuntime().exec(command, null, new File(tomcatInfoModel.getPath()));
// 执行命令
Process process = Runtime.getRuntime().exec(command);
process.getInputStream().close();
process.getErrorStream().close();
process.getOutputStream().close();
process.waitFor(5, TimeUnit.SECONDS);
// process.destroy();
process.destroy();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
// 检查tomcat是否已经启动或停止
int i = 0;
while (i < 10) {
int result = 0;
String url = String.format("http://127.0.0.1:%d/", tomcatInfoModel.getPort());
HttpRequest httpRequest = new HttpRequest(url);
httpRequest.setConnectionTimeout(3000); // 设置超时时间为3秒
try {
httpRequest.execute();
result = 1;
} catch (Exception ignored) {}
i++;
if ("start".equals(cmd) && result == 1) {
strReturn = "started";
break;
}
if ("stop".equals(cmd) && result == 0) {
strReturn = "stopped";
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
}
return strReturn;
// 查询操作结果并返回
return getStatus(tomcatInfoModel, cmd);
}
}

View File

@ -1,6 +1,5 @@
package cn.keepbx.jpom.common.commander.impl;
import cn.hutool.http.HttpRequest;
import cn.keepbx.jpom.common.commander.AbstractTomcatCommander;
import cn.keepbx.jpom.model.data.TomcatInfoModel;
@ -8,10 +7,19 @@ import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* tomcat的Windows管理命令
* @author LF
*/
public class WindowsTomcatCommander extends AbstractTomcatCommander {
/**
* windows下执行tomcat命令
* @param tomcatInfoModel tomcat信息
* @param cmd 执行的命令包括start stop
* @return 返回tomcat启动结果
*/
@Override
public String execCmd(TomcatInfoModel tomcatInfoModel, String cmd) {
String strReturn = "start".equals(cmd) ? "stopped" : "started";
// 拼接命令
String command = String.format("cmd /c java -Djava.util.logging.config.file=\"%sconf/logging.properties\" " +
@ -27,6 +35,7 @@ public class WindowsTomcatCommander extends AbstractTomcatCommander {
tomcatInfoModel.getPath(), tomcatInfoModel.getPath(), tomcatInfoModel.getPath(),
tomcatInfoModel.getPath(), tomcatInfoModel.getPath(), cmd);
try {
// 执行命令
Process process = Runtime.getRuntime().exec(command, null, new File(tomcatInfoModel.getPath()));
process.getInputStream().close();
@ -37,32 +46,8 @@ public class WindowsTomcatCommander extends AbstractTomcatCommander {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
// 检查tomcat是否已经启动或停止
int i = 0;
while (i < 10) {
int result = 0;
String url = String.format("http://127.0.0.1:%d/", tomcatInfoModel.getPort());
HttpRequest httpRequest = new HttpRequest(url);
httpRequest.setConnectionTimeout(3000); // 设置超时时间为3秒
try {
httpRequest.execute();
result = 1;
} catch (Exception ignored) {}
i++;
if ("start".equals(cmd) && result == 1) {
strReturn = "started";
break;
}
if ("stop".equals(cmd) && result == 0) {
strReturn = "stopped";
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
}
return strReturn;
// 查询操作结果并返回
return getStatus(tomcatInfoModel, cmd);
}
}

View File

@ -128,6 +128,8 @@ public class TomcatManageController extends BaseAgentController {
tomcatInfoModel.setId(SecureUtil.md5(DateUtil.now()));
tomcatInfoModel.setCreator(getUserName());
tomcatInfoModel.setCreateTime(DateUtil.now());
// 设置tomcat路径去除多余的符号
tomcatInfoModel.setPath(FileUtil.normalize(tomcatInfoModel.getPath()));
tomcatManageService.addItem(tomcatInfoModel);
return JsonMessage.getString(200, "保存成功");
} else {
@ -208,6 +210,8 @@ public class TomcatManageController extends BaseAgentController {
tomcatInfoModel.setModifyUser(getUserName());
tomcatInfoModel.setModifyTime(DateUtil.now());
// 设置tomcat路径去除多余的符号
tomcatInfoModel.setPath(FileUtil.normalize(tomcatInfoModel.getPath()));
tomcatManageService.updateItem(tomcatInfoModel);
return JsonMessage.getString(200, "修改成功");
} else {
@ -256,7 +260,11 @@ public class TomcatManageController extends BaseAgentController {
TomcatInfoModel tomcatInfoModel = tomcatManageService.getItem(id);
String result = AbstractTomcatCommander.getInstance().execCmd(tomcatInfoModel, "stop");
return JsonMessage.getString(200, "停止成功", result);
String msg = "停止成功";
if ("started".equals(result)) {
msg = "停止失败";
}
return JsonMessage.getString(200, msg, result);
}
/**

View File

@ -118,7 +118,9 @@ public class TomcatManageService extends BaseOperService<TomcatInfoModel> {
TomcatInfoModel tomcatInfoModel = getItem(id);
String body = tomcatCmd(tomcatInfoModel, "text/list");
String[] result = body.split("\r\n");
String[] result = body.replace("\r\n", "$")
.replace("\n", "$")
.split("\\$");
JSONArray jsonArray = new JSONArray();

View File

@ -78,8 +78,8 @@
cols: [[
{field: 'path', title: '项目路径', width: "25%"},
{field: 'status', title: '运行状态', width: "25%", templet: function (d) { return d.status == 'running' ? '已运行' : '已停止'; }},
{field: 'session', title: 'Session个数', width: "25%"},
{field: 'opreation', title: '操作', toolbar: '#toolbar_op'}
{field: 'session', title: 'Session个数', width: "20%"},
{field: 'opreation', title: '操作', width: "250", hide: false, toolbar: '#toolbar_op'}
]],
url: "./getTomcatProject",
loading: true,
@ -253,7 +253,7 @@
var tip = {
'stop': '确定停止项目' + data.path + '吗?',
'reload': '确定重启项目' + data.path + '吗?',
'undeploy': '注意,您正删除项目' + data.path + ',确定删除吗?',
'undeploy': '注意,您正删除项目' + data.path + ',确定删除吗?',
}
if (tip[op]) {