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
573facc0
authored
Feb 15, 2019
by
xuxueli
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
httpjobhandler optimize
parent
5492b70e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
296 additions
and
200 deletions
xxl-job-admin/src/main/resources/templates/jobgroup/jobgroup.index.ftl
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-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/HttpJobHandler.java
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java
xxl-job-admin/src/main/resources/templates/jobgroup/jobgroup.index.ftl
View file @
573facc0
...
@@ -121,7 +121,7 @@
...
@@ -121,7 +121,7 @@
<div
class=
"form-group"
>
<div
class=
"form-group"
>
<label
for=
"lastname"
class=
"col-sm-2 control-label"
>
${I18n.jobgroup_field_registryList}
<font
color=
"red"
>
*
</font></label>
<label
for=
"lastname"
class=
"col-sm-2 control-label"
>
${I18n.jobgroup_field_registryList}
<font
color=
"red"
>
*
</font></label>
<div
class=
"col-sm-10"
>
<div
class=
"col-sm-10"
>
<textarea
class=
"textarea"
name=
"addressList"
maxlength=
"512"
placeholder=
"${I18n.jobgroup_field_registryList_placeholder}"
readonly=
"readonly"
style=
"background-color:#eee; width: 100%; height: 100px; font-size: 14px; line-height: 1
0px; border: 1px solid #dddddd; padding: 10
px;"
></textarea>
<textarea
class=
"textarea"
name=
"addressList"
maxlength=
"512"
placeholder=
"${I18n.jobgroup_field_registryList_placeholder}"
readonly=
"readonly"
style=
"background-color:#eee; width: 100%; height: 100px; font-size: 14px; line-height: 1
5px; border: 1px solid #dddddd; padding: 5
px;"
></textarea>
</div>
</div>
</div>
</div>
<hr>
<hr>
...
@@ -169,7 +169,7 @@
...
@@ -169,7 +169,7 @@
<div
class=
"form-group"
>
<div
class=
"form-group"
>
<label
for=
"lastname"
class=
"col-sm-2 control-label"
>
${I18n.jobgroup_field_registryList}
<font
color=
"red"
>
*
</font></label>
<label
for=
"lastname"
class=
"col-sm-2 control-label"
>
${I18n.jobgroup_field_registryList}
<font
color=
"red"
>
*
</font></label>
<div
class=
"col-sm-10"
>
<div
class=
"col-sm-10"
>
<textarea
class=
"textarea"
name=
"addressList"
maxlength=
"512"
placeholder=
"${I18n.jobgroup_field_registryList_placeholder}"
readonly=
"readonly"
style=
"background-color:#eee; width: 100%; height: 100px; font-size: 14px; line-height: 1
0px; border: 1px solid #dddddd; padding: 10
px;"
></textarea>
<textarea
class=
"textarea"
name=
"addressList"
maxlength=
"512"
placeholder=
"${I18n.jobgroup_field_registryList_placeholder}"
readonly=
"readonly"
style=
"background-color:#eee; width: 100%; height: 100px; font-size: 14px; line-height: 1
5px; border: 1px solid #dddddd; padding: 5
px;"
></textarea>
</div>
</div>
</div>
</div>
<hr>
<hr>
...
...
xxl-job-executor-samples/xxl-job-executor-sample-frameless/src/main/java/com/xuxueli/executor/sample/frameless/jobhandler/HttpJobHandler.java
View file @
573facc0
...
@@ -3,13 +3,11 @@ package com.xuxueli.executor.sample.frameless.jobhandler;
...
@@ -3,13 +3,11 @@ package com.xuxueli.executor.sample.frameless.jobhandler;
import
com.xxl.job.core.biz.model.ReturnT
;
import
com.xxl.job.core.biz.model.ReturnT
;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
org.eclipse.jetty.client.HttpClient
;
import
org.eclipse.jetty.client.api.ContentResponse
;
import
org.eclipse.jetty.client.api.Request
;
import
org.eclipse.jetty.http.HttpMethod
;
import
org.eclipse.jetty.http.HttpStatus
;
import
java.util.concurrent.TimeUnit
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
...
@@ -27,34 +25,60 @@ public class HttpJobHandler extends IJobHandler {
...
@@ -27,34 +25,60 @@ public class HttpJobHandler extends IJobHandler {
return
FAIL
;
return
FAIL
;
}
}
// httpclient
// request
HttpClient
httpClient
=
null
;
HttpURLConnection
connection
=
null
;
BufferedReader
bufferedReader
=
null
;
try
{
try
{
httpClient
=
new
HttpClient
();
// connection
httpClient
.
setFollowRedirects
(
false
);
// Configure HttpClient, for example:
URL
realUrl
=
new
URL
(
param
);
httpClient
.
start
();
// Start HttpClient
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// request
// connection setting
Request
request
=
httpClient
.
newRequest
(
param
);
connection
.
setRequestMethod
(
"GET"
);
request
.
method
(
HttpMethod
.
GET
);
connection
.
setDoOutput
(
true
);
request
.
timeout
(
5000
,
TimeUnit
.
MILLISECONDS
);
connection
.
setDoInput
(
true
);
connection
.
setUseCaches
(
false
);
// invoke
connection
.
setReadTimeout
(
5
*
1000
);
ContentResponse
response
=
request
.
send
();
connection
.
setConnectTimeout
(
3
*
1000
);
if
(
response
.
getStatus
()
!=
HttpStatus
.
OK_200
)
{
connection
.
setRequestProperty
(
"connection"
,
"Keep-Alive"
);
XxlJobLogger
.
log
(
"Http StatusCode({}) Invalid."
,
response
.
getStatus
());
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json;charset=UTF-8"
);
return
FAIL
;
connection
.
setRequestProperty
(
"Accept-Charset"
,
"application/json;charset=UTF-8"
);
// do connection
connection
.
connect
();
//Map<String, List<String>> map = connection.getHeaderFields();
// valid StatusCode
int
statusCode
=
connection
.
getResponseCode
();
if
(
statusCode
!=
200
)
{
throw
new
RuntimeException
(
"Http Request StatusCode("
+
statusCode
+
") Invalid."
);
}
// result
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
()));
StringBuilder
result
=
new
StringBuilder
();
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
result
.
append
(
line
);
}
}
String
responseMsg
=
result
.
toString
();
String
responseMsg
=
response
.
getContentAsString
();
XxlJobLogger
.
log
(
responseMsg
);
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
return
SUCCESS
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
XxlJobLogger
.
log
(
e
);
return
FAIL
;
return
FAIL
;
}
finally
{
}
finally
{
if
(
httpClient
!=
null
)
{
try
{
httpClient
.
stop
();
if
(
bufferedReader
!=
null
)
{
bufferedReader
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
disconnect
();
}
}
catch
(
Exception
e2
)
{
XxlJobLogger
.
log
(
e2
);
}
}
}
}
...
...
xxl-job-executor-samples/xxl-job-executor-sample-jfinal/src/main/java/com/xuxueli/executor/sample/jfinal/jobhandler/HttpJobHandler.java
View file @
573facc0
...
@@ -3,13 +3,11 @@ package com.xuxueli.executor.sample.jfinal.jobhandler;
...
@@ -3,13 +3,11 @@ package com.xuxueli.executor.sample.jfinal.jobhandler;
import
com.xxl.job.core.biz.model.ReturnT
;
import
com.xxl.job.core.biz.model.ReturnT
;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
org.eclipse.jetty.client.HttpClient
;
import
org.eclipse.jetty.client.api.ContentResponse
;
import
org.eclipse.jetty.client.api.Request
;
import
org.eclipse.jetty.http.HttpMethod
;
import
org.eclipse.jetty.http.HttpStatus
;
import
java.util.concurrent.TimeUnit
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
...
@@ -18,46 +16,66 @@ import java.util.concurrent.TimeUnit;
...
@@ -18,46 +16,66 @@ import java.util.concurrent.TimeUnit;
*/
*/
public
class
HttpJobHandler
extends
IJobHandler
{
public
class
HttpJobHandler
extends
IJobHandler
{
@Override
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
// valid
// request
if
(
param
==
null
||
param
.
trim
().
length
()==
0
)
{
HttpURLConnection
connection
=
null
;
XxlJobLogger
.
log
(
"URL Empty"
);
BufferedReader
bufferedReader
=
null
;
return
FAIL
;
try
{
}
// connection
URL
realUrl
=
new
URL
(
param
);
// httpclient
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
HttpClient
httpClient
=
null
;
try
{
// connection setting
httpClient
=
new
HttpClient
();
connection
.
setRequestMethod
(
"GET"
);
httpClient
.
setFollowRedirects
(
false
);
// Configure HttpClient, for example:
connection
.
setDoOutput
(
true
);
httpClient
.
start
();
// Start HttpClient
connection
.
setDoInput
(
true
);
connection
.
setUseCaches
(
false
);
// request
connection
.
setReadTimeout
(
5
*
1000
);
Request
request
=
httpClient
.
newRequest
(
param
);
connection
.
setConnectTimeout
(
3
*
1000
);
request
.
method
(
HttpMethod
.
GET
);
connection
.
setRequestProperty
(
"connection"
,
"Keep-Alive"
);
request
.
timeout
(
5000
,
TimeUnit
.
MILLISECONDS
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json;charset=UTF-8"
);
connection
.
setRequestProperty
(
"Accept-Charset"
,
"application/json;charset=UTF-8"
);
// invoke
ContentResponse
response
=
request
.
send
();
// do connection
if
(
response
.
getStatus
()
!=
HttpStatus
.
OK_200
)
{
connection
.
connect
();
XxlJobLogger
.
log
(
"Http StatusCode({}) Invalid."
,
response
.
getStatus
());
return
FAIL
;
//Map<String, List<String>> map = connection.getHeaderFields();
}
// valid StatusCode
String
responseMsg
=
response
.
getContentAsString
();
int
statusCode
=
connection
.
getResponseCode
();
XxlJobLogger
.
log
(
responseMsg
);
if
(
statusCode
!=
200
)
{
return
SUCCESS
;
throw
new
RuntimeException
(
"Http Request StatusCode("
+
statusCode
+
") Invalid."
);
}
catch
(
Exception
e
)
{
}
XxlJobLogger
.
log
(
e
);
return
FAIL
;
// result
}
finally
{
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
()));
if
(
httpClient
!=
null
)
{
StringBuilder
result
=
new
StringBuilder
();
httpClient
.
stop
();
String
line
;
}
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
}
result
.
append
(
line
);
}
}
String
responseMsg
=
result
.
toString
();
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
return
FAIL
;
}
finally
{
try
{
if
(
bufferedReader
!=
null
)
{
bufferedReader
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
disconnect
();
}
}
catch
(
Exception
e2
)
{
XxlJobLogger
.
log
(
e2
);
}
}
}
}
}
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/HttpJobHandler.java
View file @
573facc0
...
@@ -4,64 +4,82 @@ import com.xxl.job.core.biz.model.ReturnT;
...
@@ -4,64 +4,82 @@ import com.xxl.job.core.biz.model.ReturnT;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.handler.annotation.JobHandler
;
import
com.xxl.job.core.handler.annotation.JobHandler
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
org.eclipse.jetty.client.HttpClient
;
import
org.eclipse.jetty.client.api.ContentResponse
;
import
org.eclipse.jetty.client.api.Request
;
import
org.eclipse.jetty.http.HttpMethod
;
import
org.eclipse.jetty.http.HttpStatus
;
import
org.nutz.ioc.loader.annotation.IocBean
;
import
org.nutz.ioc.loader.annotation.IocBean
;
import
java.util.concurrent.TimeUnit
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
*
*
* @author xuxueli 2018-09-16 03:48:34
* @author xuxueli 2018-09-16 03:48:34
*/
*/
@JobHandler
(
value
=
"httpJobHandler"
)
@JobHandler
(
value
=
"httpJobHandler"
)
@IocBean
@IocBean
public
class
HttpJobHandler
extends
IJobHandler
{
public
class
HttpJobHandler
extends
IJobHandler
{
@Override
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
// valid
// request
if
(
param
==
null
||
param
.
trim
().
length
()==
0
)
{
HttpURLConnection
connection
=
null
;
XxlJobLogger
.
log
(
"URL Empty"
);
BufferedReader
bufferedReader
=
null
;
return
FAIL
;
try
{
}
// connection
URL
realUrl
=
new
URL
(
param
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// httpclient
// connection setting
HttpClient
httpClient
=
null
;
connection
.
setRequestMethod
(
"GET"
);
try
{
connection
.
setDoOutput
(
true
);
httpClient
=
new
HttpClient
();
connection
.
setDoInput
(
true
);
httpClient
.
setFollowRedirects
(
false
);
// Configure HttpClient, for example:
connection
.
setUseCaches
(
false
);
httpClient
.
start
();
// Start HttpClient
connection
.
setReadTimeout
(
5
*
1000
);
connection
.
setConnectTimeout
(
3
*
1000
);
connection
.
setRequestProperty
(
"connection"
,
"Keep-Alive"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json;charset=UTF-8"
);
connection
.
setRequestProperty
(
"Accept-Charset"
,
"application/json;charset=UTF-8"
);
// request
// do connection
Request
request
=
httpClient
.
newRequest
(
param
);
connection
.
connect
();
request
.
method
(
HttpMethod
.
GET
);
request
.
timeout
(
5000
,
TimeUnit
.
MILLISECONDS
);
// invoke
//Map<String, List<String>> map = connection.getHeaderFields();
ContentResponse
response
=
request
.
send
();
if
(
response
.
getStatus
()
!=
HttpStatus
.
OK_200
)
{
XxlJobLogger
.
log
(
"Http StatusCode({}) Invalid."
,
response
.
getStatus
());
return
FAIL
;
}
String
responseMsg
=
response
.
getContentAsString
();
// valid StatusCode
XxlJobLogger
.
log
(
responseMsg
);
int
statusCode
=
connection
.
getResponseCode
();
return
SUCCESS
;
if
(
statusCode
!=
200
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Http Request StatusCode("
+
statusCode
+
") Invalid."
);
XxlJobLogger
.
log
(
e
);
}
return
FAIL
;
}
finally
{
if
(
httpClient
!=
null
)
{
httpClient
.
stop
();
}
}
}
// result
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
()));
StringBuilder
result
=
new
StringBuilder
();
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
result
.
append
(
line
);
}
String
responseMsg
=
result
.
toString
();
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
return
FAIL
;
}
finally
{
try
{
if
(
bufferedReader
!=
null
)
{
bufferedReader
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
disconnect
();
}
}
catch
(
Exception
e2
)
{
XxlJobLogger
.
log
(
e2
);
}
}
}
}
}
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java
View file @
573facc0
...
@@ -4,64 +4,82 @@ import com.xxl.job.core.biz.model.ReturnT;
...
@@ -4,64 +4,82 @@ import com.xxl.job.core.biz.model.ReturnT;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.handler.annotation.JobHandler
;
import
com.xxl.job.core.handler.annotation.JobHandler
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
org.eclipse.jetty.client.HttpClient
;
import
org.eclipse.jetty.client.api.ContentResponse
;
import
org.eclipse.jetty.client.api.Request
;
import
org.eclipse.jetty.http.HttpMethod
;
import
org.eclipse.jetty.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
java.util.concurrent.TimeUnit
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
*
*
* @author xuxueli 2018-09-16 03:48:34
* @author xuxueli 2018-09-16 03:48:34
*/
*/
@JobHandler
(
value
=
"httpJobHandler"
)
@JobHandler
(
value
=
"httpJobHandler"
)
@Component
@Component
public
class
HttpJobHandler
extends
IJobHandler
{
public
class
HttpJobHandler
extends
IJobHandler
{
@Override
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
// valid
// request
if
(
param
==
null
||
param
.
trim
().
length
()==
0
)
{
HttpURLConnection
connection
=
null
;
XxlJobLogger
.
log
(
"URL Empty"
);
BufferedReader
bufferedReader
=
null
;
return
FAIL
;
try
{
}
// connection
URL
realUrl
=
new
URL
(
param
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// httpclient
// connection setting
HttpClient
httpClient
=
null
;
connection
.
setRequestMethod
(
"GET"
);
try
{
connection
.
setDoOutput
(
true
);
httpClient
=
new
HttpClient
();
connection
.
setDoInput
(
true
);
httpClient
.
setFollowRedirects
(
false
);
// Configure HttpClient, for example:
connection
.
setUseCaches
(
false
);
httpClient
.
start
();
// Start HttpClient
connection
.
setReadTimeout
(
5
*
1000
);
connection
.
setConnectTimeout
(
3
*
1000
);
connection
.
setRequestProperty
(
"connection"
,
"Keep-Alive"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json;charset=UTF-8"
);
connection
.
setRequestProperty
(
"Accept-Charset"
,
"application/json;charset=UTF-8"
);
// request
// do connection
Request
request
=
httpClient
.
newRequest
(
param
);
connection
.
connect
();
request
.
method
(
HttpMethod
.
GET
);
request
.
timeout
(
5000
,
TimeUnit
.
MILLISECONDS
);
// invoke
//Map<String, List<String>> map = connection.getHeaderFields();
ContentResponse
response
=
request
.
send
();
if
(
response
.
getStatus
()
!=
HttpStatus
.
OK_200
)
{
XxlJobLogger
.
log
(
"Http StatusCode({}) Invalid."
,
response
.
getStatus
());
return
FAIL
;
}
String
responseMsg
=
response
.
getContentAsString
();
// valid StatusCode
XxlJobLogger
.
log
(
responseMsg
);
int
statusCode
=
connection
.
getResponseCode
();
return
SUCCESS
;
if
(
statusCode
!=
200
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Http Request StatusCode("
+
statusCode
+
") Invalid."
);
XxlJobLogger
.
log
(
e
);
}
return
FAIL
;
}
finally
{
if
(
httpClient
!=
null
)
{
httpClient
.
stop
();
}
}
}
// result
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
()));
StringBuilder
result
=
new
StringBuilder
();
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
result
.
append
(
line
);
}
String
responseMsg
=
result
.
toString
();
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
return
FAIL
;
}
finally
{
try
{
if
(
bufferedReader
!=
null
)
{
bufferedReader
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
disconnect
();
}
}
catch
(
Exception
e2
)
{
XxlJobLogger
.
log
(
e2
);
}
}
}
}
}
xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/HttpJobHandler.java
View file @
573facc0
...
@@ -4,64 +4,82 @@ import com.xxl.job.core.biz.model.ReturnT;
...
@@ -4,64 +4,82 @@ import com.xxl.job.core.biz.model.ReturnT;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.handler.IJobHandler
;
import
com.xxl.job.core.handler.annotation.JobHandler
;
import
com.xxl.job.core.handler.annotation.JobHandler
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
org.eclipse.jetty.client.HttpClient
;
import
org.eclipse.jetty.client.api.ContentResponse
;
import
org.eclipse.jetty.client.api.Request
;
import
org.eclipse.jetty.http.HttpMethod
;
import
org.eclipse.jetty.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
java.util.concurrent.TimeUnit
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
/**
/**
* 跨平台Http任务
* 跨平台Http任务
*
*
* @author xuxueli 2018-09-16 03:48:34
* @author xuxueli 2018-09-16 03:48:34
*/
*/
@JobHandler
(
value
=
"httpJobHandler"
)
@JobHandler
(
value
=
"httpJobHandler"
)
@Component
@Component
public
class
HttpJobHandler
extends
IJobHandler
{
public
class
HttpJobHandler
extends
IJobHandler
{
@Override
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
// valid
// request
if
(
param
==
null
||
param
.
trim
().
length
()==
0
)
{
HttpURLConnection
connection
=
null
;
XxlJobLogger
.
log
(
"URL Empty"
);
BufferedReader
bufferedReader
=
null
;
return
FAIL
;
try
{
}
// connection
URL
realUrl
=
new
URL
(
param
);
connection
=
(
HttpURLConnection
)
realUrl
.
openConnection
();
// httpclient
// connection setting
HttpClient
httpClient
=
null
;
connection
.
setRequestMethod
(
"GET"
);
try
{
connection
.
setDoOutput
(
true
);
httpClient
=
new
HttpClient
();
connection
.
setDoInput
(
true
);
httpClient
.
setFollowRedirects
(
false
);
// Configure HttpClient, for example:
connection
.
setUseCaches
(
false
);
httpClient
.
start
();
// Start HttpClient
connection
.
setReadTimeout
(
5
*
1000
);
connection
.
setConnectTimeout
(
3
*
1000
);
connection
.
setRequestProperty
(
"connection"
,
"Keep-Alive"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json;charset=UTF-8"
);
connection
.
setRequestProperty
(
"Accept-Charset"
,
"application/json;charset=UTF-8"
);
// request
// do connection
Request
request
=
httpClient
.
newRequest
(
param
);
connection
.
connect
();
request
.
method
(
HttpMethod
.
GET
);
request
.
timeout
(
5000
,
TimeUnit
.
MILLISECONDS
);
// invoke
//Map<String, List<String>> map = connection.getHeaderFields();
ContentResponse
response
=
request
.
send
();
if
(
response
.
getStatus
()
!=
HttpStatus
.
OK_200
)
{
XxlJobLogger
.
log
(
"Http StatusCode({}) Invalid."
,
response
.
getStatus
());
return
FAIL
;
}
String
responseMsg
=
response
.
getContentAsString
();
// valid StatusCode
XxlJobLogger
.
log
(
responseMsg
);
int
statusCode
=
connection
.
getResponseCode
();
return
SUCCESS
;
if
(
statusCode
!=
200
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Http Request StatusCode("
+
statusCode
+
") Invalid."
);
XxlJobLogger
.
log
(
e
);
}
return
FAIL
;
}
finally
{
if
(
httpClient
!=
null
)
{
httpClient
.
stop
();
}
}
}
// result
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
()));
StringBuilder
result
=
new
StringBuilder
();
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
result
.
append
(
line
);
}
String
responseMsg
=
result
.
toString
();
XxlJobLogger
.
log
(
responseMsg
);
return
SUCCESS
;
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
return
FAIL
;
}
finally
{
try
{
if
(
bufferedReader
!=
null
)
{
bufferedReader
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
disconnect
();
}
}
catch
(
Exception
e2
)
{
XxlJobLogger
.
log
(
e2
);
}
}
}
}
}
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