# xxl-job
XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。
# springboot自带的定时任务;
# @EnableScheduling注解开启定时任务
# @Scheduled注解执行任务
参数cron [秒][分][时][日][月][周][年]
fixedDelay 上一次执行完毕时间点之后多长时间再执行
fixedDelayString 与fixedDelay相同,只是使用字符串的形式
zone cron表达式会基于该时区
# 原生定时任务框架先天缺陷
- 不支持分片任务
- 不支持生命周期统一管理
- 不支持集群
- 不支持失败重试
- 不支持动态调整
- 无报警机制
- 任务数据统计难以统计
# xxl-job整合
# 下载
git clone https://github.com/xuxueli/xxl-job.git
xxl-job-admin 是任务调度中心项目。生产环境中,我们可以直接使用其源码,打包部署。
xxl-job-executor-samples 是执行器的案例项目,实际开发中需要整合其配置文件到我们自己的项目中使用。
xxl-job-core 为核心项目,调度中心和执行器项目都需要依赖它。
# 导入数据
我们在 xxl-job\doc\ db 目录,里边有个名为 tables_xxl_job.sql 文件。
我们需要在数据库实例中创建名为 xxl_job 的数据库,将上边的 sql 文件在此数据库中执行。
# 启动调度中心(xxl-job-admin)
### web
server.port=8080
server.servlet.context-path=/xxl-job-admin
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=tiger
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
页面:http://localhost:8083/xxl-job-admin 用户名:admin 密码:123456
# 任务执行器,spring boot集成
- pom.xml maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0-SNAPSHOT</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 配置application.properties文件和logback.xml文件
# web port
server.port=8081
# no web
#spring.main.web-environment=false
# log config
logging.config=classpath:logback.xml
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=
### xxl-job executor appname
xxl.job.executor.appname=job-demo
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=9999
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 配置XXL-JOB执行器组件
@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 编写定时任务器
- 定时器类中,方法必须有 String 类型的入参,返回值必须是 ReturnT
,否则启动项目会报错。最后需要使用 @XxlJob 注解,否则调度中心无法调度该任务。
@Component
public class MyTask {
/**
* @XxlJob 中 value 值唯一即可
* @param param
* @return
*/
@XxlJob("firstTask")
public ReturnT<String> firstTask(String param) {
XxlJobHelper.log("start firstTask param = {}", param);
// 业务代码,此处作为演示只打印 hello xxl-job
System.out.println("hello xxl-job");
XxlJobHelper.log("end firstTask");
return ReturnT.SUCCESS;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 配置执行管理器
# 配置任务中心
- 我们设置每 1分钟执行一次任务
# 执行器项目的控制台打印了 “hello xxl-job”,通过操作调度中心,成功启动和执行定时器任务。
# 日志XxlJobHelper.log()
- 可以在调度日志中查看
# 相关资料
XXL-JOB 基础教程 (opens new window)
全量分析 阅读量: