SpringBoot+xxl-job 代码
时间: 2024-02-01 08:01:59 浏览: 84
以下是一个简单的 Spring Boot 集成 xxl-job 的示例代码:
1. 在 pom.xml 中添加依赖:
```xml
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${xxl.job.version}</version>
</dependency>
```
2. 在 application.properties 中添加配置:
```properties
# xxl-job admin 地址
xxl.job.admin.addresses=http://localhost:8080/xxl-job-admin
# 执行器名称
xxl.job.executor.appname=myJobExecutor
# 执行器 IP
xxl.job.executor.ip=127.0.0.1
# 执行器端口
xxl.job.executor.port=9999
# 执行器日志路径
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
# 执行器日志保留天数
xxl.job.executor.logretentiondays=30
```
3. 创建一个 JobHandler 类:
```java
@Component
public class MyJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
// 业务逻辑
return ReturnT.SUCCESS;
}
}
```
4. 在启动类中添加 xxl-job 的配置和初始化:
```java
@SpringBootApplication
@EnableXxlJob
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
executor.setAdminAddresses("http://localhost:8080/xxl-job-admin");
executor.setAppName("myJobExecutor");
executor.setIp("127.0.0.1");
executor.setPort(9999);
executor.setLogPath("/data/applogs/xxl-job/jobhandler");
executor.setLogRetentionDays(30);
return executor;
}
}
```
5. 在 xxl-job-admin 中创建一个任务,并指定执行器为 myJobExecutor,JobHandler 类为 MyJobHandler。
以上就是一个简单的 Spring Boot 集成 xxl-job 的示例代码,你可以根据自己的需求进行修改和扩展。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)