Browse Source

增加服务检测状态控制

master
VIVIMAN 3 years ago
parent
commit
77d45c3528
  1. 12
      src/main/java/com/insigma/entry/TabColObj.java
  2. 8
      src/main/java/com/insigma/service/Computer.java
  3. 20
      src/main/java/com/insigma/service/Database.java
  4. 22
      src/main/java/com/insigma/service/Middleware.java
  5. 33
      src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java
  6. 208
      src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java
  7. 20
      src/main/java/com/insigma/utils/FileUtil.java
  8. 12
      src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java
  9. 36
      src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java

12
src/main/java/com/insigma/entry/TabColObj.java

@ -21,7 +21,17 @@ public class TabColObj {
private String col; private String col;
public static List<String> getSQL(){ public static List<String> getSQL(){
return new ArrayList<>(); return Arrays.asList(
"",
"",
"",
"",
"",
"",
"",
"",
""
);
} }
public static List<TabColObj> getData(){ public static List<TabColObj> getData(){
return Arrays.asList( return Arrays.asList(

8
src/main/java/com/insigma/service/Computer.java

@ -18,5 +18,11 @@ public abstract class Computer {
* 执行 备份操作 * 执行 备份操作
* @return 是否备份成功 * @return 是否备份成功
*/ */
public abstract boolean runBak(); public abstract boolean runBak() throws Exception;
/***
* 执行 还原操作
* @param path 还原文件路径
* @return 是否备份成功
*/
public abstract boolean runRestore(String path);
} }

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

@ -30,7 +30,7 @@ public interface Database {
* @return * @return
*/ */
default boolean setDbSize(int ...size) throws Exception { default boolean setDbSize(int ...size) throws Exception {
System.out.printf("设置出参数:%d,但是未检测到实现应用调用此方法!", size); System.out.println(String.format("设置出参数:%d,但是未检测到实现应用调用此方法!", size));
return false; return false;
} }
/*** /***
@ -52,4 +52,22 @@ public interface Database {
System.out.println("进行数据库注册服务,但是未检测到实现应用调用此方法!"); System.out.println("进行数据库注册服务,但是未检测到实现应用调用此方法!");
return false; return false;
} }
/**
* 实现数据库服务启动
* @return
*/
default boolean startDbService() throws Exception {
System.out.println("进行数据库服务启动,但是未检测到实现应用调用此方法!");
return false;
}
/**
* 实现数据库服务停止
* @return
*/
default boolean stopDbService() throws Exception {
System.out.println("进行数据库服务停止,但是未检测到实现应用调用此方法!");
return false;
}
} }

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

@ -13,7 +13,7 @@ public interface Middleware {
* @return * @return
*/ */
default boolean cleanMwCache() throws Exception { default boolean cleanMwCache() throws Exception {
System.out.printf("清除缓存,但是未检测到实现应用调用此方法!"); System.out.println("清除缓存,但是未检测到实现应用调用此方法!");
return false; return false;
} }
/*** /***
@ -22,7 +22,7 @@ public interface Middleware {
* @return * @return
*/ */
default boolean setMwSize(int ...size) throws Exception { default boolean setMwSize(int ...size) throws Exception {
System.out.printf("设置应用参数:%d,%d,%d,%d,但是未检测到实现应用调用此方法!", size); System.out.println(String.format("设置应用参数:%d,%d,%d,%d,但是未检测到实现应用调用此方法!", size));
return false; return false;
} }
@ -34,4 +34,22 @@ public interface Middleware {
System.out.println("进行中间件注册服务,但是未检测到实现应用调用此方法!"); System.out.println("进行中间件注册服务,但是未检测到实现应用调用此方法!");
return false; return false;
} }
/**
* 实现中间件服务启动
* @return
*/
default boolean startMwService() throws Exception {
System.out.println("进行中间件服务启动,但是未检测到实现应用调用此方法!");
return false;
}
/**
* 实现中间件服务停止
* @return
*/
default boolean stopMwService() throws Exception {
System.out.println("进行中间件服务停止,但是未检测到实现应用调用此方法!");
return false;
}
} }

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

@ -40,6 +40,11 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa
return false; return false;
} }
@Override
public boolean runRestore(String path) {
return false;
}
@Override @Override
public boolean rebuildIndex(List<IndexObj> index) { public boolean rebuildIndex(List<IndexObj> index) {
boolean retBool; boolean retBool;
@ -184,6 +189,34 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa
return true; return true;
} }
@Override
public boolean startDbService() {
log.info("启动 数据库 服务...");
runShell("systemctl start " + AppCfg.DB);
return true;
}
@Override
public boolean stopDbService() {
log.info("停止 数据库 服务...");
runShell("systemctl stop " + AppCfg.DB);
return true;
}
@Override
public boolean startMwService() {
log.info("启动 中间件 服务...");
runShell("systemctl start " + AppCfg.MW);
return true;
}
@Override
public boolean stopMwService() {
log.info("停止 中间件 服务...");
runShell("systemctl stop " + AppCfg.MW);
return true;
}
@Override @Override
public boolean registrationMwService() { public boolean registrationMwService() {
// TODO 无法实现 // TODO 无法实现

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

@ -9,7 +9,10 @@ import com.insigma.service.*;
import com.insigma.utils.*; import com.insigma.utils.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
@ -34,35 +37,101 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
} }
@Override @Override
public boolean runBak() { public boolean runBak() throws Exception {
return false; boolean retBool;
log.info("创建备份文件目录...");
String format = DateTimeFormatter.BASIC_ISO_DATE.format(LocalDate.now());
boolean b = FileUtil.mkDirectory(AppCfg.HZB + File.separatorChar + format);
if(b){
log.info("创建备份目录:{}", format);
WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING) {
log.info("备份照片文件...");
copyFileByA57(format);
log.info("停止 数据库 服务...");
ws.stopService();
log.info("备份压缩数据库文件...");
ZipUtil.zip7Z(
AppCfg.HZB + File.separatorChar + "mysql" + File.separatorChar + "data",
AppCfg.HZB + File.separatorChar + format + File.separatorChar + "data.gz");
log.info("备份压缩备份文件...");
ZipUtil.zip7Z(
AppCfg.HZB + File.separatorChar + "format",
AppCfg.HZB + File.separatorChar + format + ".gz");
log.info("启动 数据库 服务...");
ws.startService();
}
retBool = true;
} else {
retBool = false;
}
return retBool;
} }
@Override private void copyFileByA57(String format) {
public boolean rebuildIndex(List<IndexObj> index) {
boolean retBool;
log.info("创建链接..."); log.info("创建链接...");
final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive);
String sql;
try { try {
for (IndexObj idx : index) { String sql = "select concat('tomcat8/HZBPHOTOS/', PHOTOPATH, '/', PHOTONAME) fileName from a57 where exists (select 1 from a01 where a01.a0000=a57.a0000 and status=1)";
try { List<String> query = session.query(sql, String.class);
sql = String.format("DROP INDEX %s ON %s", idx.getIdx(), idx.getTab()); String from, to;
log.debug(sql); for (int i = 0, j = query.size(); i < j; i++) {
session.execute(sql); from = AppCfg.HZB + query.get(i);
}finally { to = AppCfg.HZB + format + query.get(i);
sql = String.format("ALTER TABLE %s ADD INDEX %s (%s) USING BTREE", idx.getTab(), idx.getIdx(), idx.getCol()); cn.hutool.core.io.FileUtil.copy(from, to, false);
log.debug(sql);
session.execute(sql);
}
} }
retBool = true;
} catch (SQLException e) { } catch (SQLException e) {
log.error("执行更新索引发生异常:{}", e.getMessage()); log.error("执行查询报错:{}",e.getMessage());
session.close(); e.printStackTrace();
}
}
@Override
public boolean runRestore(String path) {
boolean retBool;
boolean b = cn.hutool.core.io.FileUtil.isFile(path);
if(b){
log.info("判断文件:{}...存在:{}", path, b);
retBool = true;
}else{
log.error("判断文件:{}...不存在,请检查!", path);
retBool = false; retBool = false;
} }
session.close(); return retBool;
}
@Override
public boolean rebuildIndex(List<IndexObj> index) {
boolean retBool = false;
WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
log.info("创建链接...");
final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive);
String sql;
try {
for (IndexObj idx : index) {
try {
sql = String.format("DROP INDEX %s ON %s", idx.getIdx(), idx.getTab());
log.debug(sql);
session.execute(sql);
}finally {
sql = String.format("ALTER TABLE %s ADD INDEX %s (%s) USING BTREE", idx.getTab(), idx.getIdx(), idx.getCol());
log.debug(sql);
session.execute(sql);
}
}
retBool = true;
} catch (SQLException e) {
log.error("执行更新索引发生异常:{}", e.getMessage());
session.close();
retBool = false;
}
session.close();
}
return retBool; return retBool;
} }
@ -96,34 +165,37 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
@Override @Override
public boolean cleanDbCache(List<String> sqls, List<TabColObj> tabColObjs) { public boolean cleanDbCache(List<String> sqls, List<TabColObj> tabColObjs) {
boolean retBool; boolean retBool = false;
log.info("创建链接..."); WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE);
final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING) {
try { log.info("创建链接...");
log.info("执行 预置 SQL 脚本..."); final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive);
for (String sql : sqls) { try {
log.debug(sql); log.info("执行 预置 SQL 脚本...");
session.execute(sql); for (String sql : sqls) {
}
log.info("执行 清洗数据中特殊字符串 脚本...");
String sql;
for (String lj : DbUtil.RUBBISH) {
for (TabColObj tab : tabColObjs) {
sql = String.format("update %s set %s=replace(%s, '%s', '') where %s is not null",
tab.getTab(), tab.getCol(), tab.getCol(), lj, tab.getCol());
log.debug(sql); log.debug(sql);
session.execute(sql); session.execute(sql);
} }
log.info("执行 清洗数据中特殊字符串 脚本...");
String sql;
for (String lj : DbUtil.RUBBISH) {
for (TabColObj tab : tabColObjs) {
sql = String.format("update %s set %s=replace(%s, '%s', '') where %s is not null",
tab.getTab(), tab.getCol(), tab.getCol(), lj, tab.getCol());
log.debug(sql);
session.execute(sql);
}
}
retBool = true;
} catch (SQLException e) {
log.error("执行 清洗数据中特殊字符串 发生异常:{}", e.getMessage());
e.printStackTrace();
session.close();
retBool = false;
} }
retBool = true; log.info("执行 清洗数据中特殊字符串 完成...");
} catch (SQLException e) {
log.error("执行 清洗数据中特殊字符串 发生异常:{}", e.getMessage());
e.printStackTrace();
session.close(); session.close();
retBool = false;
} }
log.info("执行 清洗数据中特殊字符串 完成...");
session.close();
return retBool; return retBool;
} }
@ -243,6 +315,58 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
return true; return true;
} }
@Override
public boolean startDbService() throws Exception {
log.info("启动 数据库 服务...");
WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.STOPPED){
ws.startService();
return true;
}
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
ws.restartService();
return true;
}
return false;
}
@Override
public boolean stopDbService() throws Exception {
log.info("停止 数据库 服务...");
WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
ws.stopService();
return true;
}
return false;
}
@Override
public boolean startMwService() throws Exception {
log.info("启动 中间件 服务...");
WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.STOPPED){
ws.startService();
return true;
}
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
ws.restartService();
return true;
}
return false;
}
@Override
public boolean stopMwService() throws Exception {
log.info("停止 中间件 服务...");
WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE);
if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){
ws.stopService();
return true;
}
return false;
}
public void openServer(){ public void openServer(){
runShell("SERVICES.MSC"); runShell("SERVICES.MSC");
log.info("打开本地服务完成!"); log.info("打开本地服务完成!");

20
src/main/java/com/insigma/utils/FileUtil.java

@ -40,7 +40,25 @@ public class FileUtil {
} }
} }
/**
* 按照路径创建目录
* @param path 路径名称
* @return
*/
public static boolean mkDirectory(String path) {
boolean bool;
try {
File file = new File(path);
if (!file.exists()) {
bool = file.mkdirs();
} else {
bool = false;
}
} catch (Exception e) {
bool = false;
}
return bool;
}
/** /**
* 删除整个文件夹里的内容 * 删除整个文件夹里的内容

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

@ -74,4 +74,16 @@ public class LinuxTongWebKingBaseTest {
assertTrue(linuxTongWebKingBase.runShell("vim a.txt")); assertTrue(linuxTongWebKingBase.runShell("vim a.txt"));
log.info("测试完成!"); log.info("测试完成!");
} }
@Test
public void runBak() {
assertTrue(linuxTongWebKingBase.runBak());
log.info("测试完成!");
}
@Test
public void runRestore() {
assertTrue(linuxTongWebKingBase.runRestore(""));
log.info("测试完成!");
}
} }

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

@ -74,6 +74,42 @@ public class WindowsTomcatMysqlTest {
log.info("测试完成!"); log.info("测试完成!");
} }
@Test
public void runBak() throws Exception {
assertTrue(windowsTomcatMysql.runBak());
log.info("测试完成!");
}
@Test
public void runRestore() {
assertTrue(windowsTomcatMysql.runRestore(""));
log.info("测试完成!");
}
@Test
public void startDbService() throws Exception {
assertTrue(windowsTomcatMysql.startDbService());
log.info("测试完成!");
}
@Test
public void stopDbService() throws Exception {
assertTrue(windowsTomcatMysql.stopDbService());
log.info("测试完成!");
}
@Test
public void startMwService() throws Exception {
assertTrue(windowsTomcatMysql.startMwService());
log.info("测试完成!");
}
@Test
public void stopMwService() throws Exception {
assertTrue(windowsTomcatMysql.stopMwService());
log.info("测试完成!");
}
@Test @Test
public void openServer() { public void openServer() {
windowsTomcatMysql.openServer(); windowsTomcatMysql.openServer();

Loading…
Cancel
Save