提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.timer.jobhandler; |
懒 |
2 |
|
|
3 |
import com.xxl.job.core.biz.model.ReturnT; |
|
4 |
import com.xxl.job.core.handler.IJobHandler; |
|
5 |
import com.xxl.job.core.handler.annotation.JobHandler; |
|
6 |
import com.xxl.job.core.log.XxlJobLogger; |
|
7 |
|
|
8 |
import java.io.BufferedInputStream; |
|
9 |
import java.io.BufferedReader; |
|
10 |
import java.io.InputStreamReader; |
|
11 |
|
|
12 |
/** |
|
13 |
* 命令行任务 |
|
14 |
* |
|
15 |
* @author xuxueli 2018-09-16 03:48:34 |
|
16 |
*/ |
|
17 |
@JobHandler(value = "commandJobHandler") |
|
18 |
public class CommandJobHandler extends IJobHandler { |
|
19 |
|
|
20 |
@Override |
|
21 |
public ReturnT<String> execute(String param) throws Exception { |
|
22 |
String command = param; |
|
23 |
int exitValue = -1; |
|
24 |
|
|
25 |
BufferedReader bufferedReader = null; |
|
26 |
try { |
|
27 |
// command process |
|
28 |
Process process = Runtime.getRuntime().exec(command); |
|
29 |
BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream()); |
|
30 |
bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream)); |
|
31 |
|
|
32 |
// command log |
|
33 |
String line; |
|
34 |
while ((line = bufferedReader.readLine()) != null) { |
|
35 |
XxlJobLogger.log(line); |
|
36 |
} |
|
37 |
|
|
38 |
// command exit |
|
39 |
process.waitFor(); |
|
40 |
exitValue = process.exitValue(); |
|
41 |
} catch (Exception e) { |
|
42 |
XxlJobLogger.log(e); |
|
43 |
} finally { |
|
44 |
if (bufferedReader != null) { |
|
45 |
bufferedReader.close(); |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
if (exitValue == 0) { |
|
50 |
return IJobHandler.SUCCESS; |
|
51 |
} else { |
|
52 |
return new ReturnT<String>(IJobHandler.FAIL.getCode(), "command exit value(" + exitValue + ") is failed"); |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
} |