提交 b4b46a1a 编写于 作者: O o2null

Merge branch 'feature/cmd' into 'develop'

Feature/cmd重写控制台命令到ddd,需要commons-cli支持

See merge request o2oa/o2oa!155
......@@ -186,5 +186,6 @@
"###nodeAgentEncrypt": "是否启用节点代理加密###",
"###quickStartWebApp": "是否使用快速应用部署###",
"###banner": "服务器控制台启动标识###",
"###autoStart": "是否自动启动###"
"###autoStart": "是否自动启动###",
"###eraseContentEnable": "是否允许使用擦除数据功能###"
}
\ No newline at end of file
current_dir=$(cd "$(dirname "$0")"; pwd);
current_dir=$(cd "$(dirname "$0")"; pwd);
if [ -d ${current_dir}/local/update ]; then
if [ ! -d ${current_dir}/commons ]; then
mkdir ${current_dir}/commons
......
......@@ -13,20 +13,22 @@ import java.util.concurrent.LinkedBlockingQueue;
import javax.naming.InitialContext;
import javax.ws.rs.core.MediaType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.MimeTypes;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.x.base.core.project.x_program_center;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.tools.BaseTools;
import com.x.base.core.project.tools.DefaultCharset;
import com.x.base.core.project.tools.Host;
import com.x.base.core.project.tools.NumberTools;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.MimeTypes;
public class Config {
private static Config INSTANCE;
......@@ -449,7 +451,14 @@ public class Config {
if (null == instance().version) {
synchronized (Config.class) {
if (null == instance().version) {
instance().version = BaseTools.readCfg(PATH_VERSION);
String text = BaseTools.readCfg(PATH_VERSION);
if (XGsonBuilder.isJson(text)) {
JsonObject obj = XGsonBuilder.instance().fromJson(text, JsonObject.class);
instance().version = obj.get("version").getAsString();
} else {
instance().version = text;
}
}
}
}
......
package com.x.base.core.project.config;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.tools.BaseTools;
import com.x.base.core.project.tools.DateTools;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
public class Node extends ConfigObject {
public static final Integer default_nodeAgentPort = 20010;
......@@ -75,6 +74,8 @@ public class Node extends ConfigObject {
private String banner;
@FieldDescribe("是否自动启动")
private Boolean autoStart;
@FieldDescribe("是否允许使用擦除数据功能")
private Boolean eraseContentEnable;
/* 20191009兼容centerServer */
protected void setCenter(CenterServer centerServer) {
......@@ -82,6 +83,11 @@ public class Node extends ConfigObject {
}
/* 20191009兼容centerServer end */
public Boolean getEraseContentEnable() {
return BooleanUtils.isNotFalse(eraseContentEnable);
}
public Boolean autoStart() {
return BooleanUtils.isNotFalse(autoStart);
}
......@@ -325,4 +331,6 @@ public class Node extends ConfigObject {
}
}
......@@ -13,6 +13,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -336,25 +337,28 @@ public class StringTools {
public static boolean matchWildcard(String str, String pattern) {
return Objects.toString(str, "").matches(Objects.toString(pattern, "").replace("?", ".?").replace("*", ".*?"));
// if (StringUtils.isNotEmpty(str) && StringUtils.isNotEmpty(pattern) && StringUtils.contains(pattern, "*")) {
// if (StringUtils.equals(pattern, "*")) {
// return true;
// }
// if (StringUtils.startsWith(pattern, "*")) {
// return StringUtils.endsWith(str, StringUtils.substringAfter(pattern, "*"));
// }
// if (StringUtils.endsWith(pattern, "*")) {
// return StringUtils.startsWith(str, StringUtils.substringBeforeLast(pattern, "*"));
// }
// String[] parts = StringUtils.split(pattern, "*", 2);
// if (StringUtils.startsWith(str, parts[0]) && StringUtils.endsWith(str, parts[1])) {
// return true;
// } else {
// return false;
// }
// } else {
// return StringUtils.equals(str, pattern);
// }
// if (StringUtils.isNotEmpty(str) && StringUtils.isNotEmpty(pattern) &&
// StringUtils.contains(pattern, "*")) {
// if (StringUtils.equals(pattern, "*")) {
// return true;
// }
// if (StringUtils.startsWith(pattern, "*")) {
// return StringUtils.endsWith(str, StringUtils.substringAfter(pattern, "*"));
// }
// if (StringUtils.endsWith(pattern, "*")) {
// return StringUtils.startsWith(str, StringUtils.substringBeforeLast(pattern,
// "*"));
// }
// String[] parts = StringUtils.split(pattern, "*", 2);
// if (StringUtils.startsWith(str, parts[0]) && StringUtils.endsWith(str,
// parts[1])) {
// return true;
// } else {
// return false;
// }
// } else {
// return StringUtils.equals(str, pattern);
// }
}
public static List<String> includesExcludesWithWildcard(List<String> list, Collection<String> includes,
......@@ -450,4 +454,66 @@ public class StringTools {
return false;
}
// copy from /org/apache/tools/ant/types/Commandline.java
public static String[] translateCommandline(String toProcess) throws Exception {
if (toProcess == null || toProcess.isEmpty()) {
// no command? no string
return new String[0];
}
// parse with a simple finite state machine
final int normal = 0;
final int inQuote = 1;
final int inDoubleQuote = 2;
int state = normal;
final StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true);
final ArrayList<String> result = new ArrayList<>();
final StringBuilder current = new StringBuilder();
boolean lastTokenHasBeenQuoted = false;
while (tok.hasMoreTokens()) {
String nextTok = tok.nextToken();
switch (state) {
case inQuote:
if ("\'".equals(nextTok)) {
lastTokenHasBeenQuoted = true;
state = normal;
} else {
current.append(nextTok);
}
break;
case inDoubleQuote:
if ("\"".equals(nextTok)) {
lastTokenHasBeenQuoted = true;
state = normal;
} else {
current.append(nextTok);
}
break;
default:
if ("\'".equals(nextTok)) {
state = inQuote;
} else if ("\"".equals(nextTok)) {
state = inDoubleQuote;
} else if (" ".equals(nextTok)) {
if (lastTokenHasBeenQuoted || current.length() > 0) {
result.add(current.toString());
current.setLength(0);
}
} else {
current.append(nextTok);
}
lastTokenHasBeenQuoted = false;
break;
}
}
if (lastTokenHasBeenQuoted || current.length() > 0) {
result.add(current.toString());
}
if (state == inQuote || state == inDoubleQuote) {
throw new Exception("unbalanced quotes in " + toProcess);
}
return result.toArray(new String[result.size()]);
}
}
\ No newline at end of file
......@@ -46,6 +46,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
......@@ -58,6 +59,7 @@
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<phase>verify</phase>
......
......@@ -26,18 +26,23 @@ public class CommandFactory {
public static final Pattern stop_pattern = Pattern
.compile("^ {0,}stop {0,}(data|storage|center|application|web|all|) {0,}$", Pattern.CASE_INSENSITIVE);
public static final Pattern dump_path_pattern = Pattern.compile("^ {0,}dump (data|storage) path (.+) (.+)$",
Pattern.CASE_INSENSITIVE);
// public static final Pattern dump_path_pattern = Pattern.compile("^ {0,}dump
// (data|storage) path (.+) (.+)$",
// Pattern.CASE_INSENSITIVE);
public static final Pattern dump_pattern = Pattern.compile("^ {0,}dump {0,}(data|storage) (.+)$",
Pattern.CASE_INSENSITIVE);
// public static final Pattern dump_pattern = Pattern.compile("^ {0,}dump
// {0,}(data|storage) (.+)$",
// Pattern.CASE_INSENSITIVE);
public static final Pattern restore_path_pattern = Pattern
.compile("^ {0,}restore {1,}(data|storage) {1,}path {1,}(.+) {1,}(.+)$", Pattern.CASE_INSENSITIVE);
// public static final Pattern restore_path_pattern = Pattern
// .compile("^ {0,}restore {1,}(data|storage) {1,}path {1,}(.+) {1,}(.+)$",
// Pattern.CASE_INSENSITIVE);
public static final Pattern restore_pattern = Pattern.compile(
"^ {0,}restore {0,}(data|storage) {0,}([2][0][1-9][0-9][0-1][0-9][0-3][0-9][0-5][0-9][0-5][0-9][0-5][0-9]) (.+)$",
Pattern.CASE_INSENSITIVE);
// public static final Pattern restore_pattern = Pattern.compile(
// "^ {0,}restore {0,}(data|storage)
// {0,}([2][0][1-9][0-9][0-1][0-9][0-3][0-9][0-5][0-9][0-5][0-9][0-5][0-9])
// (.+)$",
// Pattern.CASE_INSENSITIVE);
public static final Pattern help_pattern = Pattern.compile("^ {0,}help {0,}$", Pattern.CASE_INSENSITIVE);
......@@ -51,37 +56,12 @@ public class CommandFactory {
public static final Pattern setPassword_pattern = Pattern.compile("^ {0,}setPassword (.+) (.+)$",
Pattern.CASE_INSENSITIVE);
public static final Pattern erase_content_pattern = Pattern.compile("^ {0,}erase content (cms|pp|bbs|log) (.+)$",
Pattern.CASE_INSENSITIVE);
public static final Pattern compact_data_pattern = Pattern.compile("^ {0,}compact data (.+)$",
Pattern.CASE_INSENSITIVE);
public static final Pattern convert_dataItem_pattern = Pattern.compile("^ {0,}convert dataItem (.+)$",
Pattern.CASE_INSENSITIVE);
public static final Pattern show_os_pattern = Pattern.compile("^ {0,}show os ([1-9][0-9]*) ([1-9][0-9]*) {0,}$",
Pattern.CASE_INSENSITIVE);
public static final Pattern show_memory_pattern = Pattern
.compile("^ {0,}show memory ([1-9][0-9]*) ([1-9][0-9]*) {0,}$", Pattern.CASE_INSENSITIVE);
public static final Pattern show_thread_pattern = Pattern
.compile("^ {0,}show thread ([1-9][0-9]*) ([1-9][0-9]*) {0,}$", Pattern.CASE_INSENSITIVE);
public static final Pattern show_cpu_pattern = Pattern.compile("^ {0,}show cpu ([1-9][0-9]*) ([1-9][0-9]*) {0,}$",
public static final Pattern debugDesignDetail_pattern = Pattern.compile("^(\\s*)ddd(\\s*)",
Pattern.CASE_INSENSITIVE);
public static final Pattern show_http_thread_pattern = Pattern
.compile("^ {0,}show http thread ([1-9][0-9]*) ([1-9][0-9]*) {0,}$", Pattern.CASE_INSENSITIVE);
// public static final Pattern show_dataSource_pattern = Pattern
// .compile("^ {0,}show dataSource ([1-9][0-9]*) ([1-9][0-9]*) {0,}$", Pattern.CASE_INSENSITIVE);
public static final Pattern stack_pattern = Pattern.compile("^ {0,}stack ([1-9][0-9]*) ([1-9][0-9]*) (.+)$",
Pattern.CASE_INSENSITIVE);
public static final Pattern heapDump_pattern = Pattern.compile("^ {0,}heap dump (.+)$", Pattern.CASE_INSENSITIVE);
public static void printStartHelp() {
try {
printStartImage();
......@@ -104,45 +84,49 @@ public class CommandFactory {
public static void printHelpLine() {
try {
String help = "";
help += " help" + "\t\t\t\t\t\t\t" + "show useage message.";
help += StringUtils.LF;
help += " start|stop [all]" + "\t\t\t\t\t" + "start stop all enable server.";
help += StringUtils.LF;
help += " start|stop data" + "\t\t\t\t\t" + "start stop data server.";
help += StringUtils.LF;
help += " start|stop storage" + "\t\t\t\t\t" + "start stop storage server.";
help += StringUtils.LF;
help += " start|stop center" + "\t\t\t\t\t" + "start stop center server.";
help += StringUtils.LF;
help += " start|stop application" + "\t\t\t\t\t" + "start stop application server.";
help += StringUtils.LF;
help += " start|stop web" + "\t\t\t\t\t\t" + "start stop web server.";
help += " help show useage message.";
help += StringUtils.LF;
help += " dump data (passwd)" + "\t\t\t\t\t" + "dump data from database.";
help += " start|stop [all] start stop all enable server.";
help += StringUtils.LF;
help += " dump storage (passwd)" + "\t\t\t\t\t" + "dump storage from database,file.";
help += " start|stop data start stop data server.";
help += StringUtils.LF;
help += " restore data yyyyMMddHHmmss (passwd)" + "\t\t\t" + "restore data to database.";
help += " start|stop storage start stop storage server.";
help += StringUtils.LF;
help += " restore storage yyyyMMddHHmmss (passwd)" + "\t\t" + "restore storage to database,file.";
help += " start|stop center start stop center server.";
help += StringUtils.LF;
help += " setPassword (oldpasswd) (newpasswd)" + "\t\t\t" + "change initial manager password.";
// help += StringUtils.LF;
// help += " update (backup) (latest) (passwd)" + "\t\t\t" + "upgrade to new version.";
help += " start|stop application start stop application server.";
help += StringUtils.LF;
help += " update file (path) (backup) (passwd)" + "\t\t\t" + "upgrade to new version from local zip file.";
help += " start|stop web start stop web server.";
help += StringUtils.LF;
help += " compact data (passwd)" + "\t\t\t\t\t" + "compact local h2 repository database.";
// help += " dump data (passwd) dump data from database.";
// help += StringUtils.LF;
// help += " dump storage (passwd) dump storage from database,file.";
// help += StringUtils.LF;
// help += " restore data yyyyMMddHHmmss (passwd)" + "\t\t\t" + "restore data to
// database.";
// help += StringUtils.LF;
// help += " restore storage yyyyMMddHHmmss (passwd)" + "\t\t" + "restore
// storage to database,file.";
// help += StringUtils.LF;
help += " setPassword (oldpasswd) (newpasswd) change initial manager password.";
help += StringUtils.LF;
help += " erase content (cms|pp|bbs|log) (passwd)" + "\t\t" + "remove all data except design.";
help += " update file (path) (backup) (passwd) upgrade to new version from local zip file.";
help += StringUtils.LF;
help += " create encrypt key" + "\t\t\t\t\t" + "create random RSA key.";
// help += " compact data (passwd) compact local h2 repository database.";
// help += StringUtils.LF;
// help += " erase content (cms|pp|bbs|log) (passwd)" + "\t\t" + "remove all
// data except design.";
// help += StringUtils.LF;
help += " create encrypt key create random RSA key.";
// help += StringUtils.LF;
// help += " show (os|cpu|memory|thread) interval repeat" + "\t\t" + "show
// operating system infomation.";
help += StringUtils.LF;
help += " show (os|cpu|memory|thread) interval repeat" + "\t\t" + "show operating system infomation.";
help += " version show available update version.";
help += StringUtils.LF;
help += " version " + "\t\t\t\t\t\t" + "show available update version.";
help += " exit exit after stop.";
help += StringUtils.LF;
help += " exit" + "\t\t\t\t\t\t\t" + "exit after stop.";
help += " ddd -<argument> option debug design detail command with argument.";
help += StringUtils.LF;
System.out.println(help);
} catch (Exception e) {
......
......@@ -16,7 +16,7 @@ import org.quartz.JobExecutionException;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.server.console.action.ActionDumpData;
import com.x.server.console.action.DumpData;
public class DumpDataTask implements Job {
......@@ -26,8 +26,8 @@ public class DumpDataTask implements Job {
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
try {
logger.print("schedule dump data task start.");
ActionDumpData action = new ActionDumpData();
action.execute(Config.currentNode().dumpData().path(), Config.token().getPassword());
DumpData action = new DumpData();
action.execute(Config.currentNode().dumpData().path());
if (Config.currentNode().dumpData().size() > 0) {
File dir = new File(Config.base(), "local/dump");
List<File> list = new ArrayList<>();
......
......@@ -16,7 +16,7 @@ import org.quartz.JobExecutionException;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.server.console.action.ActionDumpStorage;
import com.x.server.console.action.DumpStorage;
public class DumpStorageTask implements Job {
......@@ -26,8 +26,8 @@ public class DumpStorageTask implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
logger.print("schedule dump storage task start.");
ActionDumpStorage action = new ActionDumpStorage();
action.execute(Config.currentNode().dumpStorage().path(), Config.token().getPassword());
DumpStorage action = new DumpStorage();
action.execute(Config.currentNode().dumpStorage().path());
if (Config.currentNode().dumpStorage().size() > 0) {
File dir = new File(Config.base(), "local/dump");
List<File> list = new ArrayList<>();
......
......@@ -12,10 +12,8 @@ import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.FileLock;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.regex.Matcher;
......@@ -35,25 +33,11 @@ import com.x.base.core.project.config.DataServer;
import com.x.base.core.project.config.StorageServer;
import com.x.base.core.project.config.WebServer;
import com.x.base.core.project.tools.DefaultCharset;
import com.x.server.console.action.ActionCompactData;
import com.x.base.core.project.tools.StringTools;
import com.x.server.console.action.ActionConfig;
import com.x.server.console.action.ActionCreateEncryptKey;
import com.x.server.console.action.ActionDumpData;
import com.x.server.console.action.ActionDumpStorage;
import com.x.server.console.action.ActionEraseContentBbs;
import com.x.server.console.action.ActionEraseContentCms;
import com.x.server.console.action.ActionEraseContentLog;
import com.x.server.console.action.ActionEraseContentProcessPlatform;
import com.x.server.console.action.ActionHeapDump;
import com.x.server.console.action.ActionRestoreData;
import com.x.server.console.action.ActionRestoreStorage;
import com.x.server.console.action.ActionDebugDesignDetail;
import com.x.server.console.action.ActionSetPassword;
import com.x.server.console.action.ActionShowCpu;
import com.x.server.console.action.ActionShowHttpThread;
import com.x.server.console.action.ActionShowMemory;
import com.x.server.console.action.ActionShowOs;
import com.x.server.console.action.ActionShowThread;
import com.x.server.console.action.ActionStack;
import com.x.server.console.action.ActionUpdateFile;
import com.x.server.console.action.ActionVersion;
import com.x.server.console.log.LogTools;
......@@ -80,8 +64,8 @@ public class Main {
throw new Exception("无法找到当前节点,请检查config/node_{name}.json与local/node.cfg文件内容中的名称是否一致.");
}
final LinkedBlockingQueue<String> commandQueue = new LinkedBlockingQueue<>();
// try (PipedInputStream pipedInput = new PipedInputStream();
// PipedOutputStream pipedOutput = new PipedOutputStream(pipedInput)) {
// try (PipedInputStream pipedInput = new PipedInputStream();
// PipedOutputStream pipedOutput = new PipedOutputStream(pipedInput)) {
new Thread() {
/* 文件中的命令输出到解析器 */
public void run() {
......@@ -123,8 +107,9 @@ public class Main {
/** 在linux环境中当前端console窗口关闭后会导致可以立即read到一个null的input值 */
if (null != cmd) {
commandQueue.put(cmd);
continue;
}
Thread.sleep(4000);
Thread.sleep(5000);
}
} catch (Exception e) {
System.out.println("console input closed!");
......@@ -157,152 +142,110 @@ public class Main {
continue;
}
matcher = CommandFactory.show_os_pattern.matcher(cmd);
if (matcher.find()) {
showOs(matcher.group(1), matcher.group(2));
continue;
}
matcher = CommandFactory.show_cpu_pattern.matcher(cmd);
if (matcher.find()) {
showCpu(matcher.group(1), matcher.group(2));
continue;
}
matcher = CommandFactory.stack_pattern.matcher(cmd);
if (matcher.find()) {
stack(matcher.group(1), matcher.group(2), matcher.group(3));
continue;
}
matcher = CommandFactory.heapDump_pattern.matcher(cmd);
if (matcher.find()) {
heapDump(matcher.group(1));
continue;
}
matcher = CommandFactory.show_memory_pattern.matcher(cmd);
if (matcher.find()) {
showMemory(matcher.group(1), matcher.group(2));
continue;
}
matcher = CommandFactory.show_thread_pattern.matcher(cmd);
if (matcher.find()) {
showThread(matcher.group(1), matcher.group(2));
continue;
}
matcher = CommandFactory.show_http_thread_pattern.matcher(cmd);
if (matcher.find()) {
showHttpThread(matcher.group(1), matcher.group(2));
continue;
}
matcher = CommandFactory.start_pattern.matcher(cmd);
if (matcher.find()) {
switch (matcher.group(1)) {
case "application":
startApplicationServer();
break;
case "center":
startCenterServer();
break;
case "web":
startWebServer();
break;
case "storage":
startStorageServer();
break;
case "data":
startDataServer();
break;
default:
startAll();
break;
case "application":
startApplicationServer();
break;
case "center":
startCenterServer();
break;
case "web":
startWebServer();
break;
case "storage":
startStorageServer();
break;
case "data":
startDataServer();
break;
default:
startAll();
break;
}
continue;
}
matcher = CommandFactory.stop_pattern.matcher(cmd);
if (matcher.find()) {
switch (matcher.group(1)) {
case "application":
stopApplicationServer();
break;
case "center":
stopCenterServer();
break;
case "web":
stopWebServer();
break;
case "storage":
stopStorageServer();
break;
case "data":
stopDataServer();
break;
default:
stopAll();
break;
}
continue;
}
matcher = CommandFactory.dump_path_pattern.matcher(cmd);
if (matcher.find()) {
switch (matcher.group(1)) {
case "data":
dumpData(matcher.group(2), matcher.group(3));
break;
case "storage":
dumpStorage(matcher.group(2), matcher.group(3));
break;
default:
break;
}
continue;
}
matcher = CommandFactory.dump_pattern.matcher(cmd);
if (matcher.find()) {
switch (matcher.group(1)) {
case "data":
dumpData("", matcher.group(2));
break;
case "storage":
dumpStorage("", matcher.group(2));
break;
default:
break;
}
continue;
}
matcher = CommandFactory.restore_path_pattern.matcher(cmd);
if (matcher.find()) {
switch (matcher.group(1)) {
case "data":
resotreDataPath(matcher.group(2), matcher.group(3));
break;
case "storage":
resotreStoragePath(matcher.group(2), matcher.group(3));
break;
default:
break;
}
continue;
}
matcher = CommandFactory.restore_pattern.matcher(cmd);
if (matcher.find()) {
switch (matcher.group(1)) {
case "data":
resotreData(matcher.group(2), matcher.group(3));
break;
case "storage":
resotreStorage(matcher.group(2), matcher.group(3));
break;
default:
break;
case "application":
stopApplicationServer();
break;
case "center":
stopCenterServer();
break;
case "web":
stopWebServer();
break;
case "storage":
stopStorageServer();
break;
case "data":
stopDataServer();
break;
default:
stopAll();
break;
}
continue;
}
// matcher = CommandFactory.dump_path_pattern.matcher(cmd);
// if (matcher.find()) {
// switch (matcher.group(1)) {
// case "data":
// dumpData(matcher.group(2), matcher.group(3));
// break;
// case "storage":
// dumpStorage(matcher.group(2), matcher.group(3));
// break;
// default:
// break;
// }
// continue;
// }
// matcher = CommandFactory.dump_pattern.matcher(cmd);
// if (matcher.find()) {
// switch (matcher.group(1)) {
// case "data":
// dumpData("", matcher.group(2));
// break;
// case "storage":
// dumpStorage("", matcher.group(2));
// break;
// default:
// break;
// }
// continue;
// }
// matcher = CommandFactory.restore_path_pattern.matcher(cmd);
// if (matcher.find()) {
// switch (matcher.group(1)) {
// case "data":
// resotreDataPath(matcher.group(2), matcher.group(3));
// break;
// case "storage":
// resotreStoragePath(matcher.group(2), matcher.group(3));
// break;
// default:
// break;
// }
// continue;
// }
// matcher = CommandFactory.restore_pattern.matcher(cmd);
// if (matcher.find()) {
// switch (matcher.group(1)) {
// case "data":
// resotreData(matcher.group(2), matcher.group(3));
// break;
// case "storage":
// resotreStorage(matcher.group(2), matcher.group(3));
// break;
// default:
// break;
// }
// continue;
// }
matcher = CommandFactory.help_pattern.matcher(cmd);
if (matcher.find()) {
CommandFactory.printHelp();
......@@ -335,37 +278,15 @@ public class Main {
}
}
matcher = CommandFactory.erase_content_pattern.matcher(cmd);
if (matcher.find()) {
switch (matcher.group(1)) {
case "pp":
eraseContentProcessPlatform(matcher.group(2));
break;
case "cms":
eraseContentCms(matcher.group(2));
break;
case "log":
eraseContentLog(matcher.group(2));
break;
case "bbs":
eraseContentBbs(matcher.group(2));
break;
default:
break;
}
continue;
}
matcher = CommandFactory.compact_data_pattern.matcher(cmd);
matcher = CommandFactory.create_encrypt_key_pattern.matcher(cmd);
if (matcher.find()) {
compactData(matcher.group(1));
createEncryptKey(matcher.group(1));
continue;
}
matcher = CommandFactory.create_encrypt_key_pattern.matcher(cmd);
matcher = CommandFactory.debugDesignDetail_pattern.matcher(cmd);
if (matcher.find()) {
createEncryptKey(matcher.group(1));
debugDesignDetail(cmd);
continue;
}
......@@ -396,51 +317,8 @@ public class Main {
}
return true;
}
private static boolean showOs(String interval, String repeat) {
try {
return new ActionShowOs().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
private static boolean showCpu(String interval, String repeat) {
try {
return new ActionShowCpu().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
private static boolean showMemory(String interval, String repeat) {
try {
return new ActionShowMemory().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
private static boolean showThread(String interval, String repeat) {
try {
return new ActionShowThread().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
private static boolean showHttpThread(String interval, String repeat) {
try {
return new ActionShowHttpThread().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
private static boolean createEncryptKey(String password) {
try {
......@@ -673,70 +551,81 @@ public class Main {
}
}
private static boolean dumpData(String path, String password) {
try {
return (new ActionDumpData()).execute(path, password);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
private static boolean dumpStorage(String path, String password) {
try {
return (new ActionDumpStorage()).execute(path, password);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
private static void resotreData(String dateString, String password) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = format.parse(dateString);
File file = new File(Config.base(), "local/dump/dumpData_" + format.format(date));
if (file.exists() && file.isDirectory()) {
(new ActionRestoreData()).execute(date, password);
} else {
System.out.println("directory " + file.getAbsolutePath() + " not existed.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void resotreDataPath(String path, String password) {
try {
(new ActionRestoreData()).execute(path, password);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void resotreStorage(String dateString, String password) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = format.parse(dateString);
File file = new File(Config.base(), "local/dump/dumpStorage_" + format.format(date));
if (file.exists() && file.isDirectory()) {
ActionRestoreStorage restoreStorage = new ActionRestoreStorage();
restoreStorage.execute(date, password);
} else {
System.out.println("directory " + file.getAbsolutePath() + " not existed.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void resotreStoragePath(String path, String password) {
try {
new ActionRestoreStorage().execute(path, password);
} catch (Exception e) {
e.printStackTrace();
}
}
// private static boolean dumpData(String path, String password) {
// try {
// return (new ActionDumpData()).execute(path, password);
// } catch (Exception e) {
// e.printStackTrace();
// }
// return true;
// }
//
// private static boolean dumpStorage(String path, String password) {
// try {
// return (new ActionDumpStorage()).execute(path, password);
// } catch (Exception e) {
// e.printStackTrace();
// }
// return true;
// }
// private static void resotreData(String dateString, String password) {
// try {
// SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
// Date date = format.parse(dateString);
// File file = new File(Config.base(), "local/dump/dumpData_" + format.format(date));
// if (file.exists() && file.isDirectory()) {
// (new ActionRestoreData()).execute(date, password);
// } else {
// System.out.println("directory " + file.getAbsolutePath() + " not existed.");
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// private static void resotreDataPath(String path, String password) {
// try {
// (new ActionRestoreData()).execute(path, password);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// private static void resotreStorage(String dateString, String password) {
// try {
// SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
// Date date = format.parse(dateString);
// File file = new File(Config.base(), "local/dump/dumpStorage_" + format.format(date));
// if (file.exists() && file.isDirectory()) {
// ActionRestoreStorage restoreStorage = new ActionRestoreStorage();
// restoreStorage.execute(date, password);
// } else {
// System.out.println("directory " + file.getAbsolutePath() + " not existed.");
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
private static void debugDesignDetail(String cmd) {
try {
String[] args = StringTools.translateCommandline(cmd);
args = Arrays.copyOfRange(args, 1, args.length);
ActionDebugDesignDetail action = new ActionDebugDesignDetail();
action.execute(args);
} catch (Exception e) {
e.printStackTrace();
}
}
// private static void resotreStoragePath(String path, String password) {
// try {
// new ActionRestoreStorage().execute(path, password);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
private static void createTempClassesDirectory(String base) throws Exception {
File local_temp_classes_dir = new File(base, "local/temp/classes");
......@@ -893,67 +782,15 @@ public class Main {
return false;
}
private static boolean compactData(String password) throws Exception {
try {
return new ActionCompactData().execute(password);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private static boolean eraseContentProcessPlatform(String password) throws Exception {
try {
return new ActionEraseContentProcessPlatform().execute(password);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private static boolean eraseContentCms(String password) throws Exception {
try {
return new ActionEraseContentCms().execute(password);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// private static boolean compactData(String password) throws Exception {
// try {
// return new ActionCompactData().execute(password);
// } catch (Exception e) {
// e.printStackTrace();
// }
// return false;
// }
private static boolean eraseContentBbs(String password) throws Exception {
try {
return new ActionEraseContentBbs().execute(password);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private static boolean eraseContentLog(String password) throws Exception {
try {
return new ActionEraseContentLog().execute(password);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private static boolean stack(String count, String interval, String password) {
try {
return new ActionStack().execute(Integer.parseInt(count, 10), Integer.parseInt(interval, 10), password);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
private static boolean heapDump(String password) {
try {
return new ActionHeapDump().execute(password);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
\ No newline at end of file
package com.x.server.console;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
public class OptionFactory {
public static Options options() {
Options ops = new Options();
ops.addOption(new Option("help", "print usage message."));
ops.addOption(start());
ops.addOption(exit());
return ops;
}
private static Option start() {
Option o = Option.builder("start").hasArg().argName("center|storage|data|web|application|all")
.desc("start server or start all server alone.").build();
return o;
}
private static Option exit() {
Option o = Option.builder("exit").desc("stop all server and exit.").build();
return o;
}
}
......@@ -244,8 +244,10 @@ public class ResourceFactory {
private static void processPlatformExecutors() throws Exception {
ExecutorService[] services = new ExecutorService[Config.processPlatform().getExecutorCount()];
for (int i = 0; i < Config.processPlatform().getExecutorCount(); i++) {
services[i] = Executors.newSingleThreadExecutor();
//services[i] = Executors.newSingleThreadExecutor();
services[i] = Executors.newFixedThreadPool(1);
}
new Resource(Config.RESOURCE_NODE_PROCESSPLATFORMEXECUTORS, services);
}
......
package com.x.server.console;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DateTools;
import com.x.server.console.action.ActionRestoreData;
import com.x.server.console.action.RestoreData;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class RestoreDataTask implements Job {
......@@ -19,8 +18,8 @@ public class RestoreDataTask implements Job {
try {
logger.print("schedule restore data task start, restore from:{}.",
Config.currentNode().restoreData().path());
ActionRestoreData action = new ActionRestoreData();
action.execute(Config.currentNode().restoreData().path(), Config.token().getPassword());
RestoreData action = new RestoreData();
action.execute(Config.currentNode().restoreData().path());
} catch (Exception e) {
throw new JobExecutionException(e);
}
......
package com.x.server.console;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DateTools;
import com.x.server.console.action.ActionRestoreStorage;
import com.x.server.console.action.RestoreStorage;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class RestoreStorageTask implements Job {
......@@ -19,8 +18,8 @@ public class RestoreStorageTask implements Job {
try {
logger.print("schedule restore storage task start, restore from:{}.",
Config.currentNode().restoreData().path());
ActionRestoreStorage action = new ActionRestoreStorage();
action.execute(Config.currentNode().restoreStorage().path(), Config.token().getPassword());
RestoreStorage action = new RestoreStorage();
action.execute(Config.currentNode().restoreStorage().path());
} catch (Exception e) {
throw new JobExecutionException(e);
}
......
package com.x.server.console.action;
import java.util.Objects;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils;
/*
@author zhourui
*/
public class ActionDebugDesignDetail extends ActionBase {
private static Logger logger = LoggerFactory.getLogger(ActionDebugDesignDetail.class);
private static Options options = new Options();
private static final String CMD_PPE = "ppe";
private static final String CMD_OS = "os";
private static final String CMD_TS = "ts";
private static final String CMD_HD = "hd";
private static final String CMD_TD = "td";
private static final String CMD_EC = "ec";
private static final String CMD_DD = "dd";
private static final String CMD_DS = "ds";
private static final String CMD_RD = "rd";
private static final String CMD_RS = "rs";
private static final String CMD_CLH2 = "clh2";
private static final int REPEAT_MAX = 100;
private static final int REPEAT_MIN = 1;
public void execute(String... args) {
try {
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options(), args);
if (cmd.hasOption(CMD_PPE)) {
ppe(cmd);
} else if (cmd.hasOption(CMD_OS)) {
os(cmd);
} else if (cmd.hasOption(CMD_TS)) {
ts(cmd);
} else if (cmd.hasOption(CMD_HD)) {
hd(cmd);
} else if (cmd.hasOption(CMD_TD)) {
td(cmd);
} else if (cmd.hasOption(CMD_EC)) {
ec(cmd);
} else if (cmd.hasOption(CMD_DD)) {
dd(cmd);
} else if (cmd.hasOption(CMD_DS)) {
ds(cmd);
} else if (cmd.hasOption(CMD_RD)) {
rd(cmd);
} else if (cmd.hasOption(CMD_RS)) {
rs(cmd);
} else if (cmd.hasOption(CMD_CLH2)) {
clh2(cmd);
} else {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("ddd (Debug Design Detail)", options);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Options options() {
options.addOption(ppeOption());
options.addOption(osOption());
options.addOption(tsOption());
options.addOption(hdOption());
options.addOption(tdOption());
options.addOption(ecOption());
options.addOption(ddOption());
options.addOption(dsOption());
options.addOption(rdOption());
options.addOption(rsOption());
options.addOption(clh2Option());
return options;
}
private static Option ppeOption() {
return Option.builder(CMD_PPE).longOpt("processPlatformExecutor").hasArg(false).desc("显示流程平台执行线程状态.").build();
}
private static Option osOption() {
return Option.builder("os").longOpt("operatingSystem").argName("repeat").numberOfArgs(1).optionalArg(true)
.hasArgs().desc("显示操作系统信息,间隔2秒.").build();
}
private static Option tsOption() {
return Option.builder("ts").longOpt("threadStatus").argName("repeat").optionalArg(true).hasArgs()
.desc("服务器线程状态,间隔2秒.合并多次执行线程信息到最后一份日志.").build();
}
private static Option hdOption() {
return Option.builder("hd").longOpt("heapDump").hasArg(false).desc("生成堆转储文件.").build();
}
private static Option tdOption() {
return Option.builder("td").longOpt("threadDump").argName("count").optionalArg(true).hasArg()
.desc("导出对比线程状态,间隔5秒.").build();
}
private static Option ecOption() {
return Option.builder("ec").longOpt("eraseContent").argName("type").hasArg().optionalArg(false)
.desc("清空实例数据,保留设计数据,type可选值 bbs cms log processPlatform.").build();
}
private static Option clh2Option() {
return Option.builder("clh2").longOpt("compactLocalH2").desc("压缩本地H2数据库.").build();
}
private static Option ddOption() {
return Option.builder("dd").longOpt("dumpData").argName("path").hasArg().optionalArg(true)
.desc("导出数据库服务器的数据转换成json格式保存到本地文件.").build();
}
private static Option dsOption() {
return Option.builder("ds").longOpt("dumpStorage").argName("path").hasArg().optionalArg(true)
.desc("导出存储服务器的文件数据转换成json格式保存到本地文件.").build();
}
private static Option rdOption() {
return Option.builder("rd").longOpt("restoreData").argName("path or date").hasArg()
.desc("将导出的json格式数据恢复到数据库服务器.").build();
}
private static Option rsOption() {
return Option.builder("rs").longOpt("restoreStorage").argName("path or date").hasArg()
.desc("将导出的json格式文件数据恢复到存储服务器.").build();
}
private void ec(CommandLine cmd) throws Exception {
if (BooleanUtils.isNotTrue(Config.currentNode().getEraseContentEnable())) {
logger.print("erase content is disabled.");
}
String type = Objects.toString(cmd.getOptionValue("ec"));
switch (type) {
case "processPlatform":
new EraseContentProcessPlatform().execute();
break;
case "bbs":
new EraseContentBbs().execute();
break;
case "cms":
new EraseContentCms().execute();
break;
case "log":
new EraseContentLog().execute();
break;
default:
logger.print("type may be processPlatform bbs cms log.");
}
}
private void td(CommandLine cmd) throws Exception {
Integer count = this.getArgInteger(cmd, CMD_TD, 10);
ThreadDump threadDump = new ThreadDump();
threadDump.execute(count);
}
private void clh2(CommandLine cmd) throws Exception {
CompactLocalH2 compactLocalH2 = new CompactLocalH2();
compactLocalH2.execute();
}
private void dd(CommandLine cmd) throws Exception {
String path = Objects.toString(cmd.getOptionValue(CMD_DD), "");
DumpData dumpData = new DumpData();
dumpData.execute(path);
}
private void ds(CommandLine cmd) throws Exception {
String path = Objects.toString(cmd.getOptionValue(CMD_DS), "");
DumpStorage dumpStorage = new DumpStorage();
dumpStorage.execute(path);
}
private void rd(CommandLine cmd) throws Exception {
String path = Objects.toString(cmd.getOptionValue(CMD_RD), "");
RestoreData restoreData = new RestoreData();
restoreData.execute(path);
}
private void rs(CommandLine cmd) throws Exception {
String path = Objects.toString(cmd.getOptionValue(CMD_RS), "");
RestoreStorage restoreStorage = new RestoreStorage();
restoreStorage.execute(path);
}
private void ts(CommandLine cmd) {
final Integer repeat = this.getArgInteger(cmd, CMD_TS, 1);
ThreadStatus threadStatus = new ThreadStatus(repeat);
threadStatus.start();
}
private void ppe(CommandLine cmd) throws Exception {
ProcessPlatformExecutor processPlatformExecutor = new ProcessPlatformExecutor();
processPlatformExecutor.execute();
}
private void os(CommandLine cmd) {
final Integer command = this.getArgInteger(cmd, "os", 1);
OperatingSystem operatingSystem = new OperatingSystem(command);
operatingSystem.start();
}
private void hd(CommandLine cmd) throws Exception {
HeapDump heapDump = new HeapDump();
heapDump.execute();
}
private Integer getArgInteger(CommandLine cmd, String opt, Integer defaultValue) {
Integer repeat = defaultValue;
String r = cmd.getOptionValue(opt);
if (NumberUtils.isParsable(r)) {
repeat = NumberUtils.toInt(r);
}
if (repeat < REPEAT_MIN || repeat > REPEAT_MAX) {
repeat = REPEAT_MIN;
}
return repeat;
}
}
\ No newline at end of file
package com.x.server.console.action;
import java.lang.management.ManagementFactory;
import java.util.Date;
import org.apache.commons.lang3.math.NumberUtils;
import com.sun.management.OperatingSystemMXBean;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
public class ActionShowCpu extends ActionBase {
private static Logger logger = LoggerFactory.getLogger(ActionShowCpu.class);
private Date start;
private void init() throws Exception {
this.start = new Date();
}
public boolean execute(Integer interval, Integer repeat) {
try {
this.init();
OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
final Integer interval_adjust = Math.min(Math.max(interval, 1), 20);
final Integer repeat_repeat = Math.min(Math.max(repeat, 1), 200);
new Thread() {
public void run() {
try {
for (int i = 0; i < repeat_repeat; i++) {
System.out.println("show cpu process cpu load:" + percent(bean.getProcessCpuLoad())
+ ", system cpu load:" + percent(bean.getSystemCpuLoad()) + ".");
Thread.sleep(interval_adjust * 1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private String percent(Double d) {
return new Double((d * 100)).intValue() + "%";
}
}
\ No newline at end of file
package com.x.server.console.action;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.server.console.server.Servers;
public class ActionShowHttpThread extends ActionBase {
private static Logger logger = LoggerFactory.getLogger(ActionShowHttpThread.class);
public boolean execute(Integer interval, Integer repeat) {
try {
final Integer interval_adjust = Math.min(Math.max(interval, 1), 20);
final Integer repeat_repeat = Math.min(Math.max(repeat, 1), 200);
new Thread() {
public void run() {
try {
for (int i = 0; i < repeat_repeat; i++) {
show();
Thread.sleep(interval_adjust * 1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private void show() {
StringBuffer sb = new StringBuffer();
if (Servers.centerServerIsRunning()) {
sb.append("center server:");
sb.append(String.format("thread pool size:%d, idle:%d. ", Servers.centerServer.getThreadPool().getThreads(),
Servers.centerServer.getThreadPool().getIdleThreads()));
} else {
sb.append("is not running. ");
}
if (Servers.applicationServerIsRunning()) {
sb.append("application server:");
sb.append(String.format("thread pool size:%d, idle:%d. ",
Servers.applicationServer.getThreadPool().getThreads(),
Servers.applicationServer.getThreadPool().getIdleThreads()));
} else {
sb.append("is not running. ");
}
if (Servers.webServerIsRunning()) {
sb.append("web server:");
sb.append(String.format("thread pool size:%d, idle:%d. ", Servers.webServer.getThreadPool().getThreads(),
Servers.webServer.getThreadPool().getIdleThreads()));
} else {
sb.append("is not running. ");
}
System.out.println(sb.toString());
}
}
\ No newline at end of file
package com.x.server.console.action;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.util.Date;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
public class ActionShowMemory extends ActionBase {
private static Logger logger = LoggerFactory.getLogger(ActionShowMemory.class);
private Date start;
private void init() throws Exception {
this.start = new Date();
}
public boolean execute(Integer interval, Integer repeat) {
try {
this.init();
MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
final Integer interval_adjust = Math.min(Math.max(interval, 1), 20);
final Integer repeat_repeat = Math.min(Math.max(repeat, 1), 200);
new Thread() {
public void run() {
try {
for (int i = 0; i < repeat_repeat; i++) {
System.out.println("show memory heap used:" + bean.getHeapMemoryUsage().getUsed() / 1048576
+ "m, max:" + bean.getHeapMemoryUsage().getMax() / 1048576 + "m, committed:"
+ bean.getHeapMemoryUsage().getCommitted() / 1048576 + "m, non heap used:"
+ bean.getNonHeapMemoryUsage().getUsed() / 1048576 + "m, max:"
+ bean.getNonHeapMemoryUsage().getMax() / 1048576 + "m, committed:"
+ bean.getNonHeapMemoryUsage().getCommitted() / 1048576 + "m.");
Thread.sleep(interval_adjust * 1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
\ No newline at end of file
package com.x.server.console.action;
import java.lang.management.ManagementFactory;
import java.util.Date;
import org.apache.commons.lang3.math.NumberUtils;
import com.sun.management.OperatingSystemMXBean;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
public class ActionShowOs extends ActionBase {
private static Logger logger = LoggerFactory.getLogger(ActionShowOs.class);
private Date start;
private void init() throws Exception {
this.start = new Date();
}
public boolean execute(Integer interval, Integer repeat) {
try {
this.init();
OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
final Integer interval_adjust = Math.min(Math.max(interval, 1), 20);
final Integer repeat_repeat = Math.min(Math.max(repeat, 1), 200);
new Thread() {
public void run() {
try {
for (int i = 0; i < repeat_repeat; i++) {
System.out.println("show os operating system processors:" + bean.getAvailableProcessors()
+ ", total memory:" + bean.getTotalPhysicalMemorySize() / (1024 * 1024) + "m, free:"
+ bean.getFreePhysicalMemorySize() / (1024 * 1024) + "m, committed virtual memory:"
+ bean.getCommittedVirtualMemorySize() / (1024 * 1024) + "m.");
Thread.sleep(interval_adjust * 1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private String percent(Double d) {
return (d.intValue() * 100) + "%";
}
}
\ No newline at end of file
package com.x.server.console.action;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.Date;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
public class ActionShowThread extends ActionBase {
private static Logger logger = LoggerFactory.getLogger(ActionShowThread.class);
private Date start;
private void init() throws Exception {
this.start = new Date();
}
public boolean execute(Integer interval, Integer repeat) {
try {
this.init();
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
final Integer interval_adjust = Math.min(Math.max(interval, 1), 20);
final Integer repeat_repeat = Math.min(Math.max(repeat, 1), 200);
new Thread() {
public void run() {
try {
for (int i = 0; i < repeat_repeat; i++) {
int deadLockedCount = bean.findDeadlockedThreads() == null ? 0
: bean.findDeadlockedThreads().length;
System.out.println("show thread total started:" + bean.getTotalStartedThreadCount()
+ ", count:" + bean.getThreadCount() + ", dead:" + deadLockedCount + ", daemon:"
+ bean.getDaemonThreadCount() + ", peak:" + bean.getPeakThreadCount() + ".");
Thread.sleep(interval_adjust * 1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
\ No newline at end of file
package com.x.server.console.action;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.http.HttpMediaType;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.ListTools;
public class ActionVersion extends ActionBase {
private static Logger logger = LoggerFactory.getLogger(ActionVersion.class);
public void execute() throws Exception {
List<WrapVersion> wos = this.list();
if (ListTools.isEmpty(wos)) {
System.out.println("already the latest version!");
} else {
for (WrapVersion _o : wos) {
System.out.println("version:" + _o.getVersion() + ", size:" + (_o.getSize() / 1024 / 1024) + "M");
for (String _s : _o.getDescriptionList()) {
System.out.println(_s);
}
}
}
}
List<WrapVersion> list() throws Exception {
List<WrapVersion> wos = new ArrayList<>();
String address = Config.collect().url("/o2_collect_assemble/jaxrs/update/list/next/" + Config.version());
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", HttpMediaType.APPLICATION_JSON_UTF_8);
connection.setRequestMethod("GET");
connection.setDoOutput(false);
connection.setDoInput(true);
connection.connect();
String json = "";
try (InputStream input = connection.getInputStream()) {
json = IOUtils.toString(input, StandardCharsets.UTF_8);
}
connection.disconnect();
Gson gson = XGsonBuilder.instance();
JsonElement jsonElement = gson.fromJson(json, JsonElement.class);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("data")) {
wos = gson.fromJson(jsonObject.get("data"), new TypeToken<List<WrapVersion>>() {
}.getType());
}
}
wos = wos.stream().sorted(Comparator.comparing(WrapVersion::getVersion)).collect(Collectors.toList());
return wos;
}
public static class WrapVersion {
private List<String> descriptionList = new ArrayList<>();
private String version;
private Long size;
public List<String> getDescriptionList() {
return descriptionList;
}
public void setDescriptionList(List<String> descriptionList) {
this.descriptionList = descriptionList;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
logger.print(Config.version());
}
}
......@@ -3,14 +3,6 @@ package com.x.server.console.action;
import java.sql.DriverManager;
import java.util.Date;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.h2.store.fs.FileUtils;
import org.h2.tools.DeleteDbFiles;
import org.h2.tools.RunScript;
import org.h2.tools.Script;
import com.x.base.core.container.factory.SlicePropertiesBuilder;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.DataServer;
import com.x.base.core.project.logger.Logger;
......@@ -18,22 +10,20 @@ import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DateTools;
import com.x.server.console.server.Servers;
public class ActionCompactData {
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.h2.store.fs.FileUtils;
import org.h2.tools.DeleteDbFiles;
import org.h2.tools.RunScript;
import org.h2.tools.Script;
private static Logger logger = LoggerFactory.getLogger(ActionCompactData.class);
public class CompactLocalH2 {
private Date start;
private static Logger logger = LoggerFactory.getLogger(CompactLocalH2.class);
private void init() throws Exception {
this.start = new Date();
}
private Date start =new Date();
public boolean execute(String password) throws Exception {
this.init();
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
public boolean execute() throws Exception {
DataServer server = Config.currentNode().getData();
if (null == server) {
logger.print("not config dataServer.");
......@@ -44,7 +34,7 @@ public class ActionCompactData {
return false;
}
if (Servers.dataServerIsRunning()) {
logger.print("data server is running.");
logger.print("data server is running, must stop data server first.");
return false;
}
/* 需要注入驱动程序 */
......@@ -59,8 +49,8 @@ public class ActionCompactData {
RunScript.execute(url, "sa", Config.token().getPassword(), backup, null, false);
FileUtils.delete(backup);
Date end = new Date();
System.out.println("compact data completed at " + DateTools.format(end) + ", elapsed:"
+ (end.getTime() - start.getTime()) + "ms.");
System.out.println(String.format("compact data completed at %s, elapsed:%dms.", DateTools.format(end),
end.getTime() - start.getTime()));
return true;
}
......
......@@ -28,24 +28,19 @@ import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.DefaultCharset;
import com.x.base.core.project.tools.ListTools;
public class ActionDumpData {
public class DumpData {
private static Logger logger = LoggerFactory.getLogger(ActionDumpData.class);
private static Logger logger = LoggerFactory.getLogger(DumpData.class);
private Date start;
private Date start = new Date();
private File dir;
private DumpRestoreDataCatalog catalog;
private Gson pureGsonDateFormated;
private Gson pureGsonDateFormated = XGsonBuilder.instance();
public boolean execute(String path, String password) throws Exception {
this.start = new Date();
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
public boolean execute(String path) throws Exception {
if (StringUtils.isEmpty(path)) {
this.dir = new File(Config.base(), "local/dump/dumpData_" + DateTools.compact(this.start));
} else {
......@@ -58,7 +53,6 @@ public class ActionDumpData {
FileUtils.forceMkdir(this.dir);
FileUtils.cleanDirectory(this.dir);
this.catalog = new DumpRestoreDataCatalog();
pureGsonDateFormated = XGsonBuilder.instance();
/* 初始化完成 */
......@@ -71,22 +65,23 @@ public class ActionDumpData {
PersistenceXmlHelper.write(persistence.getAbsolutePath(), classNames);
for (int i = 0; i < classNames.size(); i++) {
Class<JpaObject> cls = (Class<JpaObject>) Class.forName(classNames.get(i));
EntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(cls.getName(),
persistence.getName(), PersistenceXmlHelper.properties(cls.getName(), Config.slice().getEnable()));
EntityManager em = emf.createEntityManager();
try {
logger.print("dump data({}/{}): {}, count: {}.", (i + 1), classNames.size(), cls.getName(),
this.estimateCount(em, cls));
this.dump(cls, em);
} finally {
em.close();
emf.close();
}
EntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(cls.getName(),
persistence.getName(),
PersistenceXmlHelper.properties(cls.getName(), Config.slice().getEnable()));
EntityManager em = emf.createEntityManager();
try {
logger.print("dump data({}/{}): {}, count: {}.", (i + 1), classNames.size(), cls.getName(),
this.estimateCount(em, cls));
this.dump(cls, em);
} finally {
em.close();
emf.close();
}
}
FileUtils.write(new File(dir, "catalog.json"), pureGsonDateFormated.toJson(this.catalog),
DefaultCharset.charset);
logger.print("dump data completed, directory: {}, count: {}, elapsed: {} minutes.", dir.getAbsolutePath(),
this.count(), (new Date().getTime() - start.getTime()) / 1000 / 60);
this.count(), (System.currentTimeMillis() - start.getTime()) / 1000 / 60);
return true;
}
......@@ -120,7 +115,7 @@ public class ActionDumpData {
FileUtils.write(file, pureGsonDateFormated.toJson(list), DefaultCharset.charset);
}
em.clear();
System.gc();
Runtime.getRuntime().gc();
} while (ListTools.isNotEmpty(list));
this.catalog.put(cls.getName(), count);
}
......@@ -131,9 +126,9 @@ public class ActionDumpData {
Root<T> root = cq.from(cls);
Predicate p = cb.conjunction();
if (StringUtils.isNotEmpty(id)) {
p = cb.greaterThan(root.get("id"), id);
p = cb.greaterThan(root.get(JpaObject.id_FIELDNAME), id);
}
cq.select(root).where(p).orderBy(cb.asc(root.get("id")));
cq.select(root).where(p).orderBy(cb.asc(root.get(JpaObject.id_FIELDNAME)));
return em.createQuery(cq).setMaxResults(size).getResultList();
}
......
......@@ -34,24 +34,19 @@ import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.DefaultCharset;
import com.x.base.core.project.tools.ListTools;
public class ActionDumpStorage {
public class DumpStorage {
private static Logger logger = LoggerFactory.getLogger(ActionDumpStorage.class);
private static Logger logger = LoggerFactory.getLogger(DumpStorage.class);
private Date start;
private Date start= new Date();
private File dir;
private DumpRestoreStorageCatalog catalog;
private Gson pureGsonDateFormated;
private Gson pureGsonDateFormated=XGsonBuilder.instance();
public boolean execute(String path, String password) throws Exception {
this.start = new Date();
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
public boolean execute(String path) throws Exception {
if (StringUtils.isEmpty(path)) {
this.dir = new File(Config.base(), "local/dump/dumpStorage_" + DateTools.compact(this.start));
} else {
......@@ -63,7 +58,6 @@ public class ActionDumpStorage {
}
FileUtils.forceMkdir(this.dir);
FileUtils.cleanDirectory(this.dir);
this.pureGsonDateFormated = XGsonBuilder.instance();
this.catalog = new DumpRestoreStorageCatalog();
List<String> storageContainerEntityNames = new ArrayList<>();
......@@ -94,7 +88,7 @@ public class ActionDumpStorage {
logger.print(
"dump storage completed, directory: {}, count: {}, normal: {}, empty: {}, invalidStorage: {}, size: {}M, elapsed: {} minutes.",
dir.getAbsolutePath(), this.count(), this.normal(), this.empty(), this.invalidStorage(),
(this.size() / 1024 / 1024), (new Date().getTime() - start.getTime()) / 1000 / 60);
(this.size() / 1024 / 1024), (System.currentTimeMillis() - start.getTime()) / 1000 / 60);
return true;
}
......@@ -201,7 +195,7 @@ public class ActionDumpStorage {
this.dumpWrite(file, normalList, emptyList, invalidStorageList);
}
em.clear();
System.gc();
Runtime.getRuntime().gc();
} while (ListTools.isNotEmpty(list));
DumpRestoreStorageCatalogItem item = new DumpRestoreStorageCatalogItem();
item.setCount(count);
......
......@@ -16,15 +16,11 @@ import com.x.bbs.entity.BBSVoteOption;
import com.x.bbs.entity.BBSVoteOptionGroup;
import com.x.bbs.entity.BBSVoteRecord;
public class ActionEraseContentBbs extends ActionEraseContentProcessPlatform {
public class EraseContentBbs extends EraseContentProcessPlatform {
private static Logger logger = LoggerFactory.getLogger(ActionEraseContentBbs.class);
private static Logger logger = LoggerFactory.getLogger(EraseContentBbs.class);
public boolean execute(String password) throws Exception {
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
public boolean execute() throws Exception {
this.init("bbs", ItemCategory.bbs);
addClass(BBSOperationRecord.class);
addClass(BBSReplyInfo.class);
......
......@@ -18,15 +18,11 @@ import com.x.cms.core.entity.DocumentCommentCommend;
import com.x.cms.core.entity.DocumentCommend;
import com.x.cms.core.entity.CmsBatchOperation;
public class ActionEraseContentCms extends ActionEraseContentProcessPlatform {
public class EraseContentCms extends EraseContentProcessPlatform {
private static Logger logger = LoggerFactory.getLogger(ActionEraseContentCms.class);
private static Logger logger = LoggerFactory.getLogger(EraseContentCms.class);
public boolean execute(String password) throws Exception {
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
this.init("cms", ItemCategory.cms);
addClass(Document.class);
addClass(Review.class);
......
......@@ -10,15 +10,11 @@ import com.x.program.center.core.entity.ScheduleLog;
import com.x.program.center.core.entity.UnexpectedErrorLog;
import com.x.program.center.core.entity.WarnLog;
public class ActionEraseContentLog extends ActionEraseContentProcessPlatform {
public class EraseContentLog extends EraseContentProcessPlatform {
private static Logger logger = LoggerFactory.getLogger(ActionEraseContentLog.class);
private static Logger logger = LoggerFactory.getLogger(EraseContentLog.class);
public boolean execute(String password) throws Exception {
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
this.init("log", null);
addClass(ScheduleLog.class);
addClass(PromptErrorLog.class);
......
......@@ -43,9 +43,9 @@ import com.x.processplatform.core.entity.content.WorkCompleted;
import com.x.processplatform.core.entity.content.WorkLog;
import com.x.query.core.entity.Item;
public class ActionEraseContentProcessPlatform {
public class EraseContentProcessPlatform {
private static Logger logger = LoggerFactory.getLogger(ActionEraseContentProcessPlatform.class);
private static Logger logger = LoggerFactory.getLogger(EraseContentProcessPlatform.class);
private Date start;
......@@ -55,11 +55,7 @@ public class ActionEraseContentProcessPlatform {
private ItemCategory itemCategory;
public boolean execute(String password) throws Exception {
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
public boolean execute() throws Exception {
this.init("processPlatform", ItemCategory.pp);
addClass(Attachment.class);
addClass(DocumentVersion.class);
......
......@@ -6,83 +6,32 @@ import java.util.Date;
import javax.management.MBeanServer;
import org.apache.commons.lang3.StringUtils;
import com.sun.management.HotSpotDiagnosticMXBean;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DateTools;
public class ActionHeapDump extends ActionBase {
private static Logger logger = LoggerFactory.getLogger(ActionHeapDump.class);
public class HeapDump {
private Date start;
private static Logger logger = LoggerFactory.getLogger(HeapDump.class);
private Date end;
// This is the name of the HotSpot Diagnostic MBean
private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
// field to store the hotspot diagnostic MBean
private static volatile HotSpotDiagnosticMXBean hotspotMBean;
public boolean execute(String password) {
public void execute() throws Exception {
try {
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
start = new Date();
Date start = new Date();
File file = new File(Config.dir_logs(),
"heapDump_" + DateTools.format(start, DateTools.formatCompact_yyyyMMddHHmmss) + ".hprof");
dumpHeap(file.getAbsolutePath());
end = new Date();
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME,
HotSpotDiagnosticMXBean.class);
bean.dumpHeap(file.getAbsolutePath(), true);
logger.print(
"generate java heap dump to {}, elapsed: {}ms, parses file see url: https://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html",
file.getAbsoluteFile(), end.getTime() - start.getTime());
file.getAbsoluteFile(), System.currentTimeMillis() - start.getTime());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
static void dumpHeap(String fileName) {
// initialize hotspot diagnostic MBean
initHotspotMBean();
try {
hotspotMBean.dumpHeap(fileName, true);
} catch (RuntimeException re) {
throw re;
} catch (Exception exp) {
throw new RuntimeException(exp);
}
}
// initialize the hotspot diagnostic MBean field
private static void initHotspotMBean() {
if (hotspotMBean == null) {
synchronized (ActionHeapDump.class) {
if (hotspotMBean == null) {
hotspotMBean = getHotspotMBean();
}
}
}
}
// get the hotspot diagnostic MBean from the
// platform MBean server
private static HotSpotDiagnosticMXBean getHotspotMBean() {
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME,
HotSpotDiagnosticMXBean.class);
return bean;
} catch (RuntimeException re) {
throw re;
} catch (Exception exp) {
throw new RuntimeException(exp);
}
}
......
package com.x.server.console.action;
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
public class OperatingSystem extends Thread {
private static Logger logger = LoggerFactory.getLogger(DumpData.class);
private Integer count;
public OperatingSystem(int count) {
this.count = count;
}
public void run() {
OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
try {
for (int i = 0; i < count; i++) {
String msg = String.format(
"cpu:%d, system load:%.2f, process load:%.2f. memory:%dm, free:%dm, committed virtual:%dm.",
bean.getAvailableProcessors(), bean.getSystemCpuLoad(), bean.getProcessCpuLoad(),
bean.getTotalPhysicalMemorySize() / (1024 * 1024),
bean.getFreePhysicalMemorySize() / (1024 * 1024),
bean.getCommittedVirtualMemorySize() / (1024 * 1024));
System.out.println(msg);
Thread.sleep(2000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
package com.x.server.console.action;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
/**
* @author zhourui
*/
public class ProcessPlatformExecutor {
private static Logger logger = LoggerFactory.getLogger(RestoreData.class);
public void execute() throws Exception {
ExecutorService[] executorServices = Config.resource_node_processPlatformExecutors();
List<String> list = new ArrayList<>();
for (int i = 0; i < executorServices.length; i++) {
ExecutorService service = executorServices[i];
ThreadPoolExecutor executor = (ThreadPoolExecutor) service;
BlockingQueue<Runnable> queue = executor.getQueue();
list.add(String.format("processPlatform executorServices[%d] completed:%d, block:%d.", i,
executor.getCompletedTaskCount(), queue.size()));
if (!queue.isEmpty()) {
List<String> os = new ArrayList<>();
for (Runnable o : queue) {
os.add(o.getClass().toString());
}
list.add(" +++ blocking: " + StringUtils.join(os, ",") + ".");
}
}
System.out.println(StringUtils.join(list, StringUtils.LF));
}
}
\ No newline at end of file
......@@ -14,11 +14,6 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
......@@ -33,49 +28,48 @@ import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.DefaultCharset;
import com.x.base.core.project.tools.ListTools;
public class ActionRestoreData {
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.openjpa.persistence.OpenJPAPersistence;
/**
* @author zhourui
*/
public class RestoreData {
private static Logger logger = LoggerFactory.getLogger(ActionRestoreData.class);
private static Logger logger = LoggerFactory.getLogger(RestoreData.class);
private Date start;
private Date start = new Date();
private File dir;
private DumpRestoreDataCatalog catalog;
private Gson pureGsonDateFormated;
private Gson pureGsonDateFormated = XGsonBuilder.instance();
public boolean execute(Date date, String password) throws Exception {
this.start = new Date();
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not mactch.");
return false;
public boolean execute(String path) throws Exception {
if (StringUtils.isEmpty(path)) {
logger.print("path is empty.");
}
this.dir = new File(Config.base(), "local/dump/dumpData_" + DateTools.compact(date));
this.catalog = BaseTools.readConfigObject("local/dump/dumpData_" + DateTools.compact(date) + "/catalog.json",
DumpRestoreDataCatalog.class);
pureGsonDateFormated = XGsonBuilder.instance();
return this.execute();
}
public boolean execute(String path, String password) throws Exception {
this.start = new Date();
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not mactch.");
return false;
}
this.dir = new File(path);
if (!(this.dir.exists() && this.dir.isDirectory())) {
logger.print("dir not exist: {}.", path);
return false;
} else if (StringUtils.startsWith(dir.getAbsolutePath(), Config.base())) {
logger.print("path can not in base directory.");
return false;
if (BooleanUtils.isTrue(DateTools.isCompactDateTime(path))) {
this.dir = new File(Config.base(), "local/dump/dumpData_" + path);
this.catalog = BaseTools.readConfigObject("local/dump/dumpData_" + path + "/catalog.json",
DumpRestoreDataCatalog.class);
} else {
this.dir = new File(path);
if (!(this.dir.exists() && this.dir.isDirectory())) {
logger.print("dir not exist: {}.", path);
return false;
} else if (StringUtils.startsWith(dir.getAbsolutePath(), Config.base())) {
logger.print("path can not in base directory.");
return false;
}
}
this.catalog = XGsonBuilder.instance()
.fromJson(FileUtils.readFileToString(new File(dir.getAbsolutePath() + File.separator + "catalog.json"),
DefaultCharset.charset_utf_8), DumpRestoreDataCatalog.class);
pureGsonDateFormated = XGsonBuilder.instance();
return this.execute();
}
......@@ -113,7 +107,7 @@ public class ActionRestoreData {
}
}
logger.print("restore data completed, total count: {}, elapsed: {} minutes.", count,
(new Date().getTime() - start.getTime()) / 1000 / 60);
(System.currentTimeMillis() - start.getTime()) / 1000 / 60);
return true;
}
......@@ -149,7 +143,7 @@ public class ActionRestoreData {
}
em.getTransaction().commit();
em.clear();
System.gc();
Runtime.getRuntime().gc();
}
System.out.println("restore data: " + cls.getName() + " completed, count: " + count + ".");
return count;
......
......@@ -2,6 +2,7 @@ package com.x.server.console.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
......@@ -17,6 +18,7 @@ import javax.persistence.criteria.Root;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.openjpa.persistence.OpenJPAPersistence;
......@@ -37,72 +39,67 @@ import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.DefaultCharset;
import com.x.base.core.project.tools.ListTools;
public class ActionRestoreStorage {
/**
* @author zhourui
*/
private static Logger logger = LoggerFactory.getLogger(ActionRestoreStorage.class);
public class RestoreStorage {
private Date start;
private static Logger logger = LoggerFactory.getLogger(RestoreStorage.class);
private Date start = new Date();
private File dir;
private DumpRestoreStorageCatalog catalog;
private Gson pureGsonDateFormated;
public boolean execute(Date date, String password) throws Exception {
this.start = new Date();
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not mactch.");
return false;
}
this.dir = new File(Config.base(), "local/dump/dumpStorage_" + DateTools.compact(date));
this.catalog = BaseTools.readConfigObject("local/dump/dumpStorage_" + DateTools.compact(date) + "/catalog.json",
DumpRestoreStorageCatalog.class);
pureGsonDateFormated = XGsonBuilder.instance();
return this.execute();
}
private Gson pureGsonDateFormated = XGsonBuilder.instance();
public boolean execute(String path, String password) throws Exception {
this.start = new Date();
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not mactch.");
return false;
public boolean execute(final String path) throws Exception {
if (StringUtils.isEmpty(path)) {
logger.print("path is empty.");
}
this.dir = new File(path);
if (!(this.dir.exists() && this.dir.isDirectory())) {
logger.print("dir not exist: {}.", path);
return false;
} else if (StringUtils.startsWith(dir.getAbsolutePath(), Config.base())) {
logger.print("path can not in base directory.");
return false;
if (BooleanUtils.isTrue(DateTools.isCompactDateTime(path))) {
this.dir = new File(Config.base(), "local/dump/dumpStorage_" + path);
this.catalog = BaseTools.readConfigObject("local/dump/dumpStorage_" + path + "/catalog.json",
DumpRestoreStorageCatalog.class);
} else {
this.dir = new File(path);
if (!(this.dir.exists() && this.dir.isDirectory())) {
logger.print("dir not exist: {}.", path);
return false;
} else if (StringUtils.startsWith(dir.getAbsolutePath(), Config.base())) {
logger.print("path can not in base directory.");
return false;
}
this.catalog = XGsonBuilder.instance()
.fromJson(FileUtils.readFileToString(
new File(dir.getAbsolutePath() + File.separator + "catalog.json"),
DefaultCharset.charset_utf_8), DumpRestoreStorageCatalog.class);
}
this.catalog = XGsonBuilder.instance()
.fromJson(FileUtils.readFileToString(new File(dir.getAbsolutePath() + File.separator + "catalog.json"),
DefaultCharset.charset_utf_8), DumpRestoreStorageCatalog.class);
pureGsonDateFormated = XGsonBuilder.instance();
return this.execute();
}
public boolean execute() throws Exception {
List<String> storageContainerEntityNames = new ArrayList<>();
private boolean execute() throws Exception {
final List<String> storageContainerEntityNames = new ArrayList<>();
storageContainerEntityNames.addAll((List<String>) Config.resource(Config.RESOURCE_STORAGECONTAINERENTITYNAMES));
List<String> classNames = new ArrayList<>();
classNames.addAll(this.catalog.keySet());
classNames = ListTools.includesExcludesWildcard(classNames, Config.dumpRestoreStorage().getIncludes(),
Config.dumpRestoreStorage().getExcludes());
logger.print("restore storage find {} to restore.", classNames.size());
File persistence = new File(Config.dir_local_temp_classes(), DateTools.compact(this.start) + "_dump.xml");
final File persistence = new File(Config.dir_local_temp_classes(), DateTools.compact(this.start) + "_dump.xml");
PersistenceXmlHelper.write(persistence.getAbsolutePath(), classNames);
StorageMappings storageMappings = Config.storageMappings();
final StorageMappings storageMappings = Config.storageMappings();
int count = 0;
for (int i = 0; i < classNames.size(); i++) {
Class<StorageObject> cls = (Class<StorageObject>) Class.forName(classNames.get(i));
EntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(cls.getName(),
final Class<StorageObject> cls = (Class<StorageObject>) Class.forName(classNames.get(i));
final EntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(cls.getName(),
persistence.getName(), PersistenceXmlHelper.properties(cls.getName(), Config.slice().getEnable()));
EntityManager em = emf.createEntityManager();
final EntityManager em = emf.createEntityManager();
em.setFlushMode(FlushModeType.COMMIT);
try {
DumpRestoreStorageCatalogItem item = this.catalog.get(cls.getName());
final DumpRestoreStorageCatalogItem item = this.catalog.get(cls.getName());
logger.print(
"restore storage({}/{}): {}, count: {}, normal: {} will be restore, invalidStorage: {} and empty: {} will be ignore, size: {}M.",
(i + 1), classNames.size(), cls.getName(), item.getCount(), item.getNormal(),
......@@ -113,26 +110,27 @@ public class ActionRestoreStorage {
emf.close();
}
logger.print("restore storage completed, total count: {}, elapsed: {} minutes.", count,
(new Date().getTime() - start.getTime()) / 1000 / 60);
(System.currentTimeMillis() - start.getTime()) / 1000 / 60);
}
return false;
}
private <T extends StorageObject> long store(Class<T> cls, EntityManager em, StorageMappings storageMappings)
throws Exception {
File classDirectory = new File(this.dir, cls.getName());
private <T extends StorageObject> long store(final Class<T> cls, final EntityManager em,
final StorageMappings storageMappings) throws Exception {
final File classDirectory = new File(this.dir, cls.getName());
if ((!classDirectory.exists()) || (!classDirectory.isDirectory())) {
throw new Exception("can not find directory: " + classDirectory.getAbsolutePath() + ".");
}
long count = 0;
List<File> files = new ArrayList<File>(FileUtils.listFiles(classDirectory, new String[] { "json" }, false));
final List<File> files = new ArrayList<File>(
FileUtils.listFiles(classDirectory, new String[] { "json" }, false));
/** 对文件进行排序,和dump的时候的顺序保持一直 */
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
String n1 = FilenameUtils.getBaseName(o1.getName());
String n2 = FilenameUtils.getBaseName(o2.getName());
Integer i1 = Integer.parseInt(n1);
Integer i2 = Integer.parseInt(n2);
public int compare(final File o1, final File o2) {
final String n1 = FilenameUtils.getBaseName(o1.getName());
final String n2 = FilenameUtils.getBaseName(o2.getName());
final Integer i1 = Integer.parseInt(n1);
final Integer i2 = Integer.parseInt(n2);
return i1.compareTo(i2);
}
});
......@@ -144,11 +142,11 @@ public class ActionRestoreStorage {
file = files.get(i);
/** 必须先转换成 jsonElement 不能直接转成泛型T,如果直接转会有类型不匹配比如Integer变成了Double */
logger.print("restoring " + (i + 1) + "/" + files.size() + " part of storage: " + cls.getName() + ".");
JsonArray raws = this.convert(file);
final JsonArray raws = this.convert(file);
if (null != raws) {
em.getTransaction().begin();
for (JsonElement o : raws) {
T t = pureGsonDateFormated.fromJson(o, cls);
for (final JsonElement o : raws) {
final T t = pureGsonDateFormated.fromJson(o, cls);
if (Config.dumpRestoreStorage().getRedistribute()) {
mapping = storageMappings.random(cls);
} else {
......@@ -158,7 +156,7 @@ public class ActionRestoreStorage {
throw new Exception(
"can not find storageMapping class: " + cls.getName() + ", name:" + t.getName());
}
File source = new File(classDirectory, FilenameUtils.getBaseName(file.getName())
final File source = new File(classDirectory, FilenameUtils.getBaseName(file.getName())
+ StorageObject.PATHSEPARATOR + FilenameUtils.getName(t.path()));
try (FileInputStream input = new FileInputStream(source)) {
t.saveContent(mapping, input, t.getName());
......@@ -168,28 +166,28 @@ public class ActionRestoreStorage {
}
em.getTransaction().commit();
em.clear();
System.gc();
Runtime.getRuntime().gc();
}
}
return count;
}
private JsonArray convert(File file) throws Exception {
private JsonArray convert(final File file) throws IOException {
/** 这里不进行判断,因为格式是严格约定的,出现意外应该先报错停止 */
String json = FileUtils.readFileToString(file, DefaultCharset.charset);
JsonElement jsonElement = pureGsonDateFormated.fromJson(json, JsonElement.class);
JsonObject jsonObject = jsonElement.getAsJsonObject();
final String json = FileUtils.readFileToString(file, DefaultCharset.charset);
final JsonElement jsonElement = pureGsonDateFormated.fromJson(json, JsonElement.class);
final JsonObject jsonObject = jsonElement.getAsJsonObject();
return jsonObject.get("normals").getAsJsonArray();
}
private <T extends StorageObject> void clean(Class<T> cls, EntityManager em, StorageMappings storageMappings)
throws Exception {
private <T extends StorageObject> void clean(final Class<T> cls, final EntityManager em,
final StorageMappings storageMappings) throws Exception {
List<T> list = null;
StorageMapping mapping = null;
do {
if (ListTools.isNotEmpty(list)) {
em.getTransaction().begin();
for (T t : list) {
for (final T t : list) {
mapping = storageMappings.get(cls, t.getStorage());
if (null != mapping) {
t.deleteContent(mapping);
......@@ -198,9 +196,9 @@ public class ActionRestoreStorage {
}
em.getTransaction().commit();
}
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(cls);
Root<T> root = cq.from(cls);
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<T> cq = cb.createQuery(cls);
final Root<T> root = cq.from(cls);
cq.select(root);
list = em.createQuery(cq).setMaxResults(Config.dumpRestoreData().getBatchSize()).getResultList();
} while (ListTools.isNotEmpty(list));
......
......@@ -8,73 +8,75 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.ThreadUtils;
import com.google.gson.Gson;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.DefaultCharset;
public class ActionStack extends ActionBase {
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.ThreadUtils;
private static Logger logger = LoggerFactory.getLogger(ActionStack.class);
public class ThreadDump {
public boolean execute(Integer count, Integer interval, String password) {
try {
if (!StringUtils.equals(Config.token().getPassword(), password)) {
logger.print("password not match.");
return false;
}
Map<Long, Integer> state_new = new HashMap<>();
Map<Long, Integer> state_runable = new HashMap<>();
Map<Long, Integer> state_blocked = new HashMap<>();
Map<Long, Integer> state_waiting = new HashMap<>();
Map<Long, Integer> state_timed_waiting = new HashMap<>();
Map<Long, Integer> state_terminated = new HashMap<>();
File startFile = null;
for (int i = 1; i <= count; i++) {
Date date = new Date();
StringBuffer buffer = new StringBuffer();
for (Thread thread : ThreadUtils.getAllThreads()) {
this.state(thread, state_new, state_runable, state_blocked, state_waiting, state_timed_waiting,
state_terminated);
this.dump(buffer, thread);
}
File file = new File(Config.dir_logs(true), this.fileName(i, count, date));
if (i == 1) {
startFile = file;
}
if (i != count) {
FileUtils.write(file, buffer, DefaultCharset.charset_utf_8);
Thread.sleep(interval);
} else {
buffer.append(System.lineSeparator()).append("Thread state statistics:");
buffer.append(" NEW(" + state_new.size() + "),");
buffer.append(" RUNABLE(" + state_runable.size() + "),");
buffer.append(" blocked(" + state_blocked.size() + "),");
buffer.append(" waiting(" + state_waiting.size() + "),");
buffer.append(" timed_waiting(" + state_timed_waiting.size() + "),");
buffer.append(" terminated(" + state_terminated.size() + ").");
buffer.append(System.lineSeparator());
this.writeState(buffer, state_new, State.NEW, count);
this.writeState(buffer, state_runable, State.RUNNABLE, count);
this.writeState(buffer, state_blocked, State.BLOCKED, count);
this.writeState(buffer, state_waiting, State.WAITING, count);
this.writeState(buffer, state_timed_waiting, State.TIMED_WAITING, count);
this.writeState(buffer, state_terminated, State.TERMINATED, count);
FileUtils.write(file, buffer, DefaultCharset.charset_utf_8);
logger.print("stack dump thread to files: {} - {}.", startFile.getAbsolutePath(),
file.getAbsolutePath());
private static Logger logger = LoggerFactory.getLogger(DumpData.class);
public void execute(Integer count) throws Exception {
new Thread() {
public void run() {
try {
Map<Long, Integer> state_new = new HashMap<>();
Map<Long, Integer> state_runable = new HashMap<>();
Map<Long, Integer> state_blocked = new HashMap<>();
Map<Long, Integer> state_waiting = new HashMap<>();
Map<Long, Integer> state_timed_waiting = new HashMap<>();
Map<Long, Integer> state_terminated = new HashMap<>();
File startFile = null;
for (int i = 1; i <= count; i++) {
Date date = new Date();
StringBuffer buffer = new StringBuffer();
for (Thread thread : ThreadUtils.getAllThreads()) {
state(thread, state_new, state_runable, state_blocked, state_waiting,
state_timed_waiting, state_terminated);
dump(buffer, thread);
}
File file = new File(Config.dir_logs(true), fileName(i, count, date));
if (i == 1) {
startFile = file;
}
if (i != count) {
FileUtils.write(file, buffer, DefaultCharset.charset_utf_8);
logger.print(String.format("thread dump to file(%d/%d): %s.", i, count,
file.getAbsolutePath()));
Thread.sleep(5000);
} else {
buffer.append(System.lineSeparator()).append("Thread state statistics:");
buffer.append(" NEW(" + state_new.size() + "),");
buffer.append(" RUNABLE(" + state_runable.size() + "),");
buffer.append(" blocked(" + state_blocked.size() + "),");
buffer.append(" waiting(" + state_waiting.size() + "),");
buffer.append(" timed_waiting(" + state_timed_waiting.size() + "),");
buffer.append(" terminated(" + state_terminated.size() + ").");
buffer.append(System.lineSeparator());
writeState(buffer, state_new, State.NEW, count);
writeState(buffer, state_runable, State.RUNNABLE, count);
writeState(buffer, state_blocked, State.BLOCKED, count);
writeState(buffer, state_waiting, State.WAITING, count);
writeState(buffer, state_timed_waiting, State.TIMED_WAITING, count);
writeState(buffer, state_terminated, State.TERMINATED, count);
FileUtils.write(file, buffer, DefaultCharset.charset_utf_8);
logger.print(String.format("thread dump to files: %s - %s.",
startFile.getAbsolutePath(), file.getAbsolutePath()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}.start();
}
private void writeState(StringBuffer buffer, Map<Long, Integer> map, State state, Integer count) {
......@@ -90,27 +92,27 @@ public class ActionStack extends ActionBase {
Map<Long, Integer> state_terminated) {
Map<Long, Integer> state;
switch (thread.getState()) {
case NEW:
state = state_new;
break;
case RUNNABLE:
state = state_runable;
break;
case BLOCKED:
state = state_blocked;
break;
case WAITING:
state = state_waiting;
break;
case TIMED_WAITING:
state = state_timed_waiting;
break;
case TERMINATED:
state = state_terminated;
break;
default:
state = state_terminated;
break;
case NEW:
state = state_new;
break;
case RUNNABLE:
state = state_runable;
break;
case BLOCKED:
state = state_blocked;
break;
case WAITING:
state = state_waiting;
break;
case TIMED_WAITING:
state = state_timed_waiting;
break;
case TERMINATED:
state = state_terminated;
break;
default:
state = state_terminated;
break;
}
state.compute(thread.getId(), (k, v) -> {
if (null == v) {
......@@ -140,5 +142,4 @@ public class ActionStack extends ActionBase {
return "stack_" + DateTools.format(date, "yyyyMMddHHmmssSSS") + "_"
+ StringUtils.repeat('0', ((count + "").length()) - ((idx + "").length())) + idx + "_" + count + ".txt";
}
}
\ No newline at end of file
package com.x.server.console.action;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.List;
import com.x.server.console.server.Servers;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
/**
* @author
*/
public class ThreadStatus extends Thread {
private Integer repeat;
public ThreadStatus(Integer repeat) {
this.repeat = repeat;
}
public void run() {
try {
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
for (int i = 0; i < repeat; i++) {
List<String> list = new ArrayList<>();
int deadLockedCount = bean.findDeadlockedThreads() == null ? 0 : bean.findDeadlockedThreads().length;
list.add(String.format("thread total started:%d, count:%d, peak:%d, daemon:%d, dead:%d.",
bean.getTotalStartedThreadCount(), bean.getThreadCount(), bean.getPeakThreadCount(),
bean.getDaemonThreadCount(), deadLockedCount));
if (BooleanUtils.isTrue(Servers.centerServerIsRunning())) {
list.add(String.format(" +++ center server thread pool size:%d, idle:%d.",
Servers.centerServer.getThreadPool().getThreads(),
Servers.centerServer.getThreadPool().getIdleThreads()));
}
if (BooleanUtils.isTrue(Servers.applicationServerIsRunning())) {
list.add(String.format(" +++ application server thread pool size:%d, idle:%d.",
Servers.applicationServer.getThreadPool().getThreads(),
Servers.applicationServer.getThreadPool().getIdleThreads()));
}
if (BooleanUtils.isTrue(Servers.webServerIsRunning())) {
list.add(String.format(" +++ web server thread pool size:%d, idle:%d.",
Servers.webServer.getThreadPool().getThreads(),
Servers.webServer.getThreadPool().getIdleThreads()));
}
System.out.println(StringUtils.join(list, StringUtils.LF));
Thread.sleep(2000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册