学习如何使用 Spring 定时任务
定时任务能在指定时间、指定频率来执行一个方法。例如,定时清理缓存、备份数据。
0. 前言 你将创建什么? 你将使用 @Scheduled
注解实现每 5 秒将当前时间打印到日志的功能。
你需要什么知识?
一款你熟悉的 IDE(如,IntelliJ IDEA)
Java 1.8 或更高的版本
Maven 3.2 或更高的版本
1. 创建初始项目 1.1 创建 Spring Initializr 项目 你可以使用 IntelliJ IDEA 进行创建项目,也可以在 Spring Initializr 进行创建。
1.2 导入 maven 依赖 pom.xml
文件如下:
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.2.6.RELEASE</version > <relativePath /> </parent > <groupId > com.lxiaocode</groupId > <artifactId > scheduling-tasks</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > scheduling-tasks</name > <description > Demo project for Spring Boot</description > <properties > <java.version > 1.8</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > <exclusions > <exclusion > <groupId > org.junit.vintage</groupId > <artifactId > junit-vintage-engine</artifactId > </exclusion > </exclusions > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > </project >
2. 创建定时任务 你现在已经完成对项目的初始设置,可以开始创建你的定时任务了:
@Component public class ScheduledTasks { private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class ) ; private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss" ); @Scheduled (fixedRate = 5000 ) public void reportCurrentTime () { logger.info("The Time is now {}" , DATE_FORMAT.format(new Date())); } }
@Scheduled
注解将方法定义为定时任务。
该示例使用了 fixedRate
属性,它表示从方法开始调用到下一次方法开始调用的时间间隔。除此之外,还有 fixedDelay
属性,它表示从方法结束到下一次方法开始调用的时间间隔。你也可以使用 @Scheduled(cron="...")
设置更复杂的调度规则。
@Component
注解的作用是什么??
@Compontent
注解会将该类定义为应用程序中的组件。当应用启动时,Spring 会将它作为 bean 创建出来。
3. 开启定时任务 到此为止,你已经完成定时任务的创建,但是离使用定时任务还需要一步。我们需要到 Spring Boot 启动类中使用 @EnableScheduling
注解开启定时任务:
@SpringBootApplication @EnableScheduling public class SchedulingTasksApplication { public static void main (String[] args) { SpringApplication.run(SchedulingTasksApplication.class , args ) ; } }
@EnableScheduling
注解确保创建后台任务执行者对象,没有它定时任务将不会启动。
4. 测试定时任务 启动 Spring Boot 项目,你将在控制台看到不断有信息打印出来:
2020-04-20 15:19:53.081 INFO 17304 --- [ scheduling-1] c.l.schedulingtasks.ScheduledTasks : The Time is now 15:19:53 2020-04-20 15:19:58.082 INFO 17304 --- [ scheduling-1] c.l.schedulingtasks.ScheduledTasks : The Time is now 15:19:58 2020-04-20 15:20:03.081 INFO 17304 --- [ scheduling-1] c.l.schedulingtasks.ScheduledTasks : The Time is now 15:20:03
GitHub 项目源码
参考文献:Spring | Guides 。