xuxueli преди 5 години
родител
ревизия
9fed7b9c60

+ 0 - 58
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/CommandJobHandler.java Целия файл

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

+ 0 - 40
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java Целия файл

@@ -1,40 +0,0 @@
1
-package com.xxl.job.executor.service.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
-import org.springframework.stereotype.Component;
8
-
9
-import java.util.concurrent.TimeUnit;
10
-
11
-
12
-/**
13
- * 【类方式任务开发即将废弃,建议采用方法方式开发,参考:com.xxl.job.executor.service.jobhandler.SampleXxlJob 】
14
- *
15
- * 任务Handler示例(Bean模式)
16
- *
17
- * 开发步骤:
18
- * 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”;
19
- * 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例;
20
- * 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。
21
- * 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
22
- *
23
- * @author xuxueli 2015-12-19 19:43:36
24
- */
25
-@JobHandler(value="demoJobHandler")
26
-@Component
27
-public class DemoJobHandler extends IJobHandler {
28
-
29
-	@Override
30
-	public ReturnT<String> execute(String param) throws Exception {
31
-		XxlJobLogger.log("XXL-JOB, Hello World.");
32
-
33
-		for (int i = 0; i < 5; i++) {
34
-			XxlJobLogger.log("beat at:" + i);
35
-			TimeUnit.SECONDS.sleep(2);
36
-		}
37
-		return SUCCESS;
38
-	}
39
-
40
-}

+ 0 - 85
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java Целия файл

@@ -1,85 +0,0 @@
1
-package com.xxl.job.executor.service.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
-import org.springframework.stereotype.Component;
8
-
9
-import java.io.BufferedReader;
10
-import java.io.InputStreamReader;
11
-import java.net.HttpURLConnection;
12
-import java.net.URL;
13
-
14
-/**
15
- * 跨平台Http任务
16
- *
17
- * @author xuxueli 2018-09-16 03:48:34
18
- */
19
-@JobHandler(value = "httpJobHandler")
20
-@Component
21
-public class HttpJobHandler extends IJobHandler {
22
-
23
-    @Override
24
-    public ReturnT<String> execute(String param) throws Exception {
25
-
26
-        // request
27
-        HttpURLConnection connection = null;
28
-        BufferedReader bufferedReader = null;
29
-        try {
30
-            // connection
31
-            URL realUrl = new URL(param);
32
-            connection = (HttpURLConnection) realUrl.openConnection();
33
-
34
-            // connection setting
35
-            connection.setRequestMethod("GET");
36
-            connection.setDoOutput(true);
37
-            connection.setDoInput(true);
38
-            connection.setUseCaches(false);
39
-            connection.setReadTimeout(5 * 1000);
40
-            connection.setConnectTimeout(3 * 1000);
41
-            connection.setRequestProperty("connection", "Keep-Alive");
42
-            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
43
-            connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
44
-
45
-            // do connection
46
-            connection.connect();
47
-
48
-            //Map<String, List<String>> map = connection.getHeaderFields();
49
-
50
-            // valid StatusCode
51
-            int statusCode = connection.getResponseCode();
52
-            if (statusCode != 200) {
53
-                throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
54
-            }
55
-
56
-            // result
57
-            bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
58
-            StringBuilder result = new StringBuilder();
59
-            String line;
60
-            while ((line = bufferedReader.readLine()) != null) {
61
-                result.append(line);
62
-            }
63
-            String responseMsg = result.toString();
64
-
65
-            XxlJobLogger.log(responseMsg);
66
-            return SUCCESS;
67
-        } catch (Exception e) {
68
-            XxlJobLogger.log(e);
69
-            return FAIL;
70
-        } finally {
71
-            try {
72
-                if (bufferedReader != null) {
73
-                    bufferedReader.close();
74
-                }
75
-                if (connection != null) {
76
-                    connection.disconnect();
77
-                }
78
-            } catch (Exception e2) {
79
-                XxlJobLogger.log(e2);
80
-            }
81
-        }
82
-
83
-    }
84
-
85
-}

+ 159 - 10
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java Целия файл

@@ -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
 }

+ 0 - 38
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java Целия файл

@@ -1,38 +0,0 @@
1
-package com.xxl.job.executor.service.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
-import com.xxl.job.core.util.ShardingUtil;
8
-import org.springframework.stereotype.Component;
9
-
10
-/**
11
- * 分片广播任务
12
- *
13
- * @author xuxueli 2017-07-25 20:56:50
14
- */
15
-@JobHandler(value="shardingJobHandler")
16
-@Component
17
-public class ShardingJobHandler extends IJobHandler {
18
-
19
-	@Override
20
-	public ReturnT<String> execute(String param) throws Exception {
21
-
22
-		// 分片参数
23
-		ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
24
-		XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
25
-
26
-		// 业务逻辑
27
-		for (int i = 0; i < shardingVO.getTotal(); i++) {
28
-			if (i == shardingVO.getIndex()) {
29
-				XxlJobLogger.log("第 {} 片, 命中分片开始处理", i);
30
-			} else {
31
-				XxlJobLogger.log("第 {} 片, 忽略", i);
32
-			}
33
-		}
34
-
35
-		return SUCCESS;
36
-	}
37
-
38
-}