xxl-Job详解(整合springboot)超详细
XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。
整合SpringBoot步骤:
- 添加Maven依赖
<!-- xxl-job-core -->
<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=http://localhost:8080/xxl-job-admin
# xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
# xxl-job executor ip
xxl.job.executor.ip=
# xxl-job executor port
xxl.job.executor.port=9999
# xxl-job executor logpath
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
# xxl-job executor logretentiondays
xxl.job.executor.logretentiondays=
- 创建配置类
@Configuration
public class XxlJobConfig {
private static 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;
@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.setAccessToken(null);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
@Bean
public CommandLineRunner xxlJobExecutorRunner(XxlJobSpringExecutor xxlJobSpringExecutor) {
return (args) -> xxlJobSpringExecutor.start();
}
}
- 创建任务处理器
@Component
public class SampleXxlJob {
private static final Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
@XxlJob("demoJobHandler")
public void
评论已关闭