Browse Source

增加服务控制代码

master
VIVIMAN 3 years ago
parent
commit
5e3391e2bc
  1. 6
      src/main/java/com/insigma/HyToolApplication.java
  2. 24
      src/main/java/com/insigma/config/AppCfg.java
  3. 2
      src/main/java/com/insigma/service/Database.java
  4. 4
      src/main/java/com/insigma/service/Middleware.java
  5. 20
      src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java
  6. 58
      src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java
  7. 1
      src/main/java/com/insigma/utils/LinuxCommandUtil.java
  8. 1
      src/main/java/com/insigma/utils/WinCommandUtil.java
  9. 12
      src/main/java/com/insigma/utils/WinServiceTool.java
  10. 2
      src/main/resources/application.properties
  11. 1
      src/main/resources/static/32bit.bat
  12. 2
      src/main/resources/static/64bit.bat
  13. 7
      src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java
  14. 7
      src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java
  15. 3
      src/test/java/com/insigma/utils/WinServiceToolTest.java

6
src/main/java/com/insigma/HyToolApplication.java

@ -22,7 +22,8 @@ import java.util.Properties;
public class HyToolApplication { public class HyToolApplication {
public static void main(String[] args) { public static void main(String[] args) {
Properties properties=System.getProperties();
String propertiesValue = properties.getProperty("os.arch");
//读取配置文件的值来选择启动ui //读取配置文件的值来选择启动ui
try { try {
Properties loadProperties = PropertiesLoaderUtils Properties loadProperties = PropertiesLoaderUtils
@ -35,6 +36,9 @@ public class HyToolApplication {
AppCfg.DB = loadProperties.getProperty("hy.db"); AppCfg.DB = loadProperties.getProperty("hy.db");
AppCfg.MW = loadProperties.getProperty("hy.mw"); AppCfg.MW = loadProperties.getProperty("hy.mw");
AppCfg.HZB = loadProperties.getProperty("hy.hzb"); AppCfg.HZB = loadProperties.getProperty("hy.hzb");
AppCfg.CODE = loadProperties.getProperty("hy.code");
AppCfg.OSArch = loadProperties.getProperty("hy.os-arch");
AppCfg.is32Bit = AppCfg.OSArch.equals(propertiesValue);
AppCfg.BROWSER = loadProperties.getProperty("hy.browser"); AppCfg.BROWSER = loadProperties.getProperty("hy.browser");
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {

24
src/main/java/com/insigma/config/AppCfg.java

@ -7,8 +7,32 @@ package com.insigma.config;
* @since 17:21 2022/4/18 * @since 17:21 2022/4/18
*/ */
public class AppCfg { public class AppCfg {
/**
* 操作系统 架构 <br/>x86
*/
public static String OSArch;
/**
* 操作系统 架构 <br/>x86
*/
public static boolean is32Bit;
/**
* 浏览器服务名 关键字 <br/>360se.exe
*/
public static String BROWSER; public static String BROWSER;
/**
* 数据库服务名 <br/>GWY20_Mysql
*/
public static String DB; public static String DB;
/**
* 中间件服务名 <br/>GWY20_Tomcat
*/
public static String MW; public static String MW;
/**
* 应用 安装目录 <br/>D:/hzb2021
*/
public static String HZB; public static String HZB;
/**
* 系统环境执行脚本编码格式 <br/>GBK
*/
public static String CODE;
} }

2
src/main/java/com/insigma/service/Database.java

@ -29,7 +29,7 @@ public interface Database {
* @param size 设置大小值 * @param size 设置大小值
* @return * @return
*/ */
default boolean setDbSize(int size) { default boolean setDbSize(int size) throws Exception {
System.out.printf("设置出参数:%d,但是未检测到实现应用调用此方法!", size); System.out.printf("设置出参数:%d,但是未检测到实现应用调用此方法!", size);
return false; return false;
} }

4
src/main/java/com/insigma/service/Middleware.java

@ -16,7 +16,7 @@ public interface Middleware {
* 清除 应用 缓存 * 清除 应用 缓存
* @return * @return
*/ */
default boolean cleanMwCache() { default boolean cleanMwCache() throws Exception {
System.out.printf("清除缓存,但是未检测到实现应用调用此方法!"); System.out.printf("清除缓存,但是未检测到实现应用调用此方法!");
return false; return false;
} }
@ -25,7 +25,7 @@ public interface Middleware {
* @param size 设置大小值 * @param size 设置大小值
* @return * @return
*/ */
default boolean setMwSize(int size) { default boolean setMwSize(int size) throws Exception {
System.out.printf("设置应用参数:%d,但是未检测到实现应用调用此方法!", size); System.out.printf("设置应用参数:%d,但是未检测到实现应用调用此方法!", size);
return false; return false;
} }

20
src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java

@ -32,36 +32,36 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa
@Override @Override
public boolean rebuildIndex(List<IndexObj> index) { public boolean rebuildIndex(List<IndexObj> index) {
return Database.super.rebuildIndex(index); return true;
} }
@Override @Override
public boolean setDbSize(int size) { public boolean setDbSize(int size) throws Exception {
return Database.super.setDbSize(size); return true;
} }
@Override @Override
public boolean cleanDbCache(List<String> sql, List<TabColObj> tabColObjs) { public boolean cleanDbCache(List<String> sql, List<TabColObj> tabColObjs) {
return Database.super.cleanDbCache(sql, tabColObjs); return true;
} }
@Override @Override
public boolean registrationDbService() { public boolean registrationDbService() {
return Database.super.registrationDbService(); return true;
} }
@Override @Override
public boolean cleanMwCache() { public boolean cleanMwCache() throws Exception {
return Middleware.super.cleanMwCache(); return true;
} }
@Override @Override
public boolean setMwSize(int size) { public boolean setMwSize(int size) throws Exception {
return Middleware.super.setMwSize(size); return true;
} }
@Override @Override
public boolean registrationMwService() { public boolean registrationMwService() {
return Middleware.super.registrationMwService(); return true;
} }
} }

58
src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java

@ -6,10 +6,7 @@ import com.insigma.entry.IndexObj;
import com.insigma.entry.TabColObj; import com.insigma.entry.TabColObj;
import com.insigma.service.Computer; import com.insigma.service.Computer;
import com.insigma.service.*; import com.insigma.service.*;
import com.insigma.utils.DbUtil; import com.insigma.utils.*;
import com.insigma.utils.FileUtil;
import com.insigma.utils.WinCommandUtil;
import com.insigma.utils.ZipUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.sql.SQLException; import java.sql.SQLException;
@ -38,6 +35,7 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
@Override @Override
public boolean rebuildIndex(List<IndexObj> index) { public boolean rebuildIndex(List<IndexObj> index) {
boolean retBool;
log.info("创建链接..."); log.info("创建链接...");
final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd); final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd);
try { try {
@ -45,31 +43,37 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
session.execute("DROP INDEX ? ON ?", idx.getIdx(), idx.getTab()); session.execute("DROP INDEX ? ON ?", idx.getIdx(), idx.getTab());
session.execute("ALTER TABLE ? ADD INDEX ? (?) USING BTREE", idx.getTab(), idx.getIdx(), idx.getCol()); session.execute("ALTER TABLE ? ADD INDEX ? (?) USING BTREE", idx.getTab(), idx.getIdx(), idx.getCol());
} }
retBool = true;
} catch (SQLException e) { } catch (SQLException e) {
log.error("执行更新索引发生异常:{}", e.getMessage()); log.error("执行更新索引发生异常:{}", e.getMessage());
e.printStackTrace();
session.close(); session.close();
return false; retBool = false;
} }
session.close(); session.close();
return true; return retBool;
} }
@Override @Override
public boolean setDbSize(int size) { public boolean setDbSize(int size) throws Exception {
log.info("停止 数据库 服务..."); log.info("停止 数据库 服务...");
runShell("sc stop " + AppCfg.DB); WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
ws.stopService();
}
log.info("设置 数据库 内存参数..."); log.info("设置 数据库 内存参数...");
FileUtil.replaceLine(AppCfg.HZB + "/mysql/my.ini", FileUtil.replaceLine(AppCfg.HZB + "/mysql/my.ini",
"innodb_buffer_pool_size", "innodb_buffer_pool_size",
String.format("innodb_buffer_pool_size=%dM", size)); String.format("innodb_buffer_pool_size=%dM", size));
log.info("启动 数据库 服务..."); log.info("启动 数据库 服务...");
runShell("sc start " + AppCfg.DB); ws.startService();
return Database.super.setDbSize(size); return true;
} }
@Override @Override
public boolean cleanDbCache(List<String> sql, List<TabColObj> tabColObjs) { public boolean cleanDbCache(List<String> sql, List<TabColObj> tabColObjs) {
boolean retBool;
log.info("创建链接..."); log.info("创建链接...");
final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd); final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd);
try { try {
@ -84,26 +88,32 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
tab.getTab(), tab.getCol(), tab.getCol(), lj, tab.getCol()); tab.getTab(), tab.getCol(), tab.getCol(), lj, tab.getCol());
} }
} }
retBool = true;
} catch (SQLException e) { } catch (SQLException e) {
log.error("执行 清洗数据中特殊字符串 发生异常:{}", e.getMessage()); log.error("执行 清洗数据中特殊字符串 发生异常:{}", e.getMessage());
e.printStackTrace(); e.printStackTrace();
session.close(); session.close();
return false; retBool = false;
} }
log.info("执行 清洗数据中特殊字符串 完成..."); log.info("执行 清洗数据中特殊字符串 完成...");
session.close(); session.close();
return true; return retBool;
} }
@Override @Override
public boolean registrationDbService() { public boolean registrationDbService() {
// TODO
return true; return true;
} }
@Override @Override
public boolean cleanMwCache() { public boolean cleanMwCache() throws Exception {
log.info("停止 中间件 服务..."); log.info("停止 中间件 服务...");
runShell("sc stop " + AppCfg.MW); WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
ws.stopService();
}
log.info("退出 浏览器..."); log.info("退出 浏览器...");
runShell("taskkill /f /im " + AppCfg.BROWSER); runShell("taskkill /f /im " + AppCfg.BROWSER);
@ -121,24 +131,32 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
FileUtil.delAllFile(AppCfg.HZB + "/tomcat8/logs"); FileUtil.delAllFile(AppCfg.HZB + "/tomcat8/logs");
log.info("启动 中间件 服务..."); log.info("启动 中间件 服务...");
runShell("sc start " + AppCfg.MW); ws.startService();
return true; return true;
} }
@Override @Override
public boolean setMwSize(int size) { public boolean setMwSize(int size) throws Exception {
log.info("停止 中间件 服务..."); log.info("停止 中间件 服务...");
runShell("sc stop " + AppCfg.MW); WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
ws.stopService();
}
// TODO
log.info("启动 中间件 服务..."); log.info("启动 中间件 服务...");
runShell("sc start " + AppCfg.MW); ws.startService();
return true; return true;
} }
@Override @Override
public boolean registrationMwService() { public boolean registrationMwService() {
if(AppCfg.is32Bit){
runShell(AppCfg.HZB + "/tomcat8/webapps/qggwy/WEB-INF/classes/static/32bit.bat");
}else{
runShell(AppCfg.HZB + "/tomcat8/webapps/qggwy/WEB-INF/classes/static/64bit.bat");
}
return true; return true;
} }

1
src/main/java/com/insigma/utils/LinuxCommandUtil.java

@ -37,7 +37,6 @@ public class LinuxCommandUtil implements Runnable {
exitVal = process.waitFor(); exitVal = process.waitFor();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
log.error("执行命令发生异常:{}", e.getMessage()); log.error("执行命令发生异常:{}", e.getMessage());
e.printStackTrace();
} }
if (exitVal != 0) { if (exitVal != 0) {

1
src/main/java/com/insigma/utils/WinCommandUtil.java

@ -41,7 +41,6 @@ public class WinCommandUtil implements Runnable {
exitVal = process.waitFor(); exitVal = process.waitFor();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
log.error("执行命令发生异常:{}", e.getMessage()); log.error("执行命令发生异常:{}", e.getMessage());
e.printStackTrace();
} }
if (exitVal != 0) { if (exitVal != 0) {

12
src/main/java/com/insigma/utils/WinServiceTool.java

@ -1,8 +1,8 @@
package com.insigma.utils; package com.insigma.utils;
import com.alibaba.druid.util.StringUtils;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.thymeleaf.util.StringUtils;
import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -132,7 +132,7 @@ public class WinServiceTool {
InputStream inputStream = process.getInputStream(); InputStream inputStream = process.getInputStream();
result = convertInputStream2Str(inputStream, Charset.defaultCharset()); result = convertInputStream2Str(inputStream, Charset.defaultCharset());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.error("执行发生异常:{}", e.getMessage());
isExists = false; isExists = false;
runningStatus = ServiceState.UNKNOWN; runningStatus = ServiceState.UNKNOWN;
info = null; info = null;
@ -197,7 +197,7 @@ public class WinServiceTool {
Process exec = Runtime.getRuntime().exec("net start " + serviceName); Process exec = Runtime.getRuntime().exec("net start " + serviceName);
InputStream errorStream = exec.getErrorStream(); InputStream errorStream = exec.getErrorStream();
String errMsg = convertInputStream2Str(errorStream, getCharset()); String errMsg = convertInputStream2Str(errorStream, getCharset());
if (StringUtils.isEmpty(errMsg)) { if (!StringUtils.isEmpty(errMsg)) {
throw new Exception("执行启动服务[" + getServiceName() + "]出错:\n" + errMsg); throw new Exception("执行启动服务[" + getServiceName() + "]出错:\n" + errMsg);
} }
runningStatus = ServiceState.RUNNING; runningStatus = ServiceState.RUNNING;
@ -224,19 +224,19 @@ public class WinServiceTool {
Process exec = Runtime.getRuntime().exec("net stop " + serviceName); Process exec = Runtime.getRuntime().exec("net stop " + serviceName);
InputStream errorStream = exec.getErrorStream(); InputStream errorStream = exec.getErrorStream();
String errMsg = convertInputStream2Str(errorStream, getCharset()); String errMsg = convertInputStream2Str(errorStream, getCharset());
if (StringUtils.isEmpty(errMsg)) { if (!StringUtils.isEmpty(errMsg)) {
throw new Exception("执行停止服务[" + getServiceName() + "]出错:\n" + errMsg); throw new Exception("执行停止服务[" + getServiceName() + "]出错:\n" + errMsg);
} }
runningStatus = ServiceState.STOPPED; runningStatus = ServiceState.STOPPED;
ret = true; ret = true;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.error("执行发生异常:{}", e.getMessage());
} }
} }
return ret; return ret;
} }
private enum ServiceState { public enum ServiceState {
/** /**
* 正在运行 * 正在运行
*/ */

2
src/main/resources/application.properties

@ -11,4 +11,6 @@ db.pwd=admin
hy.db=GWY20_Mysql hy.db=GWY20_Mysql
hy.mw=GWY20_Tomcat hy.mw=GWY20_Tomcat
hy.hzb=D:/hzb2021 hy.hzb=D:/hzb2021
hy.code=GBK
hy.browser=360se.exe hy.browser=360se.exe
hy.os-arch=x86

1
src/main/resources/static/32b.bat → src/main/resources/static/32bit.bat

@ -1,3 +1,4 @@
%1 mshta vbscript:CreateObject(“Shell.Application”).ShellExecute(“cmd.exe”,``"/c %~s0 ::"``,``""``,“runas”,1)(window.close)&&exit
@Echo Off @Echo Off
Title 32位重置服务注册表手动设置安装路径 & Color 1A Title 32位重置服务注册表手动设置安装路径 & Color 1A
cd %systemroot%\system32 cd %systemroot%\system32

2
src/main/resources/static/64b.bat → src/main/resources/static/64bit.bat

@ -1,4 +1,4 @@
%1 mshta vbscript:CreateObject(“Shell.Application”).ShellExecute(“cmd.exe”,``"/c %~s0 ::"``,``""``,“runas”,1)(window.close)&&exit
@Echo Off @Echo Off
Title 32位重置服务注册表手动设置安装路径 & Color 1A Title 32位重置服务注册表手动设置安装路径 & Color 1A
cd %systemroot%\system32 cd %systemroot%\system32

7
src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java

@ -20,6 +20,7 @@ public class LinuxTongWebKingBaseTest {
AppCfg.DB = "GWY20_Mysql"; AppCfg.DB = "GWY20_Mysql";
AppCfg.MW = "GWY20_Tomcat"; AppCfg.MW = "GWY20_Tomcat";
AppCfg.HZB = "D:/hzb2021"; AppCfg.HZB = "D:/hzb2021";
AppCfg.CODE = "GBK";
AppCfg.BROWSER = "360se.exe"; AppCfg.BROWSER = "360se.exe";
} }
@ -32,7 +33,7 @@ public class LinuxTongWebKingBaseTest {
} }
@Test @Test
public void setDbSize() { public void setDbSize() throws Exception {
assertTrue(linuxTongWebKingBase.setDbSize(500)); assertTrue(linuxTongWebKingBase.setDbSize(500));
log.info("测试完成!"); log.info("测试完成!");
} }
@ -50,13 +51,13 @@ public class LinuxTongWebKingBaseTest {
} }
@Test @Test
public void cleanMwCache() { public void cleanMwCache() throws Exception {
assertTrue(linuxTongWebKingBase.cleanMwCache()); assertTrue(linuxTongWebKingBase.cleanMwCache());
log.info("测试完成!"); log.info("测试完成!");
} }
@Test @Test
public void setMwSize() { public void setMwSize() throws Exception {
assertTrue(linuxTongWebKingBase.setMwSize(500)); assertTrue(linuxTongWebKingBase.setMwSize(500));
log.info("测试完成!"); log.info("测试完成!");
} }

7
src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java

@ -19,6 +19,7 @@ public class WindowsTomcatMysqlTest {
AppCfg.DB = "GWY20_Mysql"; AppCfg.DB = "GWY20_Mysql";
AppCfg.MW = "GWY20_Tomcat"; AppCfg.MW = "GWY20_Tomcat";
AppCfg.HZB = "D:/hzb2021"; AppCfg.HZB = "D:/hzb2021";
AppCfg.CODE = "GBK";
AppCfg.BROWSER = "360se.exe"; AppCfg.BROWSER = "360se.exe";
} }
@ -31,7 +32,7 @@ public class WindowsTomcatMysqlTest {
} }
@Test @Test
public void setDbSize() { public void setDbSize() throws Exception {
assertTrue(windowsTomcatMysql.setDbSize(500)); assertTrue(windowsTomcatMysql.setDbSize(500));
log.info("测试完成!"); log.info("测试完成!");
} }
@ -49,13 +50,13 @@ public class WindowsTomcatMysqlTest {
} }
@Test @Test
public void cleanMwCache() { public void cleanMwCache() throws Exception {
assertTrue(windowsTomcatMysql.cleanMwCache()); assertTrue(windowsTomcatMysql.cleanMwCache());
log.info("测试完成!"); log.info("测试完成!");
} }
@Test @Test
public void setMwSize() { public void setMwSize() throws Exception {
assertTrue(windowsTomcatMysql.setMwSize(500)); assertTrue(windowsTomcatMysql.setMwSize(500));
log.info("测试完成!"); log.info("测试完成!");
} }

3
src/test/java/com/insigma/utils/WinServiceToolTest.java

@ -16,13 +16,14 @@ public class WinServiceToolTest {
AppCfg.DB = "GWY20_Mysql"; AppCfg.DB = "GWY20_Mysql";
AppCfg.MW = "GWY20_Tomcat"; AppCfg.MW = "GWY20_Tomcat";
AppCfg.HZB = "D:/hzb2021"; AppCfg.HZB = "D:/hzb2021";
AppCfg.CODE = "GBK";
AppCfg.BROWSER = "360se.exe"; AppCfg.BROWSER = "360se.exe";
} }
@Test @Test
public void getService() throws Exception { public void getService() throws Exception {
WinServiceTool.WindowsService windowsService = WinServiceTool.getService(AppCfg.DB, "GBK"); WinServiceTool.WindowsService windowsService = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE);
log.info("停止代码:{}", windowsService); log.info("停止代码:{}", windowsService);
assertTrue(windowsService.stopService()); assertTrue(windowsService.stopService());
log.info("启动代码:{}", windowsService); log.info("启动代码:{}", windowsService);

Loading…
Cancel
Save