我希望使用xxl-job完成定时任务但我没有使用过我具体该怎么引入到我的boot项目并使用
- 引入依赖
在 pom.xml 文件中添加以下依赖:
<dependency>
<groupId>com.xxl-job</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${xxl.job.version}</version>
</dependency>
- 配置参数
在 application.yml 或 application.properties 文件中添加如下配置:
xxl:
job:
admin:
addresses: http://localhost:8080/xxl-job-admin
accessToken:
executor:
appname: xxl-job-executor-sample
address: http://localhost:9999/xxl-job-executor
ip:
port: 9999
logpath: /data/applogs/xxl-job/jobhandler
logretentiondays: 30
- 编写定时任务
在你的项目中编写需要定时执行的任务,例如:
@Component
public class MyJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
// TODO: 定时任务执行的具体逻辑
return ReturnT.SUCCESS;
}
}
- 注册任务
在 Spring Boot 的配置类中注册任务,例如:
@Configuration
public class XxlJobConfig {
@Autowired
private MyJobHandler myJobHandler;
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
executor.setAdminAddresses("http://localhost:8080/xxl-job-admin");
executor.setAppname("xxl-job-executor-sample");
executor.setAddress("http://localhost:9999/xxl-job-executor");
executor.setIp("");
executor.setPort(9999);
executor.setAccessToken("");
executor.setLogPath("/data/applogs/xxl-job/jobhandler");
executor.setLogRetentionDays(30);
// 注册任务处理器
Map<String, IJobHandler> handlerMap = new HashMap<>();
handlerMap.put("myJobHandler", myJobHandler);
executor.setHandlerMap(handlerMap);
return executor;
}
}
- 启动项目
启动 Spring Boot 项目,并在 xxl-job-admin 中配置任务即可
原文地址: https://www.cveoy.top/t/topic/eFVF 著作权归作者所有。请勿转载和采集!