|
@@ -1,10 +1,21 @@
|
1
|
1
|
package com.xxl.job.executor.service.jobhandler;
|
2
|
2
|
|
3
|
3
|
import com.xxl.job.core.biz.model.ReturnT;
|
|
4
|
+import com.xxl.job.core.handler.IJobHandler;
|
4
|
5
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
5
|
6
|
import com.xxl.job.core.log.XxlJobLogger;
|
|
7
|
+import com.xxl.job.core.util.ShardingUtil;
|
|
8
|
+import org.slf4j.Logger;
|
|
9
|
+import org.slf4j.LoggerFactory;
|
6
|
10
|
import org.springframework.stereotype.Component;
|
7
|
11
|
|
|
12
|
+import java.io.BufferedInputStream;
|
|
13
|
+import java.io.BufferedReader;
|
|
14
|
+import java.io.InputStreamReader;
|
|
15
|
+import java.net.HttpURLConnection;
|
|
16
|
+import java.net.URL;
|
|
17
|
+import java.util.concurrent.TimeUnit;
|
|
18
|
+
|
8
|
19
|
/**
|
9
|
20
|
* XxlJob开发示例(Bean模式)
|
10
|
21
|
*
|
|
@@ -17,28 +28,166 @@ import org.springframework.stereotype.Component;
|
17
|
28
|
*/
|
18
|
29
|
@Component
|
19
|
30
|
public class SampleXxlJob {
|
|
31
|
+ private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
|
20
|
32
|
|
21
|
33
|
|
22
|
|
- @XxlJob("demoJobHandler2")
|
23
|
|
- public ReturnT<String> execute(String param) {
|
|
34
|
+ /**
|
|
35
|
+ * 1、简单任务示例(Bean模式)
|
|
36
|
+ */
|
|
37
|
+ @XxlJob("demoJobHandler")
|
|
38
|
+ public ReturnT<String> demoJobHandler(String param) throws Exception {
|
|
39
|
+ XxlJobLogger.log("XXL-JOB, Hello World.");
|
24
|
40
|
|
25
|
|
- XxlJobLogger.log("hello world.");
|
|
41
|
+ for (int i = 0; i < 5; i++) {
|
|
42
|
+ XxlJobLogger.log("beat at:" + i);
|
|
43
|
+ TimeUnit.SECONDS.sleep(2);
|
|
44
|
+ }
|
26
|
45
|
return ReturnT.SUCCESS;
|
27
|
46
|
}
|
28
|
47
|
|
29
|
|
- @XxlJob(value="demoJobHandler3", init = "init", destroy = "destory")
|
30
|
|
- public ReturnT<String> execute3(String param) {
|
31
|
48
|
|
32
|
|
- XxlJobLogger.log("hello world.");
|
|
49
|
+ /**
|
|
50
|
+ * 2、分片广播任务
|
|
51
|
+ */
|
|
52
|
+ @XxlJob("shardingJobHandler")
|
|
53
|
+ public ReturnT<String> shardingJobHandler(String param) throws Exception {
|
|
54
|
+
|
|
55
|
+ // 分片参数
|
|
56
|
+ ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
|
|
57
|
+ XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
|
|
58
|
+
|
|
59
|
+ // 业务逻辑
|
|
60
|
+ for (int i = 0; i < shardingVO.getTotal(); i++) {
|
|
61
|
+ if (i == shardingVO.getIndex()) {
|
|
62
|
+ XxlJobLogger.log("第 {} 片, 命中分片开始处理", i);
|
|
63
|
+ } else {
|
|
64
|
+ XxlJobLogger.log("第 {} 片, 忽略", i);
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+
|
33
|
68
|
return ReturnT.SUCCESS;
|
34
|
69
|
}
|
35
|
70
|
|
36
|
|
- public void init(){
|
37
|
|
- System.out.println("init");
|
|
71
|
+
|
|
72
|
+ /**
|
|
73
|
+ * 3、命令行任务
|
|
74
|
+ */
|
|
75
|
+ @XxlJob("commandJobHandler")
|
|
76
|
+ public ReturnT<String> commandJobHandler(String param) throws Exception {
|
|
77
|
+ String command = param;
|
|
78
|
+ int exitValue = -1;
|
|
79
|
+
|
|
80
|
+ BufferedReader bufferedReader = null;
|
|
81
|
+ try {
|
|
82
|
+ // command process
|
|
83
|
+ Process process = Runtime.getRuntime().exec(command);
|
|
84
|
+ BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
|
|
85
|
+ bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
|
|
86
|
+
|
|
87
|
+ // command log
|
|
88
|
+ String line;
|
|
89
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
90
|
+ XxlJobLogger.log(line);
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ // command exit
|
|
94
|
+ process.waitFor();
|
|
95
|
+ exitValue = process.exitValue();
|
|
96
|
+ } catch (Exception e) {
|
|
97
|
+ XxlJobLogger.log(e);
|
|
98
|
+ } finally {
|
|
99
|
+ if (bufferedReader != null) {
|
|
100
|
+ bufferedReader.close();
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ if (exitValue == 0) {
|
|
105
|
+ return IJobHandler.SUCCESS;
|
|
106
|
+ } else {
|
|
107
|
+ return new ReturnT<String>(IJobHandler.FAIL.getCode(), "command exit value("+exitValue+") is failed");
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+ /**
|
|
113
|
+ * 4、跨平台Http任务
|
|
114
|
+ */
|
|
115
|
+ @XxlJob("httpJobHandler")
|
|
116
|
+ public ReturnT<String> httpJobHandler(String param) throws Exception {
|
|
117
|
+
|
|
118
|
+ // request
|
|
119
|
+ HttpURLConnection connection = null;
|
|
120
|
+ BufferedReader bufferedReader = null;
|
|
121
|
+ try {
|
|
122
|
+ // connection
|
|
123
|
+ URL realUrl = new URL(param);
|
|
124
|
+ connection = (HttpURLConnection) realUrl.openConnection();
|
|
125
|
+
|
|
126
|
+ // connection setting
|
|
127
|
+ connection.setRequestMethod("GET");
|
|
128
|
+ connection.setDoOutput(true);
|
|
129
|
+ connection.setDoInput(true);
|
|
130
|
+ connection.setUseCaches(false);
|
|
131
|
+ connection.setReadTimeout(5 * 1000);
|
|
132
|
+ connection.setConnectTimeout(3 * 1000);
|
|
133
|
+ connection.setRequestProperty("connection", "Keep-Alive");
|
|
134
|
+ connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
135
|
+ connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
|
|
136
|
+
|
|
137
|
+ // do connection
|
|
138
|
+ connection.connect();
|
|
139
|
+
|
|
140
|
+ //Map<String, List<String>> map = connection.getHeaderFields();
|
|
141
|
+
|
|
142
|
+ // valid StatusCode
|
|
143
|
+ int statusCode = connection.getResponseCode();
|
|
144
|
+ if (statusCode != 200) {
|
|
145
|
+ throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ // result
|
|
149
|
+ bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
150
|
+ StringBuilder result = new StringBuilder();
|
|
151
|
+ String line;
|
|
152
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
153
|
+ result.append(line);
|
|
154
|
+ }
|
|
155
|
+ String responseMsg = result.toString();
|
|
156
|
+
|
|
157
|
+ XxlJobLogger.log(responseMsg);
|
|
158
|
+ return ReturnT.SUCCESS;
|
|
159
|
+ } catch (Exception e) {
|
|
160
|
+ XxlJobLogger.log(e);
|
|
161
|
+ return ReturnT.FAIL;
|
|
162
|
+ } finally {
|
|
163
|
+ try {
|
|
164
|
+ if (bufferedReader != null) {
|
|
165
|
+ bufferedReader.close();
|
|
166
|
+ }
|
|
167
|
+ if (connection != null) {
|
|
168
|
+ connection.disconnect();
|
|
169
|
+ }
|
|
170
|
+ } catch (Exception e2) {
|
|
171
|
+ XxlJobLogger.log(e2);
|
|
172
|
+ }
|
|
173
|
+ }
|
|
174
|
+
|
38
|
175
|
}
|
39
|
176
|
|
40
|
|
- public void destory(){
|
41
|
|
- System.out.println("destory");
|
|
177
|
+ /**
|
|
178
|
+ * 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
|
|
179
|
+ */
|
|
180
|
+ @XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
|
|
181
|
+ public ReturnT<String> demoJobHandler2(String param) throws Exception {
|
|
182
|
+ XxlJobLogger.log("XXL-JOB, Hello World.");
|
|
183
|
+ return ReturnT.SUCCESS;
|
|
184
|
+ }
|
|
185
|
+ public void init(){
|
|
186
|
+ logger.info("init");
|
|
187
|
+ }
|
|
188
|
+ public void destroy(){
|
|
189
|
+ logger.info("destory");
|
42
|
190
|
}
|
43
|
191
|
|
|
192
|
+
|
44
|
193
|
}
|