瀏覽代碼

跨平台:除了提供Java、Python、PHP等十来种任务模式之外,新增提供基于HTTP的任务模式;

xuxueli 6 年之前
父節點
當前提交
a1755156e2

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

955
 
955
 
956
 需要注意的是,任务超时中断时与任务终止机制(可查看“4.8 终止运行中的任务”)类似,也是通过 "interrupt" 中断任务,因此业务代码需要将 "InterruptedException" 外抛,否则功能不可用。
956
 需要注意的是,任务超时中断时与任务终止机制(可查看“4.8 终止运行中的任务”)类似,也是通过 "interrupt" 中断任务,因此业务代码需要将 "InterruptedException" 外抛,否则功能不可用。
957
 
957
 
958
+### 5.17 跨平台 & 跨语言
959
+跨平台、跨语言主要体现在以下两个方面:
960
+- 1、提供Java、Python、PHP……等十来种任务模式,可参考章节 “5.5 任务 "运行模式" ”;理论上可扩展任意语言任务模式;
961
+- 2、提供基于HTTP的任务Handler(Bean任务,JobHandler="httpJobHandler");业务方只需要提供HTTP链接即可,不限制语言、平台;
962
+
958
 
963
 
959
 ## 六、版本更新日志
964
 ## 六、版本更新日志
960
 ### 6.1 版本 V1.1.x,新特性[2015-12-05]
965
 ### 6.1 版本 V1.1.x,新特性[2015-12-05]
1244
 - 28、新增任务运行模式 "GLUE模式(PowerShell) ",支持PowerShell脚本任务;
1249
 - 28、新增任务运行模式 "GLUE模式(PowerShell) ",支持PowerShell脚本任务;
1245
 - 29、GLUE脚本文件自动清理功能,及时清理过期脚本文件;
1250
 - 29、GLUE脚本文件自动清理功能,及时清理过期脚本文件;
1246
 - 30、执行器注册方式切换优化,切换自动注册时主动同步在线机器,避免执行器为空的问题;
1251
 - 30、执行器注册方式切换优化,切换自动注册时主动同步在线机器,避免执行器为空的问题;
1247
-- 31、【迭代中】分片任务失败重试优化,仅重试当前失败的分片;
1252
+- 31、跨平台:除了提供Java、Python、PHP等十来种任务模式之外,新增提供基于HTTP的任务模式;
1253
+- 32、【迭代中】分片任务失败重试优化,仅重试当前失败的分片;
1248
 
1254
 
1249
 
1255
 
1250
 ### TODO LIST
1256
 ### TODO LIST

+ 2 - 0
xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/config/JFinalCoreConfig.java 查看文件

5
 import com.jfinal.kit.PropKit;
5
 import com.jfinal.kit.PropKit;
6
 import com.xuxueli.executor.sample.jfinal.controller.IndexController;
6
 import com.xuxueli.executor.sample.jfinal.controller.IndexController;
7
 import com.xuxueli.executor.sample.jfinal.jobhandler.DemoJobHandler;
7
 import com.xuxueli.executor.sample.jfinal.jobhandler.DemoJobHandler;
8
+import com.xuxueli.executor.sample.jfinal.jobhandler.HttpJobHandler;
8
 import com.xuxueli.executor.sample.jfinal.jobhandler.ShardingJobHandler;
9
 import com.xuxueli.executor.sample.jfinal.jobhandler.ShardingJobHandler;
9
 import com.xxl.job.core.executor.XxlJobExecutor;
10
 import com.xxl.job.core.executor.XxlJobExecutor;
10
 import org.slf4j.Logger;
11
 import org.slf4j.Logger;
23
 		// registry jobhandler
24
 		// registry jobhandler
24
 		XxlJobExecutor.registJobHandler("demoJobHandler", new DemoJobHandler());
25
 		XxlJobExecutor.registJobHandler("demoJobHandler", new DemoJobHandler());
25
 		XxlJobExecutor.registJobHandler("shardingJobHandler", new ShardingJobHandler());
26
 		XxlJobExecutor.registJobHandler("shardingJobHandler", new ShardingJobHandler());
27
+		XxlJobExecutor.registJobHandler("httpJobHandler", new HttpJobHandler());
26
 
28
 
27
 		// load executor prop
29
 		// load executor prop
28
 		Prop xxlJobProp = PropKit.use("xxl-job-executor.properties");
30
 		Prop xxlJobProp = PropKit.use("xxl-job-executor.properties");

+ 77 - 0
xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/HttpJobHandler.java 查看文件

1
+package com.xuxueli.executor.sample.jfinal.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.log.XxlJobLogger;
6
+import com.xxl.job.core.util.ShardingUtil;
7
+import org.apache.http.HttpEntity;
8
+import org.apache.http.HttpResponse;
9
+import org.apache.http.client.config.RequestConfig;
10
+import org.apache.http.client.methods.HttpGet;
11
+import org.apache.http.impl.client.CloseableHttpClient;
12
+import org.apache.http.impl.client.HttpClients;
13
+import org.apache.http.util.EntityUtils;
14
+import org.slf4j.Logger;
15
+import org.slf4j.LoggerFactory;
16
+
17
+import java.io.IOException;
18
+
19
+/**
20
+ * 跨平台Http任务
21
+ *
22
+ * @author xuxueli 2018-09-16 03:48:34
23
+ */
24
+public class HttpJobHandler extends IJobHandler {
25
+
26
+	@Override
27
+	public ReturnT<String> execute(String param) throws Exception {
28
+
29
+		// valid
30
+		if (param==null || param.trim().length()==0) {
31
+			XxlJobLogger.log("URL Empty");
32
+			return FAIL;
33
+		}
34
+
35
+		// httpGet config
36
+		HttpGet httpGet = new HttpGet(param);
37
+		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
38
+		httpGet.setConfig(requestConfig);
39
+
40
+		CloseableHttpClient httpClient = null;
41
+		try{
42
+			httpClient = HttpClients.custom().disableAutomaticRetries().build();
43
+
44
+			// parse response
45
+			HttpResponse response = httpClient.execute(httpGet);
46
+			HttpEntity entity = response.getEntity();
47
+			if (response.getStatusLine().getStatusCode() != 200) {
48
+				XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatusLine().getStatusCode());
49
+				return FAIL;
50
+			}
51
+			if (null == entity) {
52
+				XxlJobLogger.log("Http Entity Empty.");
53
+				return FAIL;
54
+			}
55
+
56
+			String responseMsg = EntityUtils.toString(entity, "UTF-8");
57
+			XxlJobLogger.log(responseMsg);
58
+			EntityUtils.consume(entity);
59
+			return SUCCESS;
60
+		} catch (Exception e) {
61
+			XxlJobLogger.log(e);
62
+			return FAIL;
63
+		} finally{
64
+			if (httpGet!=null) {
65
+				httpGet.releaseConnection();
66
+			}
67
+			if (httpClient!=null) {
68
+				try {
69
+					httpClient.close();
70
+				} catch (IOException e) {
71
+					XxlJobLogger.log(e);
72
+				}
73
+			}
74
+		}
75
+	}
76
+
77
+}

+ 0 - 1
xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java 查看文件

5
 import com.xxl.job.core.log.XxlJobLogger;
5
 import com.xxl.job.core.log.XxlJobLogger;
6
 import com.xxl.job.core.util.ShardingUtil;
6
 import com.xxl.job.core.util.ShardingUtil;
7
 
7
 
8
-
9
 /**
8
 /**
10
  * 分片广播任务
9
  * 分片广播任务
11
  *
10
  *

+ 78 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/HttpJobHandler.java 查看文件

1
+package com.xuxueli.executor.sample.nutz.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.apache.http.HttpEntity;
8
+import org.apache.http.HttpResponse;
9
+import org.apache.http.client.config.RequestConfig;
10
+import org.apache.http.client.methods.HttpGet;
11
+import org.apache.http.impl.client.CloseableHttpClient;
12
+import org.apache.http.impl.client.HttpClients;
13
+import org.apache.http.util.EntityUtils;
14
+import org.nutz.ioc.loader.annotation.IocBean;
15
+
16
+import java.io.IOException;
17
+
18
+/**
19
+ * 跨平台Http任务
20
+ *
21
+ * @author xuxueli 2018-09-16 03:48:34
22
+ */
23
+@JobHandler(value="httpJobHandler")
24
+@IocBean
25
+public class HttpJobHandler extends IJobHandler {
26
+
27
+	@Override
28
+	public ReturnT<String> execute(String param) throws Exception {
29
+
30
+		// valid
31
+		if (param==null || param.trim().length()==0) {
32
+			XxlJobLogger.log("URL Empty");
33
+			return FAIL;
34
+		}
35
+
36
+		// httpGet config
37
+		HttpGet httpGet = new HttpGet(param);
38
+		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
39
+		httpGet.setConfig(requestConfig);
40
+
41
+		CloseableHttpClient httpClient = null;
42
+		try{
43
+			httpClient = HttpClients.custom().disableAutomaticRetries().build();
44
+
45
+			// parse response
46
+			HttpResponse response = httpClient.execute(httpGet);
47
+			HttpEntity entity = response.getEntity();
48
+			if (response.getStatusLine().getStatusCode() != 200) {
49
+				XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatusLine().getStatusCode());
50
+				return FAIL;
51
+			}
52
+			if (null == entity) {
53
+				XxlJobLogger.log("Http Entity Empty.");
54
+				return FAIL;
55
+			}
56
+
57
+			String responseMsg = EntityUtils.toString(entity, "UTF-8");
58
+			XxlJobLogger.log(responseMsg);
59
+			EntityUtils.consume(entity);
60
+			return SUCCESS;
61
+		} catch (Exception e) {
62
+			XxlJobLogger.log(e);
63
+			return FAIL;
64
+		} finally{
65
+			if (httpGet!=null) {
66
+				httpGet.releaseConnection();
67
+			}
68
+			if (httpClient!=null) {
69
+				try {
70
+					httpClient.close();
71
+				} catch (IOException e) {
72
+					XxlJobLogger.log(e);
73
+				}
74
+			}
75
+		}
76
+	}
77
+
78
+}

+ 78 - 0
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java 查看文件

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.apache.http.HttpEntity;
8
+import org.apache.http.HttpResponse;
9
+import org.apache.http.client.config.RequestConfig;
10
+import org.apache.http.client.methods.HttpGet;
11
+import org.apache.http.impl.client.CloseableHttpClient;
12
+import org.apache.http.impl.client.HttpClients;
13
+import org.apache.http.util.EntityUtils;
14
+import org.springframework.stereotype.Component;
15
+
16
+import java.io.IOException;
17
+
18
+/**
19
+ * 跨平台Http任务
20
+ *
21
+ * @author xuxueli 2018-09-16 03:48:34
22
+ */
23
+@JobHandler(value="httpJobHandler")
24
+@Component
25
+public class HttpJobHandler extends IJobHandler {
26
+
27
+	@Override
28
+	public ReturnT<String> execute(String param) throws Exception {
29
+
30
+		// valid
31
+		if (param==null || param.trim().length()==0) {
32
+			XxlJobLogger.log("URL Empty");
33
+			return FAIL;
34
+		}
35
+
36
+		// httpGet config
37
+		HttpGet httpGet = new HttpGet(param);
38
+		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
39
+		httpGet.setConfig(requestConfig);
40
+
41
+		CloseableHttpClient httpClient = null;
42
+		try{
43
+			httpClient = HttpClients.custom().disableAutomaticRetries().build();
44
+
45
+			// parse response
46
+			HttpResponse response = httpClient.execute(httpGet);
47
+			HttpEntity entity = response.getEntity();
48
+			if (response.getStatusLine().getStatusCode() != 200) {
49
+				XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatusLine().getStatusCode());
50
+				return FAIL;
51
+			}
52
+			if (null == entity) {
53
+				XxlJobLogger.log("Http Entity Empty.");
54
+				return FAIL;
55
+			}
56
+
57
+			String responseMsg = EntityUtils.toString(entity, "UTF-8");
58
+			XxlJobLogger.log(responseMsg);
59
+			EntityUtils.consume(entity);
60
+			return SUCCESS;
61
+		} catch (Exception e) {
62
+			XxlJobLogger.log(e);
63
+			return FAIL;
64
+		} finally{
65
+			if (httpGet!=null) {
66
+				httpGet.releaseConnection();
67
+			}
68
+			if (httpClient!=null) {
69
+				try {
70
+					httpClient.close();
71
+				} catch (IOException e) {
72
+					XxlJobLogger.log(e);
73
+				}
74
+			}
75
+		}
76
+	}
77
+
78
+}

+ 0 - 1
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java 查看文件

7
 import com.xxl.job.core.util.ShardingUtil;
7
 import com.xxl.job.core.util.ShardingUtil;
8
 import org.springframework.stereotype.Service;
8
 import org.springframework.stereotype.Service;
9
 
9
 
10
-
11
 /**
10
 /**
12
  * 分片广播任务
11
  * 分片广播任务
13
  *
12
  *

+ 78 - 0
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java 查看文件

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.apache.http.HttpEntity;
8
+import org.apache.http.HttpResponse;
9
+import org.apache.http.client.config.RequestConfig;
10
+import org.apache.http.client.methods.HttpGet;
11
+import org.apache.http.impl.client.CloseableHttpClient;
12
+import org.apache.http.impl.client.HttpClients;
13
+import org.apache.http.util.EntityUtils;
14
+import org.springframework.stereotype.Component;
15
+
16
+import java.io.IOException;
17
+
18
+/**
19
+ * 跨平台Http任务
20
+ *
21
+ * @author xuxueli 2018-09-16 03:48:34
22
+ */
23
+@JobHandler(value="httpJobHandler")
24
+@Component
25
+public class HttpJobHandler extends IJobHandler {
26
+
27
+	@Override
28
+	public ReturnT<String> execute(String param) throws Exception {
29
+
30
+		// valid
31
+		if (param==null || param.trim().length()==0) {
32
+			XxlJobLogger.log("URL Empty");
33
+			return FAIL;
34
+		}
35
+
36
+		// httpGet config
37
+		HttpGet httpGet = new HttpGet(param);
38
+		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
39
+		httpGet.setConfig(requestConfig);
40
+
41
+		CloseableHttpClient httpClient = null;
42
+		try{
43
+			httpClient = HttpClients.custom().disableAutomaticRetries().build();
44
+
45
+			// parse response
46
+			HttpResponse response = httpClient.execute(httpGet);
47
+			HttpEntity entity = response.getEntity();
48
+			if (response.getStatusLine().getStatusCode() != 200) {
49
+				XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatusLine().getStatusCode());
50
+				return FAIL;
51
+			}
52
+			if (null == entity) {
53
+				XxlJobLogger.log("Http Entity Empty.");
54
+				return FAIL;
55
+			}
56
+
57
+			String responseMsg = EntityUtils.toString(entity, "UTF-8");
58
+			XxlJobLogger.log(responseMsg);
59
+			EntityUtils.consume(entity);
60
+			return SUCCESS;
61
+		} catch (Exception e) {
62
+			XxlJobLogger.log(e);
63
+			return FAIL;
64
+		} finally{
65
+			if (httpGet!=null) {
66
+				httpGet.releaseConnection();
67
+			}
68
+			if (httpClient!=null) {
69
+				try {
70
+					httpClient.close();
71
+				} catch (IOException e) {
72
+					XxlJobLogger.log(e);
73
+				}
74
+			}
75
+		}
76
+	}
77
+
78
+}

+ 0 - 1
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java 查看文件

7
 import com.xxl.job.core.util.ShardingUtil;
7
 import com.xxl.job.core.util.ShardingUtil;
8
 import org.springframework.stereotype.Service;
8
 import org.springframework.stereotype.Service;
9
 
9
 
10
-
11
 /**
10
 /**
12
  * 分片广播任务
11
  * 分片广播任务
13
  *
12
  *