|
@@ -1,12 +1,13 @@
|
|
|
package com.xxl.job.core.util;
|
|
|
|
|
|
import com.xxl.job.core.log.XxlJobLogger;
|
|
|
-import org.apache.commons.exec.CommandLine;
|
|
|
-import org.apache.commons.exec.DefaultExecutor;
|
|
|
-import org.apache.commons.exec.PumpStreamHandler;
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
|
* 1、内嵌编译器如"PythonInterpreter"无法引用扩展包,因此推荐使用java调用控制台进程方式"Runtime.getRuntime().exec()"来运行脚本(shell或python);
|
|
@@ -42,13 +43,143 @@ public class ScriptUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * 日志文件输出方式
|
|
|
+ * 脚本执行,日志文件实时输出
|
|
|
+ *
|
|
|
+ * @param command
|
|
|
+ * @param scriptFile
|
|
|
+ * @param logFile
|
|
|
+ * @param params
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static int execToFileB(String command, String scriptFile, String logFile, String... params) throws IOException {
|
|
|
+
|
|
|
+ FileOutputStream fileOutputStream = null;
|
|
|
+ Thread inputThread = null;
|
|
|
+ Thread errThread = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ fileOutputStream = new FileOutputStream(logFile, true);
|
|
|
+
|
|
|
+
|
|
|
+ List<String> cmdarray = new ArrayList<>();
|
|
|
+ cmdarray.add(command);
|
|
|
+ cmdarray.add(scriptFile);
|
|
|
+ if (params!=null && params.length>0) {
|
|
|
+ for (String param:params) {
|
|
|
+ cmdarray.add(param);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String[] cmdarrayFinal = cmdarray.toArray(new String[cmdarray.size()]);
|
|
|
+
|
|
|
+
|
|
|
+ final Process process = Runtime.getRuntime().exec(cmdarrayFinal);
|
|
|
+
|
|
|
+
|
|
|
+ final FileOutputStream finalFileOutputStream = fileOutputStream;
|
|
|
+ inputThread = new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ copy(process.getInputStream(), finalFileOutputStream, new byte[1024]);
|
|
|
+ } catch (IOException e) {
|
|
|
+ XxlJobLogger.log(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ errThread = new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ copy(process.getErrorStream(), finalFileOutputStream, new byte[1024]);
|
|
|
+ } catch (IOException e) {
|
|
|
+ XxlJobLogger.log(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ inputThread.start();
|
|
|
+ errThread.start();
|
|
|
+
|
|
|
+
|
|
|
+ int exitValue = process.waitFor();
|
|
|
+
|
|
|
+
|
|
|
+ inputThread.join();
|
|
|
+ errThread.join();
|
|
|
+
|
|
|
+ return exitValue;
|
|
|
+ } catch (Exception e) {
|
|
|
+ XxlJobLogger.log(e);
|
|
|
+ return -1;
|
|
|
+ } finally {
|
|
|
+ if (fileOutputStream != null) {
|
|
|
+ try {
|
|
|
+ fileOutputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ XxlJobLogger.log(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (inputThread != null && inputThread.isAlive()) {
|
|
|
+ inputThread.interrupt();
|
|
|
+ }
|
|
|
+ if (errThread != null && errThread.isAlive()) {
|
|
|
+ errThread.interrupt();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 数据流Copy(Input自动关闭,Output不处理)
|
|
|
+ *
|
|
|
+ * @param inputStream
|
|
|
+ * @param outputStream
|
|
|
+ * @param buffer
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static long copy(InputStream inputStream, OutputStream outputStream, byte[] buffer) throws IOException {
|
|
|
+ try {
|
|
|
+ long total = 0;
|
|
|
+ for (;;) {
|
|
|
+ int res = inputStream.read(buffer);
|
|
|
+ if (res == -1) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (res > 0) {
|
|
|
+ total += res;
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.write(buffer, 0, res);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ outputStream.flush();
|
|
|
+
|
|
|
+ inputStream.close();
|
|
|
+ inputStream = null;
|
|
|
+ return total;
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 脚本执行,日志文件实时输出
|
|
|
*
|
|
|
* 优点:支持将目标数据实时输出到指定日志文件中去
|
|
|
* 缺点:
|
|
|
* 标准输出和错误输出优先级固定,可能和脚本中顺序不一致
|
|
|
* Java无法实时获取
|
|
|
*
|
|
|
+ * <!-- commons-exec -->
|
|
|
+ * <dependency>
|
|
|
+ * <groupId>org.apache.commons</groupId>
|
|
|
+ * <artifactId>commons-exec</artifactId>
|
|
|
+ * <version>${commons-exec.version}</version>
|
|
|
+ * </dependency>
|
|
|
+ *
|
|
|
* @param command
|
|
|
* @param scriptFile
|
|
|
* @param logFile
|
|
@@ -56,7 +187,7 @@ public class ScriptUtil {
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -92,6 +223,6 @@ public class ScriptUtil {
|
|
|
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
}
|