Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
熊鹏飞
/
xxljob220
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
239200e5
authored
Mar 29, 2020
by
xuxueli
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
通用HTTP任务Handler(httpJobHandler)优化,扩展自定义参数信息,示例参数如下;
parent
ffcc82f2
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
286 additions
and
39 deletions
doc/XXL-JOB官方文档.md
xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xuxueli/executor/sample/frameless/jobhandler/HttpJobHandler.java
xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/HttpJobHandler.java
xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/HttpJobHandler.java
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/HttpJobHandler.java
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java
doc/XXL-JOB官方文档.md
View file @
239200e5
## 《分
布式任务调度平台XXL-JOB》
## 《分
布式任务调度平台XXL-JOB》
...
@@ -766,7 +766,7 @@ public ReturnT<String> execute(String param) {
...
@@ -766,7 +766,7 @@ public ReturnT<String> execute(String param) {
-
demoJobHandler:简单示例任务,任务内部模拟耗时任务逻辑,用户可在线体验Rolling Log等功能;
-
demoJobHandler:简单示例任务,任务内部模拟耗时任务逻辑,用户可在线体验Rolling Log等功能;
-
shardingJobHandler:分片示例任务,任务内部模拟处理分片参数,可参考熟悉分片任务;
-
shardingJobHandler:分片示例任务,任务内部模拟处理分片参数,可参考熟悉分片任务;
-
httpJobHandler:通用HTTP任务Handler;业务方只需要提供HTTP链接即可,不限制语言、平台;
-
httpJobHandler:通用HTTP任务Handler;业务方只需要提供HTTP链接
等信息
即可,不限制语言、平台;
-
commandJobHandler:通用命令行任务Handler;业务方只需要提供命令行即可;如 “pwd”命令;
-
commandJobHandler:通用命令行任务Handler;业务方只需要提供命令行即可;如 “pwd”命令;
...
@@ -1261,7 +1261,12 @@ API服务请求参考代码:com.xxl.job.executor.ExecutorBizTest
...
@@ -1261,7 +1261,12 @@ API服务请求参考代码:com.xxl.job.executor.ExecutorBizTest
### 5.17 跨平台 & 跨语言
### 5.17 跨平台 & 跨语言
跨平台、跨语言主要体现在以下两个方面:
跨平台、跨语言主要体现在以下两个方面:
-
1、提供Java、Python、PHP……等十来种任务模式,可参考章节 “5.5 任务 "运行模式" ”;理论上可扩展任意语言任务模式;
-
1、提供Java、Python、PHP……等十来种任务模式,可参考章节 “5.5 任务 "运行模式" ”;理论上可扩展任意语言任务模式;
-
2、提供基于HTTP的任务Handler(Bean任务,JobHandler="HttpJobHandler");业务方只需要提供HTTP链接即可,不限制语言、平台;
-
2、提供基于HTTP的任务Handler(Bean任务,JobHandler="httpJobHandler");业务方只需要提供HTTP链接等相关信息即可,不限制语言、平台;
```
url: http://www.xxx.com
method: get 或 post
data: post-data
```
### 5.18 任务失败告警
### 5.18 任务失败告警
默认提供邮件失败告警,可扩展短信、钉钉等方式。如果需要新增一种告警方式,只需要新增一个实现 "com.xxl.job.admin.core.alarm.JobAlarm" 接口的告警实现即可。可以参考默认提供邮箱告警实现 "EmailJobAlarm"。
默认提供邮件失败告警,可扩展短信、钉钉等方式。如果需要新增一种告警方式,只需要新增一个实现 "com.xxl.job.admin.core.alarm.JobAlarm" 接口的告警实现即可。可以参考默认提供邮箱告警实现 "EmailJobAlarm"。
...
@@ -1718,12 +1723,18 @@ public ReturnT<String> execute(String param) {
...
@@ -1718,12 +1723,18 @@ public ReturnT<String> execute(String param) {
- 11、修复bootstrap.min.css.map 404问题;
- 11、修复bootstrap.min.css.map 404问题;
- 12、XxlJob注解扫描方式优化,支持查找父类以及接口和基于类代理等常见情况;
- 12、XxlJob注解扫描方式优化,支持查找父类以及接口和基于类代理等常见情况;
- 13、执行器优雅停机优化,修复任务线程中断未join导致回调丢失的问题;
- 13、执行器优雅停机优化,修复任务线程中断未join导致回调丢失的问题;
-
14、
[
迭代中
]
自定义失败重试时间间隔;
- 14、通用HTTP任务Handler(httpJobHandler)优化,扩展自定义参数信息,示例参数如下;
-
15、
[
迭代中
]
任务复制功能;点击复制是弹出新建任务弹框,并初始化被复制任务信息;
```
-
16、
[
迭代中
]
新增执行器描述、任务描述属性;
url: http://www.xxx.com
-
17、
[
迭代中
]
任务执行一次的时候指定IP;
method: get 或 post
-
18、
[
迭代中
]
任务日志支持单个清理和状态转移,方便触发子任务;
data: post-data
-
19、
[
迭代中
]
任务结果丢失处理:针对长期处于运行中的任务(设置过期时间时,运行超过"过期时间+1min";未设置超时时间时,运行超过"30min"),主动检测该执行器是否在线,如果不在线主动标记失败;
```
-
15、
[
迭代中
]
自定义失败重试时间间隔;
-
16、
[
迭代中
]
任务复制功能;点击复制是弹出新建任务弹框,并初始化被复制任务信息;
-
17、
[
迭代中
]
新增执行器描述、任务描述属性;
-
18、
[
迭代中
]
任务执行一次的时候指定IP;
-
19、
[
迭代中
]
任务日志支持单个清理和状态转移,方便触发子任务;
-
20、
[
迭代中
]
任务结果丢失处理:针对长期处于运行中的任务(设置过期时间时,运行超过"过期时间+1min";未设置超时时间时,运行超过"30min"),主动检测该执行器是否在线,如果不在线主动标记失败;
### TODO LIST
### TODO LIST
...
...
xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xuxueli/executor/sample/frameless/jobhandler/HttpJobHandler.java
View file @
239200e5
...
@@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
...
@@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.Arrays
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
...
@@ -19,10 +21,35 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -19,10 +21,35 @@ public class HttpJobHandler extends IJobHandler {
@Override
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
//
valid
//
param parse
if
(
param
==
null
||
param
.
trim
().
length
()==
0
)
{
if
(
param
==
null
||
param
.
trim
().
length
()==
0
)
{
XxlJobLogger
.
log
(
"URL Empty"
);
XxlJobLogger
.
log
(
"param["
+
param
+
"] invalid."
);
return
FAIL
;
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
// request
...
@@ -30,11 +57,11 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -30,11 +57,11 @@ public class HttpJobHandler extends IJobHandler {
BufferedReader
bufferedReader
=
null
;
BufferedReader
bufferedReader
=
null
;
try
{
try
{
// connection
// connection
URL
realUrl
=
new
URL
(
param
);
URL
realUrl
=
new
URL
(
url
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// connection setting
// connection setting
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestMethod
(
method
);
connection
.
setDoOutput
(
true
);
connection
.
setDoOutput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setUseCaches
(
false
);
connection
.
setUseCaches
(
false
);
...
@@ -47,12 +74,18 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -47,12 +74,18 @@ public class HttpJobHandler extends IJobHandler {
// do connection
// do connection
connection
.
connect
();
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
// valid StatusCode
int
statusCode
=
connection
.
getResponseCode
();
int
statusCode
=
connection
.
getResponseCode
();
if
(
statusCode
!=
200
)
{
if
(
statusCode
!=
200
)
{
throw
new
RuntimeException
(
"Http Request StatusCode("
+
statusCode
+
") Invalid."
);
throw
new
RuntimeException
(
"Http Request StatusCode("
+
statusCode
+
") Invalid."
);
}
}
// result
// result
...
@@ -65,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -65,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
String
responseMsg
=
result
.
toString
();
String
responseMsg
=
result
.
toString
();
XxlJobLogger
.
log
(
responseMsg
);
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
return
ReturnT
.
SUCCESS
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
XxlJobLogger
.
log
(
e
);
return
FAIL
;
return
ReturnT
.
FAIL
;
}
finally
{
}
finally
{
try
{
try
{
if
(
bufferedReader
!=
null
)
{
if
(
bufferedReader
!=
null
)
{
...
...
xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/HttpJobHandler.java
View file @
239200e5
...
@@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
...
@@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.Arrays
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
...
@@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
@Override
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
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
// request
HttpURLConnection
connection
=
null
;
HttpURLConnection
connection
=
null
;
BufferedReader
bufferedReader
=
null
;
BufferedReader
bufferedReader
=
null
;
try
{
try
{
// connection
// connection
URL
realUrl
=
new
URL
(
param
);
URL
realUrl
=
new
URL
(
url
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// connection setting
// connection setting
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestMethod
(
method
);
connection
.
setDoOutput
(
true
);
connection
.
setDoOutput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setUseCaches
(
false
);
connection
.
setUseCaches
(
false
);
...
@@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
// do connection
// do connection
connection
.
connect
();
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
// valid StatusCode
int
statusCode
=
connection
.
getResponseCode
();
int
statusCode
=
connection
.
getResponseCode
();
...
@@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
String
responseMsg
=
result
.
toString
();
String
responseMsg
=
result
.
toString
();
XxlJobLogger
.
log
(
responseMsg
);
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
return
ReturnT
.
SUCCESS
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
XxlJobLogger
.
log
(
e
);
return
FAIL
;
return
ReturnT
.
FAIL
;
}
finally
{
}
finally
{
try
{
try
{
if
(
bufferedReader
!=
null
)
{
if
(
bufferedReader
!=
null
)
{
...
...
xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/HttpJobHandler.java
View file @
239200e5
...
@@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
...
@@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.Arrays
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
...
@@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
@Override
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
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
// request
HttpURLConnection
connection
=
null
;
HttpURLConnection
connection
=
null
;
BufferedReader
bufferedReader
=
null
;
BufferedReader
bufferedReader
=
null
;
try
{
try
{
// connection
// connection
URL
realUrl
=
new
URL
(
param
);
URL
realUrl
=
new
URL
(
url
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// connection setting
// connection setting
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestMethod
(
method
);
connection
.
setDoOutput
(
true
);
connection
.
setDoOutput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setUseCaches
(
false
);
connection
.
setUseCaches
(
false
);
...
@@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
// do connection
// do connection
connection
.
connect
();
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
// valid StatusCode
int
statusCode
=
connection
.
getResponseCode
();
int
statusCode
=
connection
.
getResponseCode
();
...
@@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
String
responseMsg
=
result
.
toString
();
String
responseMsg
=
result
.
toString
();
XxlJobLogger
.
log
(
responseMsg
);
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
return
ReturnT
.
SUCCESS
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
XxlJobLogger
.
log
(
e
);
return
FAIL
;
return
ReturnT
.
FAIL
;
}
finally
{
}
finally
{
try
{
try
{
if
(
bufferedReader
!=
null
)
{
if
(
bufferedReader
!=
null
)
{
...
...
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/HttpJobHandler.java
View file @
239200e5
...
@@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
...
@@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.Arrays
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
...
@@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -19,16 +21,47 @@ public class HttpJobHandler extends IJobHandler {
@Override
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
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
// request
HttpURLConnection
connection
=
null
;
HttpURLConnection
connection
=
null
;
BufferedReader
bufferedReader
=
null
;
BufferedReader
bufferedReader
=
null
;
try
{
try
{
// connection
// connection
URL
realUrl
=
new
URL
(
param
);
URL
realUrl
=
new
URL
(
url
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// connection setting
// connection setting
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestMethod
(
method
);
connection
.
setDoOutput
(
true
);
connection
.
setDoOutput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setUseCaches
(
false
);
connection
.
setUseCaches
(
false
);
...
@@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -41,7 +74,13 @@ public class HttpJobHandler extends IJobHandler {
// do connection
// do connection
connection
.
connect
();
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
// valid StatusCode
int
statusCode
=
connection
.
getResponseCode
();
int
statusCode
=
connection
.
getResponseCode
();
...
@@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -59,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
String
responseMsg
=
result
.
toString
();
String
responseMsg
=
result
.
toString
();
XxlJobLogger
.
log
(
responseMsg
);
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
return
ReturnT
.
SUCCESS
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
XxlJobLogger
.
log
(
e
);
return
FAIL
;
return
ReturnT
.
FAIL
;
}
finally
{
}
finally
{
try
{
try
{
if
(
bufferedReader
!=
null
)
{
if
(
bufferedReader
!=
null
)
{
...
...
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java
View file @
239200e5
...
@@ -11,9 +11,11 @@ import org.springframework.stereotype.Component;
...
@@ -11,9 +11,11 @@ import org.springframework.stereotype.Component;
import
java.io.BufferedInputStream
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.Arrays
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
/**
/**
...
@@ -111,20 +113,55 @@ public class SampleXxlJob {
...
@@ -111,20 +113,55 @@ public class SampleXxlJob {
/**
/**
* 4、跨平台Http任务
* 4、跨平台Http任务
* 参数示例:
* "url: http://www.baidu.com\n" +
* "method: get\n" +
* "data: content\n";
*/
*/
@XxlJob
(
"httpJobHandler"
)
@XxlJob
(
"httpJobHandler"
)
public
ReturnT
<
String
>
httpJobHandler
(
String
param
)
throws
Exception
{
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
// request
HttpURLConnection
connection
=
null
;
HttpURLConnection
connection
=
null
;
BufferedReader
bufferedReader
=
null
;
BufferedReader
bufferedReader
=
null
;
try
{
try
{
// connection
// connection
URL
realUrl
=
new
URL
(
param
);
URL
realUrl
=
new
URL
(
url
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// connection setting
// connection setting
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestMethod
(
method
);
connection
.
setDoOutput
(
true
);
connection
.
setDoOutput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setUseCaches
(
false
);
connection
.
setUseCaches
(
false
);
...
@@ -137,7 +174,13 @@ public class SampleXxlJob {
...
@@ -137,7 +174,13 @@ public class SampleXxlJob {
// do connection
// do connection
connection
.
connect
();
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
// valid StatusCode
int
statusCode
=
connection
.
getResponseCode
();
int
statusCode
=
connection
.
getResponseCode
();
...
...
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java
View file @
239200e5
...
@@ -11,9 +11,11 @@ import org.springframework.stereotype.Component;
...
@@ -11,9 +11,11 @@ import org.springframework.stereotype.Component;
import
java.io.BufferedInputStream
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.Arrays
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
/**
/**
...
@@ -111,20 +113,55 @@ public class SampleXxlJob {
...
@@ -111,20 +113,55 @@ public class SampleXxlJob {
/**
/**
* 4、跨平台Http任务
* 4、跨平台Http任务
* 参数示例:
* "url: http://www.baidu.com\n" +
* "method: get\n" +
* "data: content\n";
*/
*/
@XxlJob
(
"httpJobHandler"
)
@XxlJob
(
"httpJobHandler"
)
public
ReturnT
<
String
>
httpJobHandler
(
String
param
)
throws
Exception
{
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
// request
HttpURLConnection
connection
=
null
;
HttpURLConnection
connection
=
null
;
BufferedReader
bufferedReader
=
null
;
BufferedReader
bufferedReader
=
null
;
try
{
try
{
// connection
// connection
URL
realUrl
=
new
URL
(
param
);
URL
realUrl
=
new
URL
(
url
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// connection setting
// connection setting
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestMethod
(
method
);
connection
.
setDoOutput
(
true
);
connection
.
setDoOutput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setUseCaches
(
false
);
connection
.
setUseCaches
(
false
);
...
@@ -137,7 +174,13 @@ public class SampleXxlJob {
...
@@ -137,7 +174,13 @@ public class SampleXxlJob {
// do connection
// do connection
connection
.
connect
();
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
// valid StatusCode
int
statusCode
=
connection
.
getResponseCode
();
int
statusCode
=
connection
.
getResponseCode
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment