Explorar o código

加入线程处理

wangliang hai 1 semana
pai
achega
7be9f700ad

+ 110 - 6
src/main/java/com/qmth/cet/plug/service/impl/LogicServiceImpl.java

@@ -16,6 +16,8 @@ import java.io.*;
 import java.nio.charset.StandardCharsets;
 import java.util.*;
 import java.util.concurrent.ExecutorService;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 /**
  * @Description: logic service impl
@@ -63,6 +65,7 @@ public class LogicServiceImpl implements LogicService {
                     log.info("root:{}", root);
 
                     String lockKey = rootPath;
+                    log.info("lockKey:{}", lockKey);
                     String finalRootPath = rootPath;
                     executor.submit(() -> {
                         log.info("启动新线程===>id:{},name:{}", Thread.currentThread().getId(), Thread.currentThread().getName());
@@ -99,7 +102,7 @@ public class LogicServiceImpl implements LogicService {
      * @param file
      * @throws IOException
      */
-    protected void scanTxtExecLogic(String projectPath, String rootPath, File file) throws IOException {
+    protected void scanTxtExecLogic(String projectPath, String rootPath, File file) throws IOException, InterruptedException {
         //扫描txt处理
         File[] fileArray = file.listFiles();
         List<File> fileList = new ArrayList<>(Arrays.asList(fileArray));
@@ -163,6 +166,7 @@ public class LogicServiceImpl implements LogicService {
                 } else {
                     IOUtils.write(scanSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(tempScanTxt));
                 }
+                this.execCmd(tempScanTxt, tempScanResultTxt, projectPath, rootPath);
             }
             if (max == size) {
                 break;
@@ -175,18 +179,91 @@ public class LogicServiceImpl implements LogicService {
         }
     }
 
+    /**
+     * 执行cmd
+     *
+     * @param tempScanTxt
+     * @param tempScanResultTxt
+     * @param projectPath
+     * @param rootPath
+     * @throws InterruptedException
+     */
+    protected void execCmd(File tempScanTxt, File tempScanResultTxt, String projectPath, String rootPath) throws InterruptedException, IOException {
+        int randIntRange = new Random().nextInt(10);
+        Thread.sleep(1000 * randIntRange);
+        ByteArrayOutputStream ou = new ByteArrayOutputStream();
+        IOUtils.copy(new FileInputStream(tempScanTxt), ou);
+        String string = new String(ou.toByteArray(), StandardCharsets.UTF_8);
+        String[] strs = StringUtils.split(string, "\r\n");
+        List<String> scanList = new ArrayList<>(Arrays.asList(strs));
+        StringJoiner scanSj = new StringJoiner("\r\n");//全量扫描数据
+        for (int i = 0; i < scanList.size(); i++) {
+            String s = scanList.get(i);
+            //偶数
+            if (i % 2 == 0) {
+                s += ",0";
+                scanSj.add(s);
+            } else {
+                s += ",1";
+                scanSj.add(s);
+            }
+            scanList.set(i, s);
+        }
+        IOUtils.write(scanSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(tempScanResultTxt));
+
+        //读取result数据
+        StringJoiner scanSuccessSj = new StringJoiner("\r\n");//全量扫描数据
+        StringJoiner scanErrorSj = new StringJoiner("\r\n");//全量扫描数据
+        for (String s : scanList) {
+            if (s.endsWith("0")) {
+                scanSuccessSj.add(s);
+            } else if (s.endsWith("1")) {
+                scanErrorSj.add(s);
+            }
+        }
+        this.successTxtExecLogic(projectPath, rootPath, scanSuccessSj);
+        this.errorTxtExecLogic(projectPath, rootPath, scanErrorSj);
+    }
+
     /**
      * 执行成功文件逻辑
      *
      * @param projectPath
      * @param rootPath
-     * @param file
      * @throws IOException
      */
-    protected void successTxtExecLogic(String projectPath, String rootPath, File file) throws IOException {
+    protected void successTxtExecLogic(String projectPath, String rootPath, StringJoiner sj) throws IOException {
         File successTxtFile = new File(projectPath, rootPath + File.separator + "success.txt");
         if (!successTxtFile.exists()) {
             successTxtFile.createNewFile();
+            IOUtils.write(sj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(successTxtFile));
+        } else {
+            //读取文件数据
+            ByteArrayOutputStream ou = new ByteArrayOutputStream();
+            IOUtils.copy(new FileInputStream(successTxtFile), ou);
+            String string = new String(ou.toByteArray(), StandardCharsets.UTF_8);
+            StringJoiner scanSj = new StringJoiner("\r\n");//全量扫描数据
+            scanSj.add(string);
+            String temp = new String(string);
+            String[] strs = StringUtils.split(temp, "\r\n");
+            List<String> scanList = new ArrayList<>(Arrays.asList(strs));
+            Map<String, String> scanMap = scanList.stream().collect(Collectors.toMap(m -> m, Function.identity()));
+
+            //读取传来的临时数据
+            String[] strsTemp = StringUtils.split(sj.toString(), "\r\n");
+            List<String> scanTempList = new ArrayList<>(Arrays.asList(strsTemp));
+
+            boolean write = false;
+            for (int y = 0; y < scanTempList.size(); y++) {
+                String path = scanTempList.get(y);
+                if (!scanMap.containsKey(path)) {
+                    scanSj.add(path);
+                    write = true;
+                }
+            }
+            if (write) {
+                IOUtils.write(scanSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(successTxtFile));
+            }
         }
     }
 
@@ -195,13 +272,40 @@ public class LogicServiceImpl implements LogicService {
      *
      * @param projectPath
      * @param rootPath
-     * @param file
      * @throws IOException
      */
-    protected void errorTxtExecLogic(String projectPath, String rootPath, File file) throws IOException {
+    protected void errorTxtExecLogic(String projectPath, String rootPath, StringJoiner sj) throws IOException {
         File errorTxtFile = new File(projectPath, rootPath + File.separator + "error.txt");
         if (!errorTxtFile.exists()) {
             errorTxtFile.createNewFile();
+            IOUtils.write(sj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(errorTxtFile));
+        } else {
+            //读取文件数据
+            ByteArrayOutputStream ou = new ByteArrayOutputStream();
+            IOUtils.copy(new FileInputStream(errorTxtFile), ou);
+            String string = new String(ou.toByteArray(), StandardCharsets.UTF_8);
+            StringJoiner scanSj = new StringJoiner("\r\n");//全量扫描数据
+            scanSj.add(string);
+            String temp = new String(string);
+            String[] strs = StringUtils.split(temp, "\r\n");
+            List<String> scanList = new ArrayList<>(Arrays.asList(strs));
+            Map<String, String> scanMap = scanList.stream().collect(Collectors.toMap(m -> m, Function.identity()));
+
+            //读取传来的临时数据
+            String[] strsTemp = StringUtils.split(sj.toString(), "\r\n");
+            List<String> scanTempList = new ArrayList<>(Arrays.asList(strsTemp));
+
+            boolean write = false;
+            for (int y = 0; y < scanTempList.size(); y++) {
+                String path = scanTempList.get(y);
+                if (!scanMap.containsKey(path)) {
+                    scanSj.add(path);
+                    write = true;
+                }
+            }
+            if (write) {
+                IOUtils.write(scanSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(errorTxtFile));
+            }
         }
     }
-}
+}