源码地址:GitHub - xuxueli/xxl-job: A distributed task scheduling framework.(分布式任务调度平台XXL-JOB)(可以看看,学习)
项目结构
doc:一些文档 数据库
xxl-job-admin:调度中心,启动之后可以访问定时任务平台
xxl-job-core:公共的依赖
xxl-job-executor-samples:执行器
在日常开发中,我们项目中不需要将xxl-job的所有模块拿过来(公司都有调度中心),对于xxx-job-core 这是个公共的依赖,我们只要在xml文件中引入这个依赖即可,一般只需要创建一个执行器
可以参考官网的执行器结构
1.新建一个执行器模块xxl-job(一个命名)
java 和 jdk 的版本后续记得修改
2.引入核心 xxl-job-core 依赖(还有SpringBoot相关依赖等)
<!-- xxl job -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
3.创建XxlJobConfig
package com.***.common.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class XxlJobConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(XxlJobConfig.class);
@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.executor.logPath}")
private String logPath;
@Value("${xxl.job.executor.logRetentionDays}")
private int logRetentionDays;
@Value("${xxl.job.executor.address}")
private String address;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
LOGGER.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
xxlJobSpringExecutor.setAddress(address);
return xxlJobSpringExecutor;
}
}
4.创建jobhandler(在这创建你的定时任务)
package com.***.jobhandler;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class TaskDemoHandler {
private static final Logger logger = LoggerFactory.getLogger(TaskDemoHandler.class);
@XxlJob("caiTask")
public ReturnT<String> myFirstTask(String param) throws Exception {
param = XxlJobHelper.getJobParam();
logger.info("定时任务开始执行");
System.out.println(param);
logger.info("定时任务执行结束");
return ReturnT.SUCCESS;
}
}
@XxlJob("caiTask") 这是你的定时任务名称
5.在该模块 resources文件下创建 application.properties
# 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=default_token
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### 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
server.port 该定时任务模块的端口号
xxl.job.admin.addresses 调度中心访问地址
6.在该模块 resources文件下创建 logback.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1 seconds">
<contextName>logback</contextName>
<property name="log.path" value="/data/applogs/xxl-job/xxl-job-executor-sample-springboot.log"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
7.在调度中心,新增执行器
执行器的名称,手动录入,机器地址 ip:xxl.job.executor.port(配置文件)例如:http://192.168.111.202:9999/
8.在任务管理,新增任务
JobHandler 是你代码里的任务名称 @XxlJob("caiTask") caiTask
到这里就结束了,可以在调度中心测试一下