在Spring框架中,使用Spring Schedule可以很方便地创建定时任务。以下是一个使用Spring Schedule完成定时任务的DEMO:
- 引入Spring Boot依赖:在
pom.xml
文件中添加Spring Boot Starter依赖,这会自动包含Spring Scheduling。
<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>
- 启用定时任务支持:在Spring Boot应用程序主类上添加
@EnableScheduling
注解。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class ScheduledTasksApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasksApplication.class, args);
}
}
- 创建定时任务:创建一个带有
@Scheduled
注解的方法来定义任务。
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class ScheduledTasks {
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void scheduleTaskWithFixedRate() {
System.out.println("固定时间任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
}
@Scheduled(fixedDelay = 5000)
public void scheduleTaskWithFixedDelay() {
System.out.println("固定延迟任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
}
@Scheduled(cron = "0 * * * * ?")
public void scheduleTaskWithCronExpression() {
System.out.println("计划任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
}
}
在上面的代码中:
@Scheduled(fixedRate = 5000)
:每5秒执行一次任务,不管上一次任务是否完成。@Scheduled(fixedDelay = 5000)
:在上一次任务完成后等待5秒再执行下一次任务。@Scheduled(cron = "0 * * * * ?")
:使用Cron表达式在每分钟的第0秒执行任务。
完整的示例代码如下:
pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-scheduled-tasks</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ScheduledTasksApplication.java
:
package com.example.scheduledtasks;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class ScheduledTasksApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasksApplication.class, args);
}
}
ScheduledTasks.java
:
package com.example.scheduledtasks;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class ScheduledTasks {
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void scheduleTaskWithFixedRate() {
System.out.println("固定时间任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
}
@Scheduled(fixedDelay = 5000)
public void scheduleTaskWithFixedDelay() {
System.out.println("固定延迟任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
}
@Scheduled(cron = "0 * * * * ?")
public void scheduleTaskWithCronExpression() {
System.out.println("计划任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
}
}
运行应用程序:
运行ScheduledTasksApplication
主类,控制台输出定时任务的执行时间:
固定时间任务 :: 执行时间 - 2024-05-20 12:00:00
固定延迟任务 :: 执行时间 - 2024-05-20 12:00:05
计划任务 :: 执行时间 - 2024-05-20 12:01:00
...
这样完成了一个包含定时任务的程序。