springboot集成xxl-job定时任务
要在Spring Boot中集成XXL-JOB定时任务,你需要按照以下步骤操作:
- 在pom.xml中添加XXL-JOB的依赖:
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>你的版本号</version>
</dependency>
- 在application.properties或application.yml中配置XXL-JOB:
# xxl-job admin address
xxl.job.admin.addresses=你的XXL-JOB-ADMIN地址
# xxl-job executor address
xxl.job.executor.ip=自动获取可以为空
xxl.job.executor.port=执行器端口
xxl.job.accessToken=访问令牌
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
xxl.job.executor.logretentiondays=30
# 执行器AppName
xxl.job.executor.appname=你的应用名称
# 执行器注册的名字
xxl.job.executor.executorHandler=jobHandler
# 执行器在注册中心的分组
xxl.job.executor.jobgroup=默认分组
# 执行器的执行权重
xxl.job.executor.weight=1
- 创建定时任务的Handler:
@Component
public class SampleXxlJob {
@XxlJob("demoJobHandler")
public void execute() throws Exception {
// 任务逻辑
XxlJobHelper.log("这是一个XXL-JOB定时任务的示例");
// 任务执行完毕向调度中心返回执行信息
XxlJobHelper.success();
}
}
- 配置执行器:
@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 XxlJobExecutor xxlJobExecutor() {
XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
// 管理地址
xxlJobExecutor.setAdminAddresses(adminAddresses);
// 执行器配置
xxlJobExecutor.setAppName(appName);
xxlJobExecutor.setIp(ip);
xxlJobExecutor.setPort(port);
xxlJobExecutor.setAccessToken(accessToken);
// 日志配置
xxlJobExecutor.setLogPath(logPath);
xxl
评论已关闭