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. 132
      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;
public static List<String> getSQL(){
return new ArrayList<>();
return Arrays.asList(
"",
"",
"",
"",
"",
"",
"",
"",
""
);
}
public static List<TabColObj> getData(){
return Arrays.asList(

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

@ -18,5 +18,11 @@ public abstract class Computer {
* 执行 备份操作
* @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
*/
default boolean setDbSize(int ...size) throws Exception {
System.out.printf("设置出参数:%d,但是未检测到实现应用调用此方法!", size);
System.out.println(String.format("设置出参数:%d,但是未检测到实现应用调用此方法!", size));
return false;
}
/***
@ -52,4 +52,22 @@ public interface Database {
System.out.println("进行数据库注册服务,但是未检测到实现应用调用此方法!");
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
*/
default boolean cleanMwCache() throws Exception {
System.out.printf("清除缓存,但是未检测到实现应用调用此方法!");
System.out.println("清除缓存,但是未检测到实现应用调用此方法!");
return false;
}
/***
@ -22,7 +22,7 @@ public interface Middleware {
* @return
*/
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;
}
@ -34,4 +34,22 @@ public interface Middleware {
System.out.println("进行中间件注册服务,但是未检测到实现应用调用此方法!");
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;
}
@Override
public boolean runRestore(String path) {
return false;
}
@Override
public boolean rebuildIndex(List<IndexObj> index) {
boolean retBool;
@ -184,6 +189,34 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa
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
public boolean registrationMwService() {
// TODO 无法实现

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

@ -9,7 +9,10 @@ import com.insigma.service.*;
import com.insigma.utils.*;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
@ -34,13 +37,78 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
}
@Override
public boolean runBak() {
return false;
public boolean runBak() throws Exception {
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;
}
private void copyFileByA57(String format) {
log.info("创建链接...");
final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive);
try {
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)";
List<String> query = session.query(sql, String.class);
String from, to;
for (int i = 0, j = query.size(); i < j; i++) {
from = AppCfg.HZB + query.get(i);
to = AppCfg.HZB + format + query.get(i);
cn.hutool.core.io.FileUtil.copy(from, to, false);
}
} catch (SQLException e) {
log.error("执行查询报错:{}",e.getMessage());
e.printStackTrace();
}
}
@Override
public boolean rebuildIndex(List<IndexObj> index) {
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;
}
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;
@ -63,6 +131,7 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
retBool = false;
}
session.close();
}
return retBool;
}
@ -96,7 +165,9 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
@Override
public boolean cleanDbCache(List<String> sqls, List<TabColObj> tabColObjs) {
boolean retBool;
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);
try {
@ -124,6 +195,7 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
}
log.info("执行 清洗数据中特殊字符串 完成...");
session.close();
}
return retBool;
}
@ -243,6 +315,58 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware
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(){
runShell("SERVICES.MSC");
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"));
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("测试完成!");
}
@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
public void openServer() {
windowsTomcatMysql.openServer();

Loading…
Cancel
Save