Browse Source

处理执行shell脚本 输出内容过多 缓冲区阻塞问题。

wukai 2 years ago
parent
commit
867cb3e17a

+ 27 - 14
sync-common/src/main/java/com/jjt/common/utils/LinuxCommand.java

@@ -128,23 +128,36 @@ public class LinuxCommand {
         InputStream errorStream = process.getErrorStream();
         // 获取子进程的输出流
         OutputStream outputStream = process.getOutputStream();
-        try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)), true);
-             BufferedReader readInput = new BufferedReader(new InputStreamReader(inputStream));
-             BufferedReader readError = new BufferedReader(new InputStreamReader(errorStream))) {
-            pw.println("exit");
 
-            String line;
-            log.info("子进程输入流:");
-            while ((line = readInput.readLine()) != null) {
-                log.info(line);
-            }
-            log.info("子进程错误流:");
-            while ((line = readError.readLine()) != null) {
-                log.info(line);
-            }
-        } catch (IOException e) {
+        try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)), true);) {
+            pw.println("exit");
+        } catch (Exception e) {
             log.error("出错啦:{}", e.getMessage());
         }
+        //新起线程读取输入流
+        new Thread(() -> {
+            try (BufferedReader readInput = new BufferedReader(new InputStreamReader(inputStream))) {
+                String line;
+                log.info("子进程输入流:");
+                while ((line = readInput.readLine()) != null) {
+                    log.info(line);
+                }
+            } catch (IOException e) {
+                log.error("出错啦:{}", e.getMessage());
+            }
+        }).start();
+        //新开线程读取错误流
+        new Thread(() -> {
+            try (BufferedReader readError = new BufferedReader(new InputStreamReader(errorStream))) {
+                String line;
+                log.info("子进程错误流:");
+                while ((line = readError.readLine()) != null) {
+                    log.info(line);
+                }
+            } catch (IOException e) {
+                log.error("出错啦:{}", e.getMessage());
+            }
+        }).start();
     }
 
 

+ 4 - 0
sync-out/src/main/java/com/jjt/out/service/impl/OutMongoServiceImpl.java

@@ -14,6 +14,7 @@ import com.jjt.out.service.IOutProcessInfoService;
 import com.jjt.system.service.ISysConfigService;
 import net.lingala.zip4j.ZipFile;
 import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.io.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -119,6 +120,9 @@ public class OutMongoServiceImpl extends OutBaseService implements IOutMongoServ
             CompressZip.splitZip(targetDir, zipFile, splitSize);
             //打包文件--end
 
+            //删除临时文件
+            FileUtils.deleteDirectory(targetDir);
+
             //生成描述json文件--start
             try {
                 String descName = syncDir + "sync-78-" + nowTime + ".json";

+ 58 - 14
sync-out/src/test/java/Test.java

@@ -1,21 +1,65 @@
-import com.jjt.common.enums.SyncType;
-
-import java.io.IOException;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
 public class Test {
-    public static void main(String[] args) {
-        System.err.println(SyncType.mysql.toString());
+    public static void main(String[] args) throws Exception {
+        List<String> commands = new ArrayList<>();
+        commands.add("/usr/bin/sh");
+        commands.add("mongo-full-bak.sh");
+        commands.add("localhost");
+        commands.add("27017");
+        commands.add("/mnt/tt");
+
+        exec(commands, "/mnt/");
+    }
 
-        String s = "/usr/bin/mongodump --host localhost --port 27017  -d local -c oplog.rs -q '{\"ts\":{\"$gt\":{\"$timestamp\":{\"t\":1687157753,\"i\":1}}},\"op\":{\"$ne\":\"d\"},\"op\":{\"$ne\":\"n\"}}' -o /mnt/tt";
-        System.err.println(s);
+    /**
+     * 执行shell脚本
+     *
+     * @param commands 命令分段,必须要分割,不能直接拼好一条add进来会报错,commands中的String不需要加空格
+     * @param dir      脚本路径
+     * @throws Exception 异常说明
+     */
+    public static void exec(List<String> commands, String dir) throws Exception {
+        System.err.println("要执行命令了哟:{}" + Arrays.toString(commands.toArray()));
+        ProcessBuilder processBuilder = new ProcessBuilder(commands);
+        processBuilder.directory(new File(dir));
+        Process process = processBuilder.start();
+
+        print(process);
+
+        int code = process.waitFor();
+        System.err.println("命令执行状态:{}" + code);
+        if (code != 0) {
+            throw new Exception("执行命令出错啦!");
+        }
+    }
 
+    private static void print(Process process) {
+        // 获取子进程的输入流
+        InputStream inputStream = process.getInputStream();
+        // 获取子进程的错误流
+        InputStream errorStream = process.getErrorStream();
+        // 获取子进程的输出流
+        OutputStream outputStream = process.getOutputStream();
+        try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)), true);
+             BufferedReader readInput = new BufferedReader(new InputStreamReader(inputStream));
+             BufferedReader readError = new BufferedReader(new InputStreamReader(errorStream))) {
+            pw.println("exit");
 
-//        try {
-//            String cmd = "/usr/bin/mongodump --host 127.0.0.1 --port 27017 --oplog --out /mnt/bak";
-//            Process process = Runtime.getRuntime().exec(cmd);
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//            throw new RuntimeException(e);
-//        }
+            String line;
+            System.err.println("子进程输入流:");
+            while ((line = readInput.readLine()) != null) {
+                System.err.println(line);
+            }
+            System.err.println("子进程错误流:");
+            while ((line = readError.readLine()) != null) {
+                System.err.println(line);
+            }
+        } catch (IOException e) {
+            System.err.println("出错啦:{}" + e.getMessage());
+        }
     }
 }

+ 45 - 14
sync-out/src/test/java/com/test/Test.java

@@ -1,22 +1,53 @@
 package com.test;
 
+import com.alibaba.fastjson.JSONObject;
+import com.jjt.common.enums.SyncType;
+import net.lingala.zip4j.ZipFile;
+import org.apache.commons.io.FileUtils;
+
 import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.Date;
 
 public class Test {
-    public static void main(String[] args) {
-
-
-
-        long t = 1687157753 * 1000;
-        Date d = new Date();
-
-        System.err.println(d.getTime());
-
-        String x = "D:\\data\\sync\\in\\bak\\test.sh";
-        File xf = new File(x);
-        System.err.println(xf.getAbsolutePath());
-        System.err.println(xf.getPath());
-        xf.mkdirs();
+    public static void main(String[] args) throws Exception {
+//        Files.delete(Paths.get("D:\\SYSTEM\\Desktop\\temp\\zip"));
+        File file = new File("D:\\SYSTEM\\Desktop\\temp\\624");
+        FileUtils.deleteDirectory(file);
+//        System.err.println(file.getName());
+//
+//        JSONObject object = new JSONObject();
+////        object.put("")
+//        System.err.println(SyncType.mysql.toString());
+//        JSONObject opd = new JSONObject();
+//        opd.put("$ne", "d");
+//        JSONObject opn = new JSONObject();
+//        opn.put("$ne", "n");
+//
+//
+//        JSONObject tsti = new JSONObject();
+//        tsti.put("t", "1687157753");
+//        tsti.put("i", "1");
+//
+//        JSONObject ts = new JSONObject();
+//        ts.put("$timestamp", tsti);
+//
+//        JSONObject gt = new JSONObject();
+//        gt.put("$gt", ts);
+//
+//        object.put("op", opd);
+//        object.put("op", opn);
+//        object.put("ts", gt);
+//        long t = 1687157753 * 1000;
+//        Date d = new Date();
+//
+//        System.err.println(d.getTime());
+//
+//        String x = "D:\\data\\sync\\in\\bak\\test.sh";
+//        File xf = new File(x);
+//        System.err.println(xf.getAbsolutePath());
+//        System.err.println(xf.getPath());
+//        xf.mkdirs();
     }
 }