xxl-job整合记录备用
一, xxl-job快速搭建
1 拉取
从 https://github.com/xuxueli/xxl-job 拉取代码
2 创建数据库
将拉取下来的代码中, .doc/db/xxl_job_info.sql执行并创建xxl-job库
3 修改配置文件
修改 xxl-job-admin模块 下 application.properties 中的 ### xxl-job, datasource 部分修改为自己的库信息
至此完成搭建
访问页面为 http://{job搭建ip}:{job-server:port}/xxl-job-admin/ 建议在docker中搭建
二, xxl-job定时任务创建
1 引入pom
1 2 3 4 5
| <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.1</version> </dependency>
|
2 创建配置文件及配置类
1 2 3 4 5 6 7 8 9 10 11
| xxl: job: admin: addresses: http://{job搭建ip}:{job-server:port}/xxl-job-admin/ executor: appname: xxl-job-executor-sample ip: port: 9998 logpath: /data/applogs/xxl-job-test/jobhandler logretentiondays: 30 accessToken: '432e62f3b488bc861d91b0e274e850cc'
|
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 47
| import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Slf4j @Configuration public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}") private String adminAddresses;
@Value("${xxl.job.executor.appname}") private String appName;
@Value("${xxl.job.executor.ip}") private String ip;
@Value("${xxl.job.executor.port}") private int port;
@Value("${xxl.job.accessToken}") private String accessToken;
@Value("${xxl.job.executor.logpath}") private String logPath;
@Value("${xxl.job.executor.logretentiondays}") private int logRetentionDays;
@Bean public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init."); XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); xxlJobSpringExecutor.setAdminAddresses(adminAddresses); xxlJobSpringExecutor.setAppname(appName); xxlJobSpringExecutor.setIp(ip); xxlJobSpringExecutor.setPort(port); xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); return xxlJobSpringExecutor; } }
|
3 定时方法创建
代码中: 加入 @XxlJob(“名”) 即可注册定时方法
1 2 3 4 5 6 7 8
| @Component public class TaskJob {
@XxlJob("postVideo2CHXAsync") public void postVideo2CHXAsync() { } }
|
可视化平台配置该定时任务周期
1,任务管理 中 选择对应执行器 新增
运行模式 选择 BEAN , JobHandler 填入对应注册名称, 其他默认,即可