Browse Source

生产端,测试sh

wuweihao 2 năm trước cách đây
mục cha
commit
8ecb8a61ec

+ 2 - 2
720yun_fd_consumer/gis_consumer_oss/src/main/java/com/gis/oss/util/AwsOssUtil.java

@@ -162,9 +162,9 @@ public class AwsOssUtil {
 
     public void uploadBySh(String filePath, String key) {
         String cmd = String.format(CmdConstant.UPLOAD_DIR, bucket, filePath, key , "aws");
-          // CmdUtils.callShell(cmd);
         log.info("cmd aws: {}", cmd);
-        RuntimeUtil.exec(new String[]{"/bin/sh", "-c"}, cmd + "&");
+         CmdUtils.callShell(cmd);
+//        RuntimeUtil.exec(new String[]{"/bin/sh", "-c"}, cmd + "&");
         log.info("cmd &&&&&&&: {}", cmd);
 //        CmdUtils.callLine(cmd);
     }

+ 17 - 36
720yun_fd_manage/gis_common/src/main/java/com/gis/common/util/CmdUtils.java

@@ -126,45 +126,26 @@ public class CmdUtils {
     }
 
 
-    public static int cmdOss(String cmd) throws IOException, InterruptedException {
-        Process exec = RuntimeUtil.exec(cmd);
-        log.info("run cmdOss");
-
-        BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
-        BufferedReader errorBuf = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
-
-        String errorLine;
-        while ((errorLine = errorBuf.readLine()) != null) {
-            log.error("errorLine: {}", errorLine);
-        }
-
-        // success ,没有获取到信息
-        String line;
-        int i = 1;
-        while ((line = br.readLine()) != null) {
-            log.info("line: {}", line);
-
-            // 查看执行日志
-//            if (i % 100 == 0) {
-//                log.info("line, i=" + i +", " +line);
-//            }
-
-//            if (line.contains("done.")) {
-//                log.info("line: {}", line);
-//                log.info("cmdThumb完成 ");
-//            }
-            i++;
+    /**
+     * 调用算法 xx.sh 脚本
+     * @param command
+     */
+    public static void callShell_1(String command){
+        log.info("cmd: {}", command);
+        try {
+            String[] cmd = new String[]{"/bin/sh", "-c", command};
+            Process process = Runtime.getRuntime().exec(cmd);
+            StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
+            errorGobbler.start();
+            StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT");
+            outGobbler.start();
+            process.waitFor();
+        } catch (Exception e) {
+            e.printStackTrace();
         }
-        log.info("执行总行数:" +i);
+    }
 
-        // 结束命令行
-        int isCmd = exec.waitFor();
 
-        // 关闭流
-        br.close();
-        errorBuf.close();
 
-        return isCmd;
 
-    }
 }

+ 79 - 0
720yun_fd_manage/gis_common/src/main/java/com/gis/common/util/StreamGobblerLine.java

@@ -0,0 +1,79 @@
+package com.gis.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.*;
+
+@Slf4j
+public class StreamGobblerLine extends Thread {
+
+	InputStream is;
+    String type;
+    OutputStream os;
+    Integer lineSize;  // 多少行打印日志一次
+
+    public StreamGobblerLine(InputStream is, String type) {
+        this(is, type, null, null);
+    }
+
+    public StreamGobblerLine(InputStream is, String type, Integer lineSize) {
+        this(is, type, null, lineSize);
+    }
+
+    StreamGobblerLine(InputStream is, String type, OutputStream redirect, Integer lineSize) {
+        this.is = is;  
+        this.type = type;  
+        this.os = redirect;
+        this.lineSize = lineSize;
+    }  
+
+    public void run() {
+        log.info("run StreamGobblerLine");
+
+        InputStreamReader isr = null;  
+        BufferedReader br = null;  
+        PrintWriter pw = null;  
+        try {  
+            if (os != null)  
+                pw = new PrintWriter(os);  
+
+            isr = new InputStreamReader(is);  
+            br = new BufferedReader(isr);  
+            String line=null;
+            int i = 1;
+            while ( (line = br.readLine()) != null) {
+                if (lineSize != null) {
+                    if (i % lineSize == 0) {
+                        log.info(type + "," + i +" : >" + line);
+                    }
+                } else {
+                    log.info(type + ","  + i +" : >" + line);
+                }
+                i++;
+
+            }
+
+            if (pw != null)  
+                pw.flush();  
+        } catch (IOException ioe) {  
+            ioe.printStackTrace();    
+        } finally{  
+            try {  
+            	if(pw!=null)
+            	{
+            		 pw.close();  
+            	}
+            	if(br!=null)
+            	{
+            		br.close();  
+            	}
+            	if(isr!=null)
+            	{
+            		isr.close();  
+            	}
+            } catch (IOException e) {  
+                e.printStackTrace();  
+            }  
+        }  
+    }  
+}

+ 2 - 0
720yun_fd_manage/gis_service/src/main/java/com/gis/service/TestService.java

@@ -20,4 +20,6 @@ public interface TestService  {
     Result updateDbFodderTourXml(String isAll);
 
     Result awsUpload(MultipartFile file);
+
+    Result sh();
 }

+ 8 - 0
720yun_fd_manage/gis_service/src/main/java/com/gis/service/impl/TestServiceImpl.java

@@ -9,6 +9,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.constant.ErrorEnum;
 import com.gis.common.exception.BaseRuntimeException;
+import com.gis.common.util.CmdUtils;
 import com.gis.common.util.Result;
 import com.gis.domain.entity.FodderEntity;
 import com.gis.domain.entity.WorkEntity;
@@ -187,6 +188,13 @@ public class TestServiceImpl implements TestService {
         return Result.success();
     }
 
+    @Override
+    public Result sh() {
+        String cmd = "bash /opt/ossutil/fyun-upload.sh test-4dkankan /mnt/720yun_fd_manage_data/fd720_n5ArPVAwm 720yun_fd_manage/fd720_n5ArPVAwm aws folder";
+        CmdUtils.callShell_1(cmd);
+        return Result.success();
+    }
+
 
     /**
      * 2022-10-21

+ 7 - 0
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/TestController.java

@@ -256,6 +256,13 @@ public class TestController extends BaseController {
     }
 
 
+    @ApiOperation(value = "sh-测试脚本")
+    @GetMapping("/sh")
+    public Result sh()  {
+        return testService.sh();
+    }
+
+