|
Spring Boot定时任务实现与控制
springboot制作,每隔30分钟执行一次任务,本次任务没有执行完,下次任务不允许执行;使用线程执行,任务执行时,在 Application启动之后执行, 要在Spring Boot中实现每隔30分钟执行一次任务,且本次任务未完成时跳过下次执行,并在应用启动后立即运行,可以按照以下步骤操作:
### 1. 添加Spring Boot依赖
确保`pom.xml`中包含Spring Boot Starter和Scheduling依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
### 2. 配置定时任务线程池
创建配置类以设置单线程调度器,确保任务按顺序触发:
```java
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executors;
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// 使用单线程调度器,防止并发执行
taskRegistrar.setScheduler(Executors.newSingleThreadScheduledExecutor());
}
}
```
### 3. 实现定时任务逻辑
创建服务类处理任务执行,使用`AtomicBoolean`控制任务状态:
```java
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
@Service
public class ScheduledTaskService {
private final AtomicBoolean isRunning = new AtomicBoolean(false);
private final ExecutorService taskExecutor = Executors.newSingleThreadExecutor();
// 应用启动后立即执行,之后每隔30分钟触发
@Scheduled(fixedRate = 30 * 60 * 1000, initialDelay = 0)
public void triggerTask() {
if (isRunning.compareAndSet(false, true)) {
taskExecutor.submit(this::executeTask);
} else {
System.out.println("任务正在执行,跳过触发: " + LocalDateTime.now());
}
}
private void executeTask() {
try {
System.out.println("任务开始执行: " + LocalDateTime.now());
// 模拟任务耗时(例如35分钟)
Thread.sleep(35 * 60 * 1000);
System.out.println("任务结束: " + LocalDateTime.now());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
isRunning.set(false); // 确保状态重置
}
}
// 应用关闭时清理线程池
@PreDestroy
public void shutdown() {
taskExecutor.shutdownNow();
}
}
```
### 4. 启用定时任务
在主应用类上添加`@EnableScheduling`注解:
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
### 方案说明
- **单线程调度器**:确保任务触发严格按照时间间隔,避免并发触发。
- **AtomicBoolean状态控制**:通过原子变量保证任务状态检查的线程安全,防止重复执行。
- **分离任务执行线程**:使用独立线程池执行实际任务,避免阻塞调度线程,确保定时触发准确性。
- **应用启动后立即执行**:通过`initialDelay = 0`实现首次任务立即执行。
### 测试验证
启动应用后,控制台将输出:
```
任务开始执行: [当前时间]
任务结束: [当前时间+35分钟]
任务正在执行,跳过触发: [30分钟后的时间点]
任务开始执行: [60分钟后的时间点]
...
```
当任务执行时间超过30分钟时,后续触发会被跳过,直到当前任务完成后的下一个触发点。
该方案确保了任务严格按30分钟间隔检查执行状态,满足任务未完成时跳过下次执行的需求,同时保证应用启动后立即执行。
|
|