Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
X
XXL-JOB
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
靳帅
XXL-JOB
Commits
c07d366a
提交
c07d366a
authored
12月 12, 2019
作者:
xuxueli
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update document
上级
9fed7b9c
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
117 行增加
和
143 行删除
+117
-143
CommandJobHandler.java
...xl/job/executor/service/jobhandler/CommandJobHandler.java
+0
-58
DemoJobHandler.java
...m/xxl/job/executor/service/jobhandler/DemoJobHandler.java
+0
-38
SampleXxlJob.java
...com/xxl/job/executor/service/jobhandler/SampleXxlJob.java
+117
-9
ShardingJobHandler.java
...l/job/executor/service/jobhandler/ShardingJobHandler.java
+0
-38
没有找到文件。
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/CommandJobHandler.java
deleted
100644 → 0
浏览文件 @
9fed7b9c
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.springframework.stereotype.Component
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
/**
* 命令行任务
*
* @author xuxueli 2018-09-16 03:48:34
*/
@JobHandler
(
value
=
"commandJobHandler"
)
@Component
public
class
CommandJobHandler
extends
IJobHandler
{
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
String
command
=
param
;
int
exitValue
=
-
1
;
BufferedReader
bufferedReader
=
null
;
try
{
// command process
Process
process
=
Runtime
.
getRuntime
().
exec
(
command
);
BufferedInputStream
bufferedInputStream
=
new
BufferedInputStream
(
process
.
getInputStream
());
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
bufferedInputStream
));
// command log
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
XxlJobLogger
.
log
(
line
);
}
// command exit
process
.
waitFor
();
exitValue
=
process
.
exitValue
();
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
}
finally
{
if
(
bufferedReader
!=
null
)
{
bufferedReader
.
close
();
}
}
if
(
exitValue
==
0
)
{
return
IJobHandler
.
SUCCESS
;
}
else
{
return
new
ReturnT
<
String
>(
IJobHandler
.
FAIL
.
getCode
(),
"command exit value("
+
exitValue
+
") is failed"
);
}
}
}
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java
deleted
100644 → 0
浏览文件 @
9fed7b9c
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.springframework.stereotype.Component
;
import
java.util.concurrent.TimeUnit
;
/**
* 任务Handler示例(Bean模式)
*
* 开发步骤:
* 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”;
* 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例;
* 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
*
* @author xuxueli 2015-12-19 19:43:36
*/
@JobHandler
(
value
=
"demoJobHandler"
)
@Component
public
class
DemoJobHandler
extends
IJobHandler
{
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
XxlJobLogger
.
log
(
"XXL-JOB, Hello World."
);
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
XxlJobLogger
.
log
(
"beat at:"
+
i
);
TimeUnit
.
SECONDS
.
sleep
(
2
);
}
return
SUCCESS
;
}
}
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-spring/src/main/java/com/xxl/job/executor/service/jobhandler/
SampleXxlJob
.java
浏览文件 @
c07d366a
...
...
@@ -2,26 +2,118 @@ 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.handler.annotation.
XxlJob
;
import
com.xxl.job.core.log.XxlJobLogger
;
import
com.xxl.job.core.util.ShardingUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.util.concurrent.TimeUnit
;
/**
*
跨平台Http任务
*
XxlJob开发示例(Bean模式)
*
* @author xuxueli 2018-09-16 03:48:34
* 开发步骤:
* 1、在Spring Bean实例中,开发Job方法,方式格式要求为 "public ReturnT<String> execute(String param)"
* 2、为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 3、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
*
* @author xuxueli 2019-12-11 21:52:51
*/
@JobHandler
(
value
=
"httpJobHandler"
)
@Component
public
class
HttpJobHandler
extends
IJobHandler
{
public
class
SampleXxlJob
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
SampleXxlJob
.
class
);
/**
* 1、简单任务示例(Bean模式)
*/
@XxlJob
(
"demoJobHandler"
)
public
ReturnT
<
String
>
demoJobHandler
(
String
param
)
throws
Exception
{
XxlJobLogger
.
log
(
"XXL-JOB, Hello World."
);
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
XxlJobLogger
.
log
(
"beat at:"
+
i
);
TimeUnit
.
SECONDS
.
sleep
(
2
);
}
return
ReturnT
.
SUCCESS
;
}
/**
* 2、分片广播任务
*/
@XxlJob
(
"shardingJobHandler"
)
public
ReturnT
<
String
>
shardingJobHandler
(
String
param
)
throws
Exception
{
// 分片参数
ShardingUtil
.
ShardingVO
shardingVO
=
ShardingUtil
.
getShardingVo
();
XxlJobLogger
.
log
(
"分片参数:当前分片序号 = {}, 总分片数 = {}"
,
shardingVO
.
getIndex
(),
shardingVO
.
getTotal
());
// 业务逻辑
for
(
int
i
=
0
;
i
<
shardingVO
.
getTotal
();
i
++)
{
if
(
i
==
shardingVO
.
getIndex
())
{
XxlJobLogger
.
log
(
"第 {} 片, 命中分片开始处理"
,
i
);
}
else
{
XxlJobLogger
.
log
(
"第 {} 片, 忽略"
,
i
);
}
}
return
ReturnT
.
SUCCESS
;
}
/**
* 3、命令行任务
*/
@XxlJob
(
"commandJobHandler"
)
public
ReturnT
<
String
>
commandJobHandler
(
String
param
)
throws
Exception
{
String
command
=
param
;
int
exitValue
=
-
1
;
BufferedReader
bufferedReader
=
null
;
try
{
// command process
Process
process
=
Runtime
.
getRuntime
().
exec
(
command
);
BufferedInputStream
bufferedInputStream
=
new
BufferedInputStream
(
process
.
getInputStream
());
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
bufferedInputStream
));
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
// command log
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
XxlJobLogger
.
log
(
line
);
}
// command exit
process
.
waitFor
();
exitValue
=
process
.
exitValue
();
}
catch
(
Exception
e
)
{
XxlJobLogger
.
log
(
e
);
}
finally
{
if
(
bufferedReader
!=
null
)
{
bufferedReader
.
close
();
}
}
if
(
exitValue
==
0
)
{
return
IJobHandler
.
SUCCESS
;
}
else
{
return
new
ReturnT
<
String
>(
IJobHandler
.
FAIL
.
getCode
(),
"command exit value("
+
exitValue
+
") is failed"
);
}
}
/**
* 4、跨平台Http任务
*/
@XxlJob
(
"httpJobHandler"
)
public
ReturnT
<
String
>
httpJobHandler
(
String
param
)
throws
Exception
{
// request
HttpURLConnection
connection
=
null
;
...
...
@@ -63,10 +155,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
)
{
...
...
@@ -82,4 +174,20 @@ public class HttpJobHandler extends IJobHandler {
}
/**
* 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
*/
@XxlJob
(
value
=
"demoJobHandler2"
,
init
=
"init"
,
destroy
=
"destroy"
)
public
ReturnT
<
String
>
demoJobHandler2
(
String
param
)
throws
Exception
{
XxlJobLogger
.
log
(
"XXL-JOB, Hello World."
);
return
ReturnT
.
SUCCESS
;
}
public
void
init
(){
logger
.
info
(
"init"
);
}
public
void
destroy
(){
logger
.
info
(
"destory"
);
}
}
xxl-job-executor-samples/xxl-job-executor-sample-spring/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java
deleted
100644 → 0
浏览文件 @
9fed7b9c
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
com.xxl.job.core.util.ShardingUtil
;
import
org.springframework.stereotype.Service
;
/**
* 分片广播任务
*
* @author xuxueli 2017-07-25 20:56:50
*/
@JobHandler
(
value
=
"shardingJobHandler"
)
@Service
public
class
ShardingJobHandler
extends
IJobHandler
{
@Override
public
ReturnT
<
String
>
execute
(
String
param
)
throws
Exception
{
// 分片参数
ShardingUtil
.
ShardingVO
shardingVO
=
ShardingUtil
.
getShardingVo
();
XxlJobLogger
.
log
(
"分片参数:当前分片序号 = {}, 总分片数 = {}"
,
shardingVO
.
getIndex
(),
shardingVO
.
getTotal
());
// 业务逻辑
for
(
int
i
=
0
;
i
<
shardingVO
.
getTotal
();
i
++)
{
if
(
i
==
shardingVO
.
getIndex
())
{
XxlJobLogger
.
log
(
"第 {} 片, 命中分片开始处理"
,
i
);
}
else
{
XxlJobLogger
.
log
(
"第 {} 片, 忽略"
,
i
);
}
}
return
SUCCESS
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论