Prechádzať zdrojové kódy

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

xuxueli 7 rokov pred
rodič
commit
a1755156e2

+ 7 - 1
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

+ 2 - 0
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");

+ 77 - 0
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<String> 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);
+				}
+			}
+		}
+	}
+
+}

+ 0 - 1
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;
 
-
 /**
  * 分片广播任务
  *

+ 78 - 0
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<String> 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);
+				}
+			}
+		}
+	}
+
+}

+ 78 - 0
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<String> 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);
+				}
+			}
+		}
+	}
+
+}

+ 0 - 1
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;
 
-
 /**
  * 分片广播任务
  *

+ 78 - 0
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<String> 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);
+				}
+			}
+		}
+	}
+
+}

+ 0 - 1
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;
 
-
 /**
  * 分片广播任务
  *