From a1755156e2da257b0c3708bfbf34d876d91dfb2c Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Sun, 16 Sep 2018 04:33:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=A8=E5=B9=B3=E5=8F=B0=EF=BC=9A=E9=99=A4?= =?UTF-8?q?=E4=BA=86=E6=8F=90=E4=BE=9BJava=E3=80=81Python=E3=80=81PHP?= =?UTF-8?q?=E7=AD=89=E5=8D=81=E6=9D=A5=E7=A7=8D=E4=BB=BB=E5=8A=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B9=8B=E5=A4=96=EF=BC=8C=E6=96=B0=E5=A2=9E=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E5=9F=BA=E4=BA=8EHTTP=E7=9A=84=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/XXL-JOB官方文档.md | 8 +- .../jfinal/config/JFinalCoreConfig.java | 2 + .../jfinal/jobhandler/HttpJobHandler.java | 77 ++++++++++++++++++ .../jfinal/jobhandler/ShardingJobHandler.java | 1 - .../nutz/jobhandler/HttpJobHandler.java | 78 +++++++++++++++++++ .../service/jobhandler/HttpJobHandler.java | 78 +++++++++++++++++++ .../jobhandler/ShardingJobHandler.java | 1 - .../service/jobhandler/HttpJobHandler.java | 78 +++++++++++++++++++ .../jobhandler/ShardingJobHandler.java | 1 - 9 files changed, 320 insertions(+), 4 deletions(-) create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/HttpJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/HttpJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java diff --git a/doc/XXL-JOB官方文档.md b/doc/XXL-JOB官方文档.md index dfcf7ad0..d19d1784 100644 --- a/doc/XXL-JOB官方文档.md +++ b/doc/XXL-JOB官方文档.md @@ -955,6 +955,11 @@ echo "分片总数 total = $3" 需要注意的是,任务超时中断时与任务终止机制(可查看“4.8 终止运行中的任务”)类似,也是通过 "interrupt" 中断任务,因此业务代码需要将 "InterruptedException" 外抛,否则功能不可用。 +### 5.17 跨平台 & 跨语言 +跨平台、跨语言主要体现在以下两个方面: +- 1、提供Java、Python、PHP……等十来种任务模式,可参考章节 “5.5 任务 "运行模式" ”;理论上可扩展任意语言任务模式; +- 2、提供基于HTTP的任务Handler(Bean任务,JobHandler="httpJobHandler");业务方只需要提供HTTP链接即可,不限制语言、平台; + ## 六、版本更新日志 ### 6.1 版本 V1.1.x,新特性[2015-12-05] @@ -1244,7 +1249,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段 - 28、新增任务运行模式 "GLUE模式(PowerShell) ",支持PowerShell脚本任务; - 29、GLUE脚本文件自动清理功能,及时清理过期脚本文件; - 30、执行器注册方式切换优化,切换自动注册时主动同步在线机器,避免执行器为空的问题; -- 31、【迭代中】分片任务失败重试优化,仅重试当前失败的分片; +- 31、跨平台:除了提供Java、Python、PHP等十来种任务模式之外,新增提供基于HTTP的任务模式; +- 32、【迭代中】分片任务失败重试优化,仅重试当前失败的分片; ### TODO LIST diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/config/JFinalCoreConfig.java b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/config/JFinalCoreConfig.java index 8a32f53f..f3c15c86 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/config/JFinalCoreConfig.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/config/JFinalCoreConfig.java @@ -5,6 +5,7 @@ import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; import com.xuxueli.executor.sample.jfinal.controller.IndexController; import com.xuxueli.executor.sample.jfinal.jobhandler.DemoJobHandler; +import com.xuxueli.executor.sample.jfinal.jobhandler.HttpJobHandler; import com.xuxueli.executor.sample.jfinal.jobhandler.ShardingJobHandler; import com.xxl.job.core.executor.XxlJobExecutor; import org.slf4j.Logger; @@ -23,6 +24,7 @@ public class JFinalCoreConfig extends JFinalConfig { // registry jobhandler XxlJobExecutor.registJobHandler("demoJobHandler", new DemoJobHandler()); XxlJobExecutor.registJobHandler("shardingJobHandler", new ShardingJobHandler()); + XxlJobExecutor.registJobHandler("httpJobHandler", new HttpJobHandler()); // load executor prop Prop xxlJobProp = PropKit.use("xxl-job-executor.properties"); diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/HttpJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/HttpJobHandler.java new file mode 100644 index 00000000..8e808cb6 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/HttpJobHandler.java @@ -0,0 +1,77 @@ +package com.xuxueli.executor.sample.jfinal.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.log.XxlJobLogger; +import com.xxl.job.core.util.ShardingUtil; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +/** + * 跨平台Http任务 + * + * @author xuxueli 2018-09-16 03:48:34 + */ +public class HttpJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String param) throws Exception { + + // valid + if (param==null || param.trim().length()==0) { + XxlJobLogger.log("URL Empty"); + return FAIL; + } + + // httpGet config + HttpGet httpGet = new HttpGet(param); + RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); + httpGet.setConfig(requestConfig); + + CloseableHttpClient httpClient = null; + try{ + httpClient = HttpClients.custom().disableAutomaticRetries().build(); + + // parse response + HttpResponse response = httpClient.execute(httpGet); + HttpEntity entity = response.getEntity(); + if (response.getStatusLine().getStatusCode() != 200) { + XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatusLine().getStatusCode()); + return FAIL; + } + if (null == entity) { + XxlJobLogger.log("Http Entity Empty."); + return FAIL; + } + + String responseMsg = EntityUtils.toString(entity, "UTF-8"); + XxlJobLogger.log(responseMsg); + EntityUtils.consume(entity); + return SUCCESS; + } catch (Exception e) { + XxlJobLogger.log(e); + return FAIL; + } finally{ + if (httpGet!=null) { + httpGet.releaseConnection(); + } + if (httpClient!=null) { + try { + httpClient.close(); + } catch (IOException e) { + XxlJobLogger.log(e); + } + } + } + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java index e881f734..bb3fa2c5 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/ShardingJobHandler.java @@ -5,7 +5,6 @@ import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.log.XxlJobLogger; import com.xxl.job.core.util.ShardingUtil; - /** * 分片广播任务 * diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/HttpJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/HttpJobHandler.java new file mode 100644 index 00000000..2254244b --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/HttpJobHandler.java @@ -0,0 +1,78 @@ +package com.xuxueli.executor.sample.nutz.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.JobHandler; +import com.xxl.job.core.log.XxlJobLogger; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.nutz.ioc.loader.annotation.IocBean; + +import java.io.IOException; + +/** + * 跨平台Http任务 + * + * @author xuxueli 2018-09-16 03:48:34 + */ +@JobHandler(value="httpJobHandler") +@IocBean +public class HttpJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String param) throws Exception { + + // valid + if (param==null || param.trim().length()==0) { + XxlJobLogger.log("URL Empty"); + return FAIL; + } + + // httpGet config + HttpGet httpGet = new HttpGet(param); + RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); + httpGet.setConfig(requestConfig); + + CloseableHttpClient httpClient = null; + try{ + httpClient = HttpClients.custom().disableAutomaticRetries().build(); + + // parse response + HttpResponse response = httpClient.execute(httpGet); + HttpEntity entity = response.getEntity(); + if (response.getStatusLine().getStatusCode() != 200) { + XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatusLine().getStatusCode()); + return FAIL; + } + if (null == entity) { + XxlJobLogger.log("Http Entity Empty."); + return FAIL; + } + + String responseMsg = EntityUtils.toString(entity, "UTF-8"); + XxlJobLogger.log(responseMsg); + EntityUtils.consume(entity); + return SUCCESS; + } catch (Exception e) { + XxlJobLogger.log(e); + return FAIL; + } finally{ + if (httpGet!=null) { + httpGet.releaseConnection(); + } + if (httpClient!=null) { + try { + httpClient.close(); + } catch (IOException e) { + XxlJobLogger.log(e); + } + } + } + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java new file mode 100644 index 00000000..400e138b --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java @@ -0,0 +1,78 @@ +package com.xxl.job.executor.service.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.JobHandler; +import com.xxl.job.core.log.XxlJobLogger; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +/** + * 跨平台Http任务 + * + * @author xuxueli 2018-09-16 03:48:34 + */ +@JobHandler(value="httpJobHandler") +@Component +public class HttpJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String param) throws Exception { + + // valid + if (param==null || param.trim().length()==0) { + XxlJobLogger.log("URL Empty"); + return FAIL; + } + + // httpGet config + HttpGet httpGet = new HttpGet(param); + RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); + httpGet.setConfig(requestConfig); + + CloseableHttpClient httpClient = null; + try{ + httpClient = HttpClients.custom().disableAutomaticRetries().build(); + + // parse response + HttpResponse response = httpClient.execute(httpGet); + HttpEntity entity = response.getEntity(); + if (response.getStatusLine().getStatusCode() != 200) { + XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatusLine().getStatusCode()); + return FAIL; + } + if (null == entity) { + XxlJobLogger.log("Http Entity Empty."); + return FAIL; + } + + String responseMsg = EntityUtils.toString(entity, "UTF-8"); + XxlJobLogger.log(responseMsg); + EntityUtils.consume(entity); + return SUCCESS; + } catch (Exception e) { + XxlJobLogger.log(e); + return FAIL; + } finally{ + if (httpGet!=null) { + httpGet.releaseConnection(); + } + if (httpClient!=null) { + try { + httpClient.close(); + } catch (IOException e) { + XxlJobLogger.log(e); + } + } + } + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java index 9b7408fe..d7f20c2b 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java @@ -7,7 +7,6 @@ import com.xxl.job.core.log.XxlJobLogger; import com.xxl.job.core.util.ShardingUtil; import org.springframework.stereotype.Service; - /** * 分片广播任务 * diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java new file mode 100644 index 00000000..400e138b --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java @@ -0,0 +1,78 @@ +package com.xxl.job.executor.service.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.JobHandler; +import com.xxl.job.core.log.XxlJobLogger; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +/** + * 跨平台Http任务 + * + * @author xuxueli 2018-09-16 03:48:34 + */ +@JobHandler(value="httpJobHandler") +@Component +public class HttpJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String param) throws Exception { + + // valid + if (param==null || param.trim().length()==0) { + XxlJobLogger.log("URL Empty"); + return FAIL; + } + + // httpGet config + HttpGet httpGet = new HttpGet(param); + RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); + httpGet.setConfig(requestConfig); + + CloseableHttpClient httpClient = null; + try{ + httpClient = HttpClients.custom().disableAutomaticRetries().build(); + + // parse response + HttpResponse response = httpClient.execute(httpGet); + HttpEntity entity = response.getEntity(); + if (response.getStatusLine().getStatusCode() != 200) { + XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatusLine().getStatusCode()); + return FAIL; + } + if (null == entity) { + XxlJobLogger.log("Http Entity Empty."); + return FAIL; + } + + String responseMsg = EntityUtils.toString(entity, "UTF-8"); + XxlJobLogger.log(responseMsg); + EntityUtils.consume(entity); + return SUCCESS; + } catch (Exception e) { + XxlJobLogger.log(e); + return FAIL; + } finally{ + if (httpGet!=null) { + httpGet.releaseConnection(); + } + if (httpClient!=null) { + try { + httpClient.close(); + } catch (IOException e) { + XxlJobLogger.log(e); + } + } + } + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java index 96acab5a..3c3a8fe7 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java @@ -7,7 +7,6 @@ import com.xxl.job.core.log.XxlJobLogger; import com.xxl.job.core.util.ShardingUtil; import org.springframework.stereotype.Service; - /** * 分片广播任务 *