springboot整合xxljob
时间: 2023-09-23 07:03:24 浏览: 111
XXL-Job是一个分布式任务调度平台,Spring Boot是一个快速开发Web应用的框架。将XXL-Job集成到Spring Boot项目中,可以方便地实现分布式任务调度。下面是整合步骤:
1. 在pom.xml文件中添加依赖:
```xml
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
```
2. 在application.properties文件中添加XXL-Job的配置:
```properties
# xxl-job admin address list, such as "http://address/admin"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
# xxl-job executor配置
xxl.job.executor.appname=myJob # 执行器AppName
xxl.job.executor.ip=127.0.0.1 # 执行器IP
xxl.job.executor.port=9999 # 执行器端口
xxl.job.accessToken= # 执行器通讯TOKEN,非必填
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler # 执行器日志路径
xxl.job.executor.logretentiondays=7 # 执行器日志保存天数,-1表示永久保存
```
3. 创建XXL-Job的执行器配置类:
```java
@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(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
executor.setAdminAddresses(adminAddresses);
executor.setAppname(appName);
executor.setIp(ip);
executor.setPort(port);
executor.setAccessToken(accessToken);
executor.setLogPath(logPath);
executor.setLogRetentionDays(logRetentionDays);
return executor;
}
}
```
4. 创建一个任务处理类:
```java
@Component
public class DemoJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
// 任务执行逻辑
System.out.println("XXL-Job demo job handler executed.");
return ReturnT.SUCCESS;
}
}
```
5. 在XXL-Job的管理后台创建一个任务,并配置触发器,然后启动任务即可。
至此,Spring Boot项目已经成功集成了XXL-Job分布式任务调度平台。
阅读全文