通用HTTP任务Handler(httpJobHandler)优化,扩展自定义参数信息,示例参数如下;

pull/16/head
xuxueli 5 years ago
parent ffcc82f23e
commit 239200e52c

@ -766,7 +766,7 @@ public ReturnT<String> execute(String param) {
- demoJobHandler简单示例任务任务内部模拟耗时任务逻辑用户可在线体验Rolling Log等功能
- shardingJobHandler分片示例任务任务内部模拟处理分片参数可参考熟悉分片任务
- httpJobHandler通用HTTP任务Handler业务方只需要提供HTTP链接即可不限制语言、平台
- httpJobHandler通用HTTP任务Handler业务方只需要提供HTTP链接等信息即可,不限制语言、平台;
- commandJobHandler通用命令行任务Handler业务方只需要提供命令行即可如 “pwd”命令
@ -1261,7 +1261,12 @@ API服务请求参考代码com.xxl.job.executor.ExecutorBizTest
### 5.17 跨平台 & 跨语言
跨平台、跨语言主要体现在以下两个方面:
- 1、提供Java、Python、PHP……等十来种任务模式可参考章节 “5.5 任务 "运行模式" ”;理论上可扩展任意语言任务模式;
- 2、提供基于HTTP的任务HandlerBean任务JobHandler="HttpJobHandler"业务方只需要提供HTTP链接即可不限制语言、平台
- 2、提供基于HTTP的任务HandlerBean任务JobHandler="httpJobHandler"业务方只需要提供HTTP链接等相关信息即可不限制语言、平台
```
url: http://www.xxx.com
method: get 或 post
data: post-data
```
### 5.18 任务失败告警
默认提供邮件失败告警,可扩展短信、钉钉等方式。如果需要新增一种告警方式,只需要新增一个实现 "com.xxl.job.admin.core.alarm.JobAlarm" 接口的告警实现即可。可以参考默认提供邮箱告警实现 "EmailJobAlarm"。
@ -1718,12 +1723,18 @@ public ReturnT<String> execute(String param) {
- 11、修复bootstrap.min.css.map 404问题
- 12、XxlJob注解扫描方式优化支持查找父类以及接口和基于类代理等常见情况
- 13、执行器优雅停机优化修复任务线程中断未join导致回调丢失的问题
- 14、[迭代中]自定义失败重试时间间隔;
- 15、[迭代中]任务复制功能;点击复制是弹出新建任务弹框,并初始化被复制任务信息;
- 16、[迭代中]新增执行器描述、任务描述属性;
- 17、[迭代中]任务执行一次的时候指定IP
- 18、[迭代中]任务日志支持单个清理和状态转移,方便触发子任务;
- 19、[迭代中]任务结果丢失处理:针对长期处于运行中的任务(设置过期时间时,运行超过"过期时间+1min";未设置超时时间时,运行超过"30min"),主动检测该执行器是否在线,如果不在线主动标记失败;
- 14、通用HTTP任务HandlerhttpJobHandler优化扩展自定义参数信息示例参数如下
```
url: http://www.xxx.com
method: get 或 post
data: post-data
```
- 15、[迭代中]自定义失败重试时间间隔;
- 16、[迭代中]任务复制功能;点击复制是弹出新建任务弹框,并初始化被复制任务信息;
- 17、[迭代中]新增执行器描述、任务描述属性;
- 18、[迭代中]任务执行一次的时候指定IP
- 19、[迭代中]任务日志支持单个清理和状态转移,方便触发子任务;
- 20、[迭代中]任务结果丢失处理:针对长期处于运行中的任务(设置过期时间时,运行超过"过期时间+1min";未设置超时时间时,运行超过"30min"),主动检测该执行器是否在线,如果不在线主动标记失败;
### TODO LIST

@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
/**
* Http
@ -19,10 +21,35 @@ public class HttpJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
// valid
// param parse
if (param==null || param.trim().length()==0) {
XxlJobLogger.log("URL Empty");
return FAIL;
XxlJobLogger.log("param["+ param +"] invalid.");
return ReturnT.FAIL;
}
String[] httpParams = param.split("\n");
String url = null;
String method = null;
String data = null;
for (String httpParam: httpParams) {
if (httpParam.startsWith("url:")) {
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
}
if (httpParam.startsWith("method:")) {
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
}
if (httpParam.startsWith("data:")) {
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
}
}
// param valid
if (url==null || url.trim().length()==0) {
XxlJobLogger.log("url["+ url +"] invalid.");
return ReturnT.FAIL;
}
if (method==null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobLogger.log("method["+ method +"] invalid.");
return ReturnT.FAIL;
}
// request
@ -30,11 +57,11 @@ public class HttpJobHandler extends IJobHandler {
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(param);
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();
// connection setting
connection.setRequestMethod("GET");
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
@ -47,12 +74,18 @@ public class HttpJobHandler extends IJobHandler {
// do connection
connection.connect();
//Map<String, List<String>> map = connection.getHeaderFields();
// data
if (data!=null && data.trim().length()>0) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}
// valid StatusCode
int statusCode = connection.getResponseCode();
if (statusCode != 200) {
throw new RuntimeException("Http Request StatusCode("+ statusCode +") Invalid.");
throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
}
// result
@ -65,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
String responseMsg = result.toString();
XxlJobLogger.log(responseMsg);
return SUCCESS;
return ReturnT.SUCCESS;
} catch (Exception e) {
XxlJobLogger.log(e);
return FAIL;
return ReturnT.FAIL;
} finally {
try {
if (bufferedReader != null) {

@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
/**
* Http
@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
// param parse
if (param==null || param.trim().length()==0) {
XxlJobLogger.log("param["+ param +"] invalid.");
return ReturnT.FAIL;
}
String[] httpParams = param.split("\n");
String url = null;
String method = null;
String data = null;
for (String httpParam: httpParams) {
if (httpParam.startsWith("url:")) {
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
}
if (httpParam.startsWith("method:")) {
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
}
if (httpParam.startsWith("data:")) {
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
}
}
// param valid
if (url==null || url.trim().length()==0) {
XxlJobLogger.log("url["+ url +"] invalid.");
return ReturnT.FAIL;
}
if (method==null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobLogger.log("method["+ method +"] invalid.");
return ReturnT.FAIL;
}
// request
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(param);
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();
// connection setting
connection.setRequestMethod("GET");
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
// do connection
connection.connect();
//Map<String, List<String>> map = connection.getHeaderFields();
// data
if (data!=null && data.trim().length()>0) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}
// valid StatusCode
int statusCode = connection.getResponseCode();
@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
String responseMsg = result.toString();
XxlJobLogger.log(responseMsg);
return SUCCESS;
return ReturnT.SUCCESS;
} catch (Exception e) {
XxlJobLogger.log(e);
return FAIL;
return ReturnT.FAIL;
} finally {
try {
if (bufferedReader != null) {

@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
/**
* Http
@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
// param parse
if (param==null || param.trim().length()==0) {
XxlJobLogger.log("param["+ param +"] invalid.");
return ReturnT.FAIL;
}
String[] httpParams = param.split("\n");
String url = null;
String method = null;
String data = null;
for (String httpParam: httpParams) {
if (httpParam.startsWith("url:")) {
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
}
if (httpParam.startsWith("method:")) {
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
}
if (httpParam.startsWith("data:")) {
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
}
}
// param valid
if (url==null || url.trim().length()==0) {
XxlJobLogger.log("url["+ url +"] invalid.");
return ReturnT.FAIL;
}
if (method==null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobLogger.log("method["+ method +"] invalid.");
return ReturnT.FAIL;
}
// request
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(param);
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();
// connection setting
connection.setRequestMethod("GET");
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
// do connection
connection.connect();
//Map<String, List<String>> map = connection.getHeaderFields();
// data
if (data!=null && data.trim().length()>0) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}
// valid StatusCode
int statusCode = connection.getResponseCode();
@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
String responseMsg = result.toString();
XxlJobLogger.log(responseMsg);
return SUCCESS;
return ReturnT.SUCCESS;
} catch (Exception e) {
XxlJobLogger.log(e);
return FAIL;
return ReturnT.FAIL;
} finally {
try {
if (bufferedReader != null) {

@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
/**
* Http
@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
// param parse
if (param==null || param.trim().length()==0) {
XxlJobLogger.log("param["+ param +"] invalid.");
return ReturnT.FAIL;
}
String[] httpParams = param.split("\n");
String url = null;
String method = null;
String data = null;
for (String httpParam: httpParams) {
if (httpParam.startsWith("url:")) {
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
}
if (httpParam.startsWith("method:")) {
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
}
if (httpParam.startsWith("data:")) {
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
}
}
// param valid
if (url==null || url.trim().length()==0) {
XxlJobLogger.log("url["+ url +"] invalid.");
return ReturnT.FAIL;
}
if (method==null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobLogger.log("method["+ method +"] invalid.");
return ReturnT.FAIL;
}
// request
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(param);
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();
// connection setting
connection.setRequestMethod("GET");
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
// do connection
connection.connect();
//Map<String, List<String>> map = connection.getHeaderFields();
// data
if (data!=null && data.trim().length()>0) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}
// valid StatusCode
int statusCode = connection.getResponseCode();
@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
String responseMsg = result.toString();
XxlJobLogger.log(responseMsg);
return SUCCESS;
return ReturnT.SUCCESS;
} catch (Exception e) {
XxlJobLogger.log(e);
return FAIL;
return ReturnT.FAIL;
} finally {
try {
if (bufferedReader != null) {

@ -11,9 +11,11 @@ import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
@ -111,20 +113,55 @@ public class SampleXxlJob {
/**
* 4Http
*
* "url: http://www.baidu.com\n" +
* "method: get\n" +
* "data: content\n";
*/
@XxlJob("httpJobHandler")
public ReturnT<String> httpJobHandler(String param) throws Exception {
// param parse
if (param==null || param.trim().length()==0) {
XxlJobLogger.log("param["+ param +"] invalid.");
return ReturnT.FAIL;
}
String[] httpParams = param.split("\n");
String url = null;
String method = null;
String data = null;
for (String httpParam: httpParams) {
if (httpParam.startsWith("url:")) {
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
}
if (httpParam.startsWith("method:")) {
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
}
if (httpParam.startsWith("data:")) {
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
}
}
// param valid
if (url==null || url.trim().length()==0) {
XxlJobLogger.log("url["+ url +"] invalid.");
return ReturnT.FAIL;
}
if (method==null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobLogger.log("method["+ method +"] invalid.");
return ReturnT.FAIL;
}
// request
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(param);
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();
// connection setting
connection.setRequestMethod("GET");
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
@ -137,7 +174,13 @@ public class SampleXxlJob {
// do connection
connection.connect();
//Map<String, List<String>> map = connection.getHeaderFields();
// data
if (data!=null && data.trim().length()>0) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}
// valid StatusCode
int statusCode = connection.getResponseCode();

@ -11,9 +11,11 @@ import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
@ -111,20 +113,55 @@ public class SampleXxlJob {
/**
* 4Http
*
* "url: http://www.baidu.com\n" +
* "method: get\n" +
* "data: content\n";
*/
@XxlJob("httpJobHandler")
public ReturnT<String> httpJobHandler(String param) throws Exception {
// param parse
if (param==null || param.trim().length()==0) {
XxlJobLogger.log("param["+ param +"] invalid.");
return ReturnT.FAIL;
}
String[] httpParams = param.split("\n");
String url = null;
String method = null;
String data = null;
for (String httpParam: httpParams) {
if (httpParam.startsWith("url:")) {
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
}
if (httpParam.startsWith("method:")) {
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
}
if (httpParam.startsWith("data:")) {
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
}
}
// param valid
if (url==null || url.trim().length()==0) {
XxlJobLogger.log("url["+ url +"] invalid.");
return ReturnT.FAIL;
}
if (method==null || !Arrays.asList("GET", "POST").contains(method)) {
XxlJobLogger.log("method["+ method +"] invalid.");
return ReturnT.FAIL;
}
// request
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(param);
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();
// connection setting
connection.setRequestMethod("GET");
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
@ -137,7 +174,13 @@ public class SampleXxlJob {
// do connection
connection.connect();
//Map<String, List<String>> map = connection.getHeaderFields();
// data
if (data!=null && data.trim().length()>0) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}
// valid StatusCode
int statusCode = connection.getResponseCode();

Loading…
Cancel
Save