浏览代码

移除commons-exec,采用原生方式实现;

xuxueli 5 年前
父节点
当前提交
82b4d735ca
共有 4 个文件被更改,包括 139 次插入17 次删除
  1. 2 2
      doc/XXL-JOB官方文档.md
  2. 0 2
      pom.xml
  3. 0 7
      xxl-job-core/pom.xml
  4. 137 6
      xxl-job-core/src/main/java/com/xxl/job/core/util/ScriptUtil.java

+ 2 - 2
doc/XXL-JOB官方文档.md 查看文件

@@ -1672,9 +1672,9 @@ public ReturnT<String> execute(String param) {
1672 1672
     return ReturnT.SUCCESS;
1673 1673
 }
1674 1674
 ```
1675
-- 2、调度中心dispatcher servlet加载顺序优化
1675
+- 2、移除commons-exec,采用原生方式实现
1676 1676
 - 3、执行器回调乱码问题修复;
1677
-- 4、[迭代中]移除commons-exec,采用原生方式实现
1677
+- 4、调度中心dispatcher servlet加载顺序优化
1678 1678
 - 5、[迭代中]任务操作API服务调整为restful方式,降低接入成本;
1679 1679
 
1680 1680
 

+ 0 - 2
pom.xml 查看文件

@@ -34,8 +34,6 @@
34 34
 		<slf4j-api.version>1.7.28</slf4j-api.version>
35 35
 		<junit.version>4.12</junit.version>
36 36
 
37
-		<commons-exec.version>1.3</commons-exec.version>
38
-
39 37
 		<groovy.version>2.5.8</groovy.version>
40 38
 
41 39
 		<maven-source-plugin.version>3.1.0</maven-source-plugin.version>

+ 0 - 7
xxl-job-core/pom.xml 查看文件

@@ -29,13 +29,6 @@
29 29
 			<version>${groovy.version}</version>
30 30
 		</dependency>
31 31
 
32
-		<!-- commons-exec -->
33
-		<dependency>
34
-			<groupId>org.apache.commons</groupId>
35
-			<artifactId>commons-exec</artifactId>
36
-			<version>${commons-exec.version}</version>
37
-		</dependency>
38
-
39 32
 		<!-- spring-context -->
40 33
 		<dependency>
41 34
 			<groupId>org.springframework</groupId>

+ 137 - 6
xxl-job-core/src/main/java/com/xxl/job/core/util/ScriptUtil.java 查看文件

@@ -1,12 +1,13 @@
1 1
 package com.xxl.job.core.util;
2 2
 
3 3
 import com.xxl.job.core.log.XxlJobLogger;
4
-import org.apache.commons.exec.CommandLine;
5
-import org.apache.commons.exec.DefaultExecutor;
6
-import org.apache.commons.exec.PumpStreamHandler;
7 4
 
8 5
 import java.io.FileOutputStream;
9 6
 import java.io.IOException;
7
+import java.io.InputStream;
8
+import java.io.OutputStream;
9
+import java.util.ArrayList;
10
+import java.util.List;
10 11
 
11 12
 /**
12 13
  *  1、内嵌编译器如"PythonInterpreter"无法引用扩展包,因此推荐使用java调用控制台进程方式"Runtime.getRuntime().exec()"来运行脚本(shell或python);
@@ -42,13 +43,143 @@ public class ScriptUtil {
42 43
     }
43 44
 
44 45
     /**
45
-     * 日志文件输出方式
46
+     * 脚本执行,日志文件实时输出
47
+     *
48
+     * @param command
49
+     * @param scriptFile
50
+     * @param logFile
51
+     * @param params
52
+     * @return
53
+     * @throws IOException
54
+     */
55
+    public static int execToFileB(String command, String scriptFile, String logFile, String... params) throws IOException {
56
+
57
+        FileOutputStream fileOutputStream = null;
58
+        Thread inputThread = null;
59
+        Thread errThread = null;
60
+        try {
61
+            // file
62
+            fileOutputStream = new FileOutputStream(logFile, true);
63
+
64
+            // command
65
+            List<String> cmdarray = new ArrayList<>();
66
+            cmdarray.add(command);
67
+            cmdarray.add(scriptFile);
68
+            if (params!=null && params.length>0) {
69
+                for (String param:params) {
70
+                    cmdarray.add(param);
71
+                }
72
+            }
73
+            String[] cmdarrayFinal = cmdarray.toArray(new String[cmdarray.size()]);
74
+
75
+            // process-exec
76
+            final Process process = Runtime.getRuntime().exec(cmdarrayFinal);
77
+
78
+            // log-thread
79
+            final FileOutputStream finalFileOutputStream = fileOutputStream;
80
+            inputThread = new Thread(new Runnable() {
81
+                @Override
82
+                public void run() {
83
+                    try {
84
+                        copy(process.getInputStream(), finalFileOutputStream, new byte[1024]);
85
+                    } catch (IOException e) {
86
+                        XxlJobLogger.log(e);
87
+                    }
88
+                }
89
+            });
90
+            errThread = new Thread(new Runnable() {
91
+                @Override
92
+                public void run() {
93
+                    try {
94
+                        copy(process.getErrorStream(), finalFileOutputStream, new byte[1024]);
95
+                    } catch (IOException e) {
96
+                        XxlJobLogger.log(e);
97
+                    }
98
+                }
99
+            });
100
+            inputThread.start();
101
+            errThread.start();
102
+
103
+            // process-wait
104
+            int exitValue = process.waitFor();      // exit code: 0=success, 1=error
105
+
106
+            // log-thread join
107
+            inputThread.join();
108
+            errThread.join();
109
+
110
+            return exitValue;
111
+        } catch (Exception e) {
112
+            XxlJobLogger.log(e);
113
+            return -1;
114
+        } finally {
115
+            if (fileOutputStream != null) {
116
+                try {
117
+                    fileOutputStream.close();
118
+                } catch (IOException e) {
119
+                    XxlJobLogger.log(e);
120
+                }
121
+
122
+            }
123
+            if (inputThread != null && inputThread.isAlive()) {
124
+                inputThread.interrupt();
125
+            }
126
+            if (errThread != null && errThread.isAlive()) {
127
+                errThread.interrupt();
128
+            }
129
+        }
130
+    }
131
+
132
+    /**
133
+     * 数据流Copy(Input自动关闭,Output不处理)
134
+     *
135
+     * @param inputStream
136
+     * @param outputStream
137
+     * @param buffer
138
+     * @return
139
+     * @throws IOException
140
+     */
141
+    private static long copy(InputStream inputStream, OutputStream outputStream, byte[] buffer) throws IOException {
142
+        try {
143
+            long total = 0;
144
+            for (;;) {
145
+                int res = inputStream.read(buffer);
146
+                if (res == -1) {
147
+                    break;
148
+                }
149
+                if (res > 0) {
150
+                    total += res;
151
+                    if (outputStream != null) {
152
+                        outputStream.write(buffer, 0, res);
153
+                    }
154
+                }
155
+            }
156
+            outputStream.flush();
157
+            //out = null;
158
+            inputStream.close();
159
+            inputStream = null;
160
+            return total;
161
+        } finally {
162
+            if (inputStream != null) {
163
+                inputStream.close();
164
+            }
165
+        }
166
+    }
167
+
168
+    /**
169
+     * 脚本执行,日志文件实时输出
46 170
      *
47 171
      * 优点:支持将目标数据实时输出到指定日志文件中去
48 172
      * 缺点:
49 173
      *      标准输出和错误输出优先级固定,可能和脚本中顺序不一致
50 174
      *      Java无法实时获取
51 175
      *
176
+     *      <!-- commons-exec -->
177
+     * 		<dependency>
178
+     * 			<groupId>org.apache.commons</groupId>
179
+     * 			<artifactId>commons-exec</artifactId>
180
+     * 			<version>${commons-exec.version}</version>
181
+     * 		</dependency>
182
+     *
52 183
      * @param command
53 184
      * @param scriptFile
54 185
      * @param logFile
@@ -56,7 +187,7 @@ public class ScriptUtil {
56 187
      * @return
57 188
      * @throws IOException
58 189
      */
59
-    public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
190
+    /*public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
60 191
         // 标准输出:print (null if watchdog timeout)
61 192
         // 错误输出:logging + 异常 (still exists if watchdog timeout)
62 193
         // 标准输入
@@ -92,6 +223,6 @@ public class ScriptUtil {
92 223
 
93 224
             }
94 225
         }
95
-    }
226
+    }*/
96 227
 
97 228
 }