andy 2 жил өмнө
parent
commit
b83e5455eb

+ 24 - 17
mainFactory/src/main/java/org/bfkj/MainFactoryApplication.java

@@ -13,6 +13,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.core.env.Environment;
+import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 
 import java.io.File;
@@ -23,7 +24,13 @@ import java.util.*;
  */
 
 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
+@EnableScheduling
 public class MainFactoryApplication {
+
+   private final String INSERT_SQL = "INSERT INTO log_success ( success, location, logContent, createtime, serviceId, workId, event, iNDataContent, outDataContent, calculationLocation, dataObjectId) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
+    private final  String ERROR_SQL = "INSERT INTO log_error ( success, location, logContent, createtime, serviceId, workId, event, iNDataContent, outDataContent, calculationLocation, dataObjectId) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
+    private final  String FULL_DATA_SQL = "INSERT INTO log_fulldata ( dataObjectId, FULL_DATA_SQL) VALUES (?,?)";
+
     /*服务初始化: 启动服务*/
     public static void main(String[] args) {
         String path = System.getProperty("user.dir"); //获取当前项目的路径
@@ -200,7 +207,7 @@ public class MainFactoryApplication {
         try {
             myDbHelper.execute("CREATE TABLE if not EXISTS `log_success` (  `id` int PRIMARY KEY  NOT NULL  ,  `success` TEXT  DEFAULT NULL,  `location` TEXT DEFAULT NULL,  `logContent` TEXT,  `serviceId`INT  DEFAULT NULL,  `workId` INT  DEFAULT NULL,  `iNDataContent` TEXT DEFAULT NULL, `outDataContent` TEXT DEFAULT NULL,`calculationLocation` TEXT DEFAULT NULL, `dataObjectId` TEXT DEFAULT NULL,`event` TEXT DEFAULT NULL,`createtime` timestamp not null default current_timestamp)");
             myDbHelper.execute("CREATE TABLE if not EXISTS `log_error` (  `id` int  PRIMARY KEY  NOT NULL   ,  `success` TEXT  DEFAULT NULL,  `location` TEXT DEFAULT NULL,  `logContent` TEXT,  `serviceId` INT  DEFAULT NULL,  `workId` INT  DEFAULT NULL,  `iNDataContent` TEXT DEFAULT NULL, `outDataContent` TEXT DEFAULT NULL,`calculationLocation` TEXT DEFAULT NULL,  `dataObjectId` TEXT DEFAULT NULL,`event` TEXT DEFAULT NULL,`createtime` timestamp not null default current_timestamp)");
-            myDbHelper.execute("CREATE TABLE if not EXISTS `log_fulldata` (  `id` int(11)  NOT NULL auto_increment  PRIMARY KEY,  `dataObjectId `TEXT  DEFAULT NULL,`fullData` TEXT DEFAULT NULL)");
+            myDbHelper.execute("CREATE TABLE if not EXISTS `log_fulldata` (  `id` int(11)  NOT NULL auto_increment  PRIMARY KEY,  `dataObjectId `TEXT  DEFAULT NULL,`FULL_DATA_SQL` TEXT DEFAULT NULL)");
         } catch (Exception e) {
             return false;
         }
@@ -258,7 +265,7 @@ public class MainFactoryApplication {
             //远程表日志表结构结构
             myDbHelper.execute("CREATE TABLE if not EXISTS `log_success` (  `id` int(11)  NOT NULL auto_increment  PRIMARY KEY,  `success` varchar(8)  DEFAULT NULL,  `location` varchar(255) DEFAULT NULL,  `logContent` varchar(1024)  DEFAULT NULL,  `serviceId` int(11) DEFAULT NULL,  `workId` int(11) DEFAULT NULL,`event` varchar(2) DEFAULT NULL, `iNDataContent` varchar(2048) DEFAULT NULL, `outDataContent` varchar(2048) DEFAULT NULL,`calculationLocation` varchar(8)  DEFAULT NULL,   `dataObjectId` varchar(32) DEFAULT NULL,`createtime` datetime )");
             myDbHelper.execute("CREATE TABLE if not EXISTS `log_error` (  `id` int(11)  NOT NULL auto_increment  PRIMARY KEY,  `success` varchar(8)  DEFAULT NULL,  `location` varchar(255) DEFAULT NULL, `logContent` varchar(1024)  DEFAULT NULL,  `serviceId` int(11) DEFAULT NULL,  `workId` int(11) DEFAULT NULL,  `event` varchar(2) DEFAULT NULL,`iNDataContent` varchar(2048) DEFAULT NULL, `outDataContent` varchar(2048) DEFAULT NULL,`calculationLocation` varchar(8)  DEFAULT NULL,  `dataObjectId` varchar(32) DEFAULT NULL,`createtime`datetime)");
-            myDbHelper.execute("CREATE TABLE if not EXISTS `log_fulldata` (  `id` int(11)  NOT NULL auto_increment  PRIMARY KEY,  `dataObjectId` varchar(32) DEFAULT NULL,`fullData` longtext DEFAULT NULL)");
+            myDbHelper.execute("CREATE TABLE if not EXISTS `log_fulldata` (  `id` int(11)  NOT NULL auto_increment  PRIMARY KEY,  `dataObjectId` varchar(32) DEFAULT NULL,`FULL_DATA_SQL` longtext DEFAULT NULL)");
             return true;
         } catch (Exception e) {
             return false;
@@ -285,28 +292,28 @@ public class MainFactoryApplication {
 
 
     /*1每2秒执行一次 日志的批量插入*/
-    @Scheduled(cron = "2 * * * * ?")
+    @Scheduled(cron = "*/2 * * * * *")
     public void batchLog() {
         Map<String, List<Object>> logSuccessMap = LogUtils.logSuccessMapList;
         long current = System.currentTimeMillis();
-        List<String> successList = logSuccessMap.keySet().stream().filter(key -> Long.getLong(key) > (current - 2)).toList();
-        List<Object[]> logSuccessList = new ArrayList<>();
+        List<String> successList = logSuccessMap.keySet().stream().filter(key -> Long.parseLong(key) < current ).toList();
+        List<Object> logSuccessList = new ArrayList<>();
         for (String currentTime : successList) {
-            logSuccessList.add(logSuccessMap.get(currentTime).toArray());
+            logSuccessList.addAll(logSuccessMap.get(currentTime));
             logSuccessMap.remove(currentTime);
         }
         Map<String, List<Object>> logErrorMap = LogUtils.logErrorMapList;
-        List<String> errorList = logErrorMap.keySet().stream().filter(key -> Long.getLong(key) > (current - 2)).toList();
-        List<Object[]> logErrorList = new ArrayList<>();
+        List<String> errorList = logErrorMap.keySet().stream().filter(key -> Long.parseLong(key) < current ).toList();
+        List<Object> logErrorList = new ArrayList<>();
         for (String currentTime : errorList) {
-            logErrorList.add(logErrorMap.get(currentTime).toArray());
+            logErrorList.addAll(logErrorMap.get(currentTime));
             logErrorMap.remove(currentTime);
         }
         Map<String, List<Object>> logSourceMap = LogUtils.sourceMapList;
-        List<String> sourceList = logSourceMap.keySet().stream().filter(key -> Long.getLong(key) > (current - 2)).toList();
-        List<Object[]> logSourceList = new ArrayList<>();
+        List<String> sourceList = logSourceMap.keySet().stream().filter(key -> Long.parseLong(key) < current ).toList();
+        List<Object> logSourceList = new ArrayList<>();
         for (String currentTime : sourceList) {
-            logSourceList.add(logSourceMap.get(currentTime).toArray());
+            logSourceList.addAll(logSourceMap.get(currentTime));
             logSourceMap.remove(currentTime);
         }
 
@@ -317,12 +324,12 @@ public class MainFactoryApplication {
         }
 
 
-        String insertSQL = "INSERT INTO log_success ( success, location, logContent, createtime, serviceId, workId, event, iNDataContent, outDataContent, calculationLocation, dataObjectId) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
-        String errorSQL = "INSERT INTO log_error ( success, location, logContent, createtime, serviceId, workId, event, iNDataContent, outDataContent, calculationLocation, dataObjectId) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
         try {
-            if (logSuccessList.size() > 0) myDbHelper.updateByCondition(insertSQL, logSuccessList);
-            if (logErrorList.size() > 0) myDbHelper.updateByCondition(errorSQL, logErrorList);
-            if (logSourceList.size() > 0) myDbHelper.updateByCondition("INSERT INTO log_fulldata ( dataObjectId, fullData) VALUES (?,?)", logSourceList);
+            if (logSuccessList.size() > 0) myDbHelper.JDBCBatch(INSERT_SQL, logSuccessList,null);
+            if (logErrorList.size() > 0) myDbHelper.JDBCBatch(ERROR_SQL, logErrorList,null);
+            if (logSourceList.size() > 0) {
+                myDbHelper.JDBCBatch(FULL_DATA_SQL, logSourceList,null);
+            }
         } catch (Exception e) {
             System.out.println("日志记录异常" + LogUtils.getException(e));
         }