xxl-job定时任务集成2.4+
最后更新于:2022-04-02 07:10:20
# xxl-job定时任务集成2.4+
> JeecgBoot 分布式定时任务集成了xxl-job
> 本文以jeecg-demo模块为例介绍如何集成定时任务
[TOC]
## 第一步 引入xxl-job定时任务依赖
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/b4/c9/b4c9c5c313bce99c4a78ba0f666fff93_1197x657.png)
~~~
org.jeecgframework.boot
jeecg-boot-starter-job
~~~
## 第二步 编写定时任务客户端handler
~~~
@Component
@Slf4j
public class DemoJobHandler {
@XxlJob(value = "demoJob")
public ReturnT demoJobHandler(String params) {
log.info("我是定时任务,我执行了...............................");
return ReturnT.SUCCESS;
}
}
~~~
## 第三步 修改 jeecg-dev.yaml 配置文件
1. 开启 xxljob
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/02/e8/02e8dba957982b308958d6e3f63ce7b4_683x227.png)
2. 修改 xxljob-admin的服务连接配置
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/65/8b/658b1d0c4d775a4b23dce91f4509da8d_900x333.png)
## 第四步 启动服务端 xxl-job-admin
### #1 初始化数据库
执行脚本:[https://github.com/xuxueli/xxl-job/blob/2.2.0/doc/db/tables\_xxl\_job.sql](https://github.com/xuxueli/xxl-job/blob/2.2.0/doc/db/tables_xxl_job.sql)
### #2 启动xxl-job-admin
修改数据连接等配置
找到类 com.xxl.job.admin.XxlJobAdminApplication, 右键运行
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/e1/c6/e1c699b0a1ab6f708c0ef6c24fb141ed_1141x514.png)
### 3 访问管理界面
* 浏览器访问
[http://localhost:9080/xxl-job-admin](http://localhost:9080/xxl-job-admin)
默认登录账号 :admin/123456
* 新增执行器(注意,需要手工创建)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/e0/60/e060c895c7d372f170ca7271ba922459_1757x650.png)
* 新增定时任务
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/ae/0b/ae0bd6357ae0007ff86103fcd7c09f88_1735x720.png)
*****
* 测试定时任务(前提微服务各个模块和定时任务服务端已启动)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/d4/c4/d4c4aa1c5ace4b5f9b6af31c48284538_1730x559.png)
执行结果如下所示
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/75/b8/75b8dd703942d9101952e9ba6a78b0e7_2248x423.png)
### 4 如何指定不同我服务模块的执行器端口
xxljob执行器端口当不指定端口的时候采用自动生成模式,也可以为不同执行器指定端口
1.查看nacos jeecg-dev.yaml中是否定义xxljob.port端口号如有定义需要注释掉
2.检查配置文件中设置端口号的方法是否放开如下图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/0b/38/0b38fb05fde6b8f56bfd157c5b96dd04_1756x487.png)
3.在具体的微服务模块中引入jeecg-boot-starter-job模块并在对应模块的application.yml文件中定义执行器的端口号如下配置
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/8d/72/8d72b31e27aa14898ebafb63cc8306d0_450x326.png)
```
jeecg:
xxljob:
ip: 127.0.0.1
port: 30058
```
### #参考文献
* [xxl-job 官方文档](https://www.xuxueli.com/xxl-job/#%E3%80%8A%E5%88%86%E5%B8%83%E5%BC%8F%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E5%B9%B3%E5%8F%B0XXL-JOB%E3%80%8B)
* 关闭quartz定时任务
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/25/18/2518006e93fd1fa8313988fb86c9e52b_818x495.png)
';