diff --git a/src/main/java/com/insigma/HyToolApplication.java b/src/main/java/com/insigma/HyToolApplication.java index 227e79a..2cacdc5 100644 --- a/src/main/java/com/insigma/HyToolApplication.java +++ b/src/main/java/com/insigma/HyToolApplication.java @@ -32,6 +32,7 @@ public class HyToolApplication { String url = loadProperties.getProperty("db.url"); String usr = loadProperties.getProperty("db.usr"); String pwd = loadProperties.getProperty("db.pwd"); + String drive = loadProperties.getProperty("db.drive"); AppCfg.DB = loadProperties.getProperty("hy.db"); AppCfg.MW = loadProperties.getProperty("hy.mw"); @@ -45,7 +46,7 @@ public class HyToolApplication { log.info("正在启动GUI =======================>>> 成功!"); SwingSet2.main(args); }); - Session session = DbUtil.getSession(url, usr, pwd); + Session session = DbUtil.getSession(url, usr, pwd, drive); log.info("获取{}连接,进行操作!", session); session.close(); } catch (Exception e) { diff --git a/src/main/java/com/insigma/entry/IndexObj.java b/src/main/java/com/insigma/entry/IndexObj.java index 44b8f9c..27e5b41 100644 --- a/src/main/java/com/insigma/entry/IndexObj.java +++ b/src/main/java/com/insigma/entry/IndexObj.java @@ -17,8 +17,8 @@ import java.util.List; public class IndexObj { private String tab; - private String col; private String idx; + private String col; /** * 获取 预置 数据 diff --git a/src/main/java/com/insigma/service/Computer.java b/src/main/java/com/insigma/service/Computer.java index 75190c5..771003e 100644 --- a/src/main/java/com/insigma/service/Computer.java +++ b/src/main/java/com/insigma/service/Computer.java @@ -11,7 +11,12 @@ public abstract class Computer { /*** * 执行 脚本程序 * @param shell 执行脚本 - * @return + * @return 执行执行脚本成功 */ public abstract boolean runShell(String shell); + /*** + * 执行 备份操作 + * @return 是否备份成功 + */ + public abstract boolean runBak(); } diff --git a/src/main/java/com/insigma/service/Database.java b/src/main/java/com/insigma/service/Database.java index aadfd91..658d1c8 100644 --- a/src/main/java/com/insigma/service/Database.java +++ b/src/main/java/com/insigma/service/Database.java @@ -29,7 +29,7 @@ public interface Database { * @param size 设置大小值 * @return */ - default boolean setDbSize(int size) throws Exception { + default boolean setDbSize(int ...size) throws Exception { System.out.printf("设置出参数:%d,但是未检测到实现应用调用此方法!", size); return false; } diff --git a/src/main/java/com/insigma/service/Middleware.java b/src/main/java/com/insigma/service/Middleware.java index 98c5a63..5c5a265 100644 --- a/src/main/java/com/insigma/service/Middleware.java +++ b/src/main/java/com/insigma/service/Middleware.java @@ -18,11 +18,11 @@ public interface Middleware { } /*** * 设置 中间件 大小 - * @param size 设置大小值 + * @param size * @return */ - default boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) throws Exception { - System.out.printf("设置应用参数:%d,%d,%d,%d,但是未检测到实现应用调用此方法!", permSize, maxPermSize, xms, xmx); + default boolean setMwSize(int ...size) throws Exception { + System.out.printf("设置应用参数:%d,%d,%d,%d,但是未检测到实现应用调用此方法!", size); return false; } diff --git a/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java b/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java index 0b9be29..083e067 100644 --- a/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java +++ b/src/main/java/com/insigma/service/impl/LinuxTongWebKingBase.java @@ -1,13 +1,18 @@ package com.insigma.service.impl; +import cn.hutool.db.Session; +import com.insigma.config.AppCfg; import com.insigma.entry.IndexObj; import com.insigma.entry.TabColObj; import com.insigma.service.Computer; import com.insigma.service.Database; import com.insigma.service.Middleware; +import com.insigma.utils.DbUtil; +import com.insigma.utils.FileUtil; import com.insigma.utils.LinuxCommandUtil; import lombok.extern.slf4j.Slf4j; +import java.sql.SQLException; import java.util.List; /** @@ -30,38 +35,158 @@ public class LinuxTongWebKingBase extends Computer implements Database, Middlewa return true; } + @Override + public boolean runBak() { + return false; + } + @Override public boolean rebuildIndex(List index) { - return true; + boolean retBool; + 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", idx.getIdx()); + log.debug(sql); + session.execute(sql); + }finally { + sql = String.format( "CREATE INDEX %s on %s (%s)", idx.getIdx(), idx.getTab(), 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; } + /*** + * 设置数据库参数 :注意参数的个数和位置 + * @param size 顺序为 shared_buffers, effective_cache_size + * @return + * @throws Exception + */ @Override - public boolean setDbSize(int size) { + public boolean setDbSize(int ...size) { + if(size == null || size.length!=2){ + log.error("请检测参数是否正确..."); + return false; + } + log.info("停止 数据库 服务..."); + runShell("systemctl stop " + AppCfg.DB); + + log.info("设置 数据库 内存参数..."); + FileUtil.replaceLine(AppCfg.HZB + "/kingbase/bin//TEST/kingbase.conf", + "shared_buffers", + String.format("shared_buffers=%dGB", size[0])); + FileUtil.replaceLine(AppCfg.HZB + "/kingbase/bin//TEST/kingbase.conf", + "effective_cache_size", + String.format("effective_cache_size=%dGB", size[1])); + + log.info("启动 数据库 服务..."); + runShell("systemctl start " + AppCfg.DB); return true; } @Override - public boolean cleanDbCache(List sql, List tabColObjs) { - return true; + public boolean cleanDbCache(List sqls, List tabColObjs) { + boolean retBool; + log.info("创建链接..."); + final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); + try { + log.info("执行 预置 SQL 脚本..."); + for (String sql : sqls) { + log.debug(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; + } + log.info("执行 清洗数据中特殊字符串 完成..."); + session.close(); + return retBool; } @Override public boolean registrationDbService() { + // TODO 无法实现 return true; } @Override public boolean cleanMwCache() { + log.info("停止 中间件 服务..."); + runShell("systemctl stop " + AppCfg.MW); + + // TODO 无法确定浏览器 + log.info("退出 浏览器..."); + log.info("删除 浏览器 历史文件..."); + log.info("解压 新浏览器 文件..."); + + log.info("删除 应用 缓存文件..."); + runShell(String.format("rm -rf %s/TongWeb7/temp", AppCfg.HZB)); + + log.info("删除 应用 日志文件..."); + runShell(String.format("rm -rf %s/TongWeb7/logs", AppCfg.HZB)); + + log.info("启动 中间件 服务..."); + runShell("systemctl start " + AppCfg.MW); return true; } + /*** + * 设置中间件参数 :注意参数的个数和位置 + * @param size 顺序为 xms, xmx + * @return + * @throws Exception + */ @Override - public boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) { + public boolean setMwSize(int ...size) { + if(size == null || size.length!=2){ + log.error("请检测参数是否正确..."); + return false; + } + log.info("停止 中间件 服务..."); + runShell("systemctl stop " + AppCfg.MW); + + log.info("设置 数据库 内存参数..."); + FileUtil.replaceLine(AppCfg.HZB + "/bin/external.vmoptions", + "-Xms", + String.format("Xms%dm", size[0])); + FileUtil.replaceLine(AppCfg.HZB + "/bin/external.vmoptions", + "-Xmx", + String.format("-Xmx%dm", size[1])); + + log.info("启动 中间件 服务..."); + runShell("systemctl start " + AppCfg.MW); return true; } @Override public boolean registrationMwService() { + // TODO 无法实现 return true; } } diff --git a/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java b/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java index f7ce2e2..ad6ead5 100644 --- a/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java +++ b/src/main/java/com/insigma/service/impl/WindowsTomcatMysql.java @@ -33,15 +33,28 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware return true; } + @Override + public boolean runBak() { + return false; + } + @Override public boolean rebuildIndex(List index) { boolean retBool; log.info("创建链接..."); - final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd); + final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); + String sql; try { for (IndexObj idx : index) { - session.execute("DROP INDEX ? ON ?", idx.getIdx(), idx.getTab()); - session.execute("ALTER TABLE ? ADD INDEX ? (?) USING BTREE", idx.getTab(), idx.getIdx(), idx.getCol()); + 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) { @@ -53,8 +66,18 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware return retBool; } + /*** + * 设置数据库参数 :注意参数的个数和位置 + * @param size 顺序为 innodb_buffer_pool_size + * @return + * @throws Exception + */ @Override - public boolean setDbSize(int size) throws Exception { + public boolean setDbSize(int ...size) throws Exception { + if(size == null || size.length!=1){ + log.error("请检测参数是否正确..."); + return false; + } log.info("停止 数据库 服务..."); WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.DB, AppCfg.CODE); if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ @@ -64,7 +87,7 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware log.info("设置 数据库 内存参数..."); FileUtil.replaceLine(AppCfg.HZB + "/mysql/my.ini", "innodb_buffer_pool_size", - String.format("innodb_buffer_pool_size=%dM", size)); + String.format("innodb_buffer_pool_size=%dM", size[0])); log.info("启动 数据库 服务..."); ws.startService(); @@ -72,20 +95,24 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware } @Override - public boolean cleanDbCache(List sql, List tabColObjs) { + public boolean cleanDbCache(List sqls, List tabColObjs) { boolean retBool; log.info("创建链接..."); - final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd); + final Session session = DbUtil.getSession(DbUtil.url, DbUtil.usr, DbUtil.pwd, DbUtil.drive); try { log.info("执行 预置 SQL 脚本..."); - for (String s : sql) { - session.execute(s); + for (String sql : sqls) { + log.debug(sql); + session.execute(sql); } log.info("执行 清洗数据中特殊字符串 脚本..."); + String sql; for (String lj : DbUtil.RUBBISH) { for (TabColObj tab : tabColObjs) { - session.execute("update ? set ?=replace(?, '?', '') where ? is not null", + 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; @@ -150,15 +177,25 @@ public class WindowsTomcatMysql extends Computer implements Database, Middleware return true; } + /*** + * 设置中间件参数 :注意参数的个数和位置 + * @param size 顺序为 permSize, maxPermSize, xms, xmx + * @return + * @throws Exception + */ @Override - public boolean setMwSize(int permSize, int maxPermSize, int xms, int xmx) throws Exception { + public boolean setMwSize(int ...size) throws Exception { + if(size == null || size.length!=4){ + log.error("请检测参数是否正确..."); + return false; + } log.info("停止 中间件 服务..."); WinServiceTool.WindowsService ws = WinServiceTool.getService(AppCfg.MW, AppCfg.CODE); if(ws.getRunningStatus() == WinServiceTool.WindowsService.ServiceState.RUNNING){ ws.stopService(); } - if(WinCommandUtil.RegisterUtil.registerJava(AppCfg.HZB, permSize, maxPermSize, xms, xmx)){ + if(WinCommandUtil.RegisterUtil.registerJava(AppCfg.HZB, size[0], size[1], size[2], size[3])){ return false; } diff --git a/src/main/java/com/insigma/utils/DbUtil.java b/src/main/java/com/insigma/utils/DbUtil.java index a92460d..bdeeb52 100644 --- a/src/main/java/com/insigma/utils/DbUtil.java +++ b/src/main/java/com/insigma/utils/DbUtil.java @@ -21,13 +21,15 @@ public class DbUtil { public static String url; public static String usr; public static String pwd; + public static String drive; - public static Session getSession(String url, String usr, String pwd) { + public static Session getSession(String url, String usr, String pwd, String drive) { if(Objects.isNull(ds)){ DbUtil.url = url; DbUtil.usr = url; DbUtil.pwd = url; - ds = new SimpleDataSource(isDb(url), usr, pwd); + DbUtil.drive = drive; + ds = new SimpleDataSource(isDb(url), usr, pwd, drive); } return Session.create(ds); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b41e7f0..b29bfd4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,6 +4,7 @@ spring.profiles.active=dev swing.ui.testCN=测试 +db.drive=com.mysql.jdbc.Driver db.url=jdbc:mysql://127.0.0.1:35017/hy_qggwy db.usr=root db.pwd=admin diff --git a/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java b/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java index 87ea2bc..2362242 100644 --- a/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java +++ b/src/test/java/com/insigma/service/impl/LinuxTongWebKingBaseTest.java @@ -14,14 +14,15 @@ import static org.junit.jupiter.api.Assertions.*; public class LinuxTongWebKingBaseTest { static { - DbUtil.url = "jdbc:mysql://127.0.0.1:35017/hy_qggwy"; - DbUtil.usr = "root"; - DbUtil.pwd = "admin"; - AppCfg.DB = "GWY20_Mysql"; - AppCfg.MW = "GWY20_Tomcat"; - AppCfg.HZB = "D:/hzb2021"; + DbUtil.drive = "com.kingbase8.Driver"; + DbUtil.url = "jdbc:kingbase8://192.168.1.111:54321/hy_qggwy"; + DbUtil.usr = "hy_qggwy"; + DbUtil.pwd = "hy_qggwy"; + AppCfg.DB = "qggwy-kingbase.service"; + AppCfg.MW = "qggwy.service"; + AppCfg.HZB = "/opt/apps/cn.com.epsoft.qggwy/files/qggwy"; AppCfg.CODE = "GBK"; - AppCfg.BROWSER = "360se.exe"; + AppCfg.BROWSER = ""; } private LinuxTongWebKingBase linuxTongWebKingBase = new LinuxTongWebKingBase(); @@ -33,7 +34,7 @@ public class LinuxTongWebKingBaseTest { } @Test - public void setDbSize() throws Exception { + public void setDbSize() { assertTrue(linuxTongWebKingBase.setDbSize(500)); log.info("测试完成!"); } @@ -51,13 +52,13 @@ public class LinuxTongWebKingBaseTest { } @Test - public void cleanMwCache() throws Exception { + public void cleanMwCache() { assertTrue(linuxTongWebKingBase.cleanMwCache()); log.info("测试完成!"); } @Test - public void setMwSize() throws Exception { + public void setMwSize() { assertTrue(linuxTongWebKingBase.setMwSize(128,256,512,512)); log.info("测试完成!"); } diff --git a/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java b/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java index 68b857c..e8c5d94 100644 --- a/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java +++ b/src/test/java/com/insigma/service/impl/WindowsTomcatMysqlTest.java @@ -13,6 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class WindowsTomcatMysqlTest { static { + DbUtil.drive = "com.mysql.jdbc.Driver"; DbUtil.url = "jdbc:mysql://127.0.0.1:35017/hy_qggwy"; DbUtil.usr = "root"; DbUtil.pwd = "admin"; diff --git a/src/test/java/com/insigma/utils/DbUtilTest.java b/src/test/java/com/insigma/utils/DbUtilTest.java index e13bdce..5b3b39b 100644 --- a/src/test/java/com/insigma/utils/DbUtilTest.java +++ b/src/test/java/com/insigma/utils/DbUtilTest.java @@ -9,7 +9,7 @@ public class DbUtilTest { @Test public void getSessionTest() throws SQLException { - Session session = DbUtil.getSession("jdbc:mysql://127.0.0.1:35017/hy_qggwy", "root", "admin"); + Session session = DbUtil.getSession("jdbc:mysql://127.0.0.1:35017/hy_qggwy", "root", "admin", "com.mysql.jdbc.Driver"); String queryString = session.queryString("select count(1) from a01 limit 1"); assertNotEquals("0", queryString); }