1、下载
https://gitee.com/xuxueli0323/xxl-job
2、导入数据库
doc/db/tables_xxl_job.sql
3、启动注册中心
启动后可以看到:
4、springboot中使用
4.1 引入xxl-job-core
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
4.2 配置application.yaml
xxl:
job:
admin:
addresses: http://127.0.0.1:8282/xxl-job-admin
executor:
appname: t1dev
ip:
port: 9010
logpath: /data/applogs/xxl-job/xxx
logretentiondays: -1
accessToken: default_token
4.3 配置config
package com.timi.t1.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@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() {
System.out.println(">>>>>>>>>>> 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;
}
}
4.4 新增执行器
注册成功
4.5 在t1dev执行器下新增任务
package com.timi.t1.xxljob;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
@Component
public class StoryJobHandler {
@XxlJob("tapdSyncJobHandler")
public void tapdSyncJobHandler(){
System.out.println("tapdSyncJobHandler start");
System.out.println("tapdSyncJobHandler end");
}
}
4.6 执行一次,可看到日志信息