在本文中,您将学习如何在 Spring Boot 中使用 @Scheduled
注解调度任务。您还将学习如何使用自定义线程池来执行所有计划任务。
@Scheduled
注释只需要配置一些参数,最终的执行由spring Boot内置处理。
Spring Boot 内部使用 TaskScheduler
接口来调度带注释的方法并执行。
本文的目的是构建一个简单的项目来学习任务调度相关知识。
让我们使用 Spring Boot CLI 创建项目。启动您的终端并输入以下命令以生成项目 -
$ spring init --name=scheduler-demo scheduler-demo
或者,您可以使用 Spring Initializer Web 应用程序生成项目。只需转到 http://start.spring.io/,将 Artifact 的值输入为“scheduler-demo”,然后单击 Generate 以生成并下载项目。
生成项目后,将其导入您喜欢的 IDE。项目的目录结构如下所示 -
您只需将 @EnableScheduling
注释添加到主应用程序类或配置类之一即可启用调度。
打开 SchedulerDemoApplication.java
并像这样添加 @EnableScheduling
注释 -
package com.example.schedulerdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SchedulerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulerDemoApplication.class, args);
}
}
使用 Spring Boot @Scheduled
注解调度任务就像使用注释一样简单,并提供了很少的参数来决定任务运行的时间。
在 com.example.schedulerdemo
包中创建一个名为 ScheduledTasks
的新类,包含以下内容 -
package com.example.schedulerdemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;
@Component
public class ScheduledTasks {
private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
public void scheduleTaskWithFixedRate() {}
public void scheduleTaskWithFixedDelay() {}
public void scheduleTaskWithInitialDelay() {}
public void scheduleTaskWithCronExpression() {}
}
该类包含四个空方法。我们将一一查看所有方法的实现。
所有预定的方法都应遵循以下两个标准 -
您可以通过在 @Scheduled
注释中使用 fixedRate
参数来实现以固定间隔执行方法。在下面的示例中,带注释的方法将每 2 秒执行一次。
@Scheduled(fixedRate = 2000)
public void scheduleTaskWithFixedRate() {
logger.info("Fixed Rate Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()) );
}
# Sample Output
Fixed Rate Task :: Execution Time - 10:26:58
Fixed Rate Task :: Execution Time - 10:27:00
Fixed Rate Task :: Execution Time - 10:27:02
....
....
fixedRate
任务在指定的时间间隔被调用,即使之前的任务调用没有完成也会被调用。
您可以使用 fixedDelay
参数在上一次调用完成和下一次调用开始之间以固定延迟执行任务。
fixedDelay
参数计算最后一次调用完成后的延迟时间。
参考以下示例 -
@Scheduled(fixedDelay = 2000)
public void scheduleTaskWithFixedDelay() {
logger.info("Fixed Delay Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException ex) {
logger.error("Ran into an error {}", ex);
throw new IllegalStateException(ex);
}
}
由于任务本身需要 5 秒才能完成,并且我们指定了在上一次调用完成和下一次调用开始之间有 2 秒的延迟,因此每次调用之间会有 7 秒的延迟 -
# Sample Output
Fixed Delay Task :: Execution Time - 10:30:01
Fixed Delay Task :: Execution Time - 10:30:08
Fixed Delay Task :: Execution Time - 10:30:15
....
....
您可以将 initialDelay
参数与 fixedRate
和 fixedDelay
一起使用,以指定的毫秒数延迟任务的第一次执行。
在下面的例子中,任务的第一次执行会延迟 5 秒,然后会以 2 秒的固定间隔正常执行——
@Scheduled(fixedRate = 2000, initialDelay = 5000)
public void scheduleTaskWithInitialDelay() {
logger.info("Fixed Rate Task with Initial Delay :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
}
# Sample output (Server Started at 10:48:46)
Fixed Rate Task with Initial Delay :: Execution Time - 10:48:51
Fixed Rate Task with Initial Delay :: Execution Time - 10:48:53
Fixed Rate Task with Initial Delay :: Execution Time - 10:48:55
....
....
如果上述简单参数不能满足您的需求,那么您可以使用 cron 表达式来执行任务的调度。
在以下示例中实现了每分钟执行一次任务 -
@Scheduled(cron = "0 * * * * ?")
public void scheduleTaskWithCronExpression() {
logger.info("Cron Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
}
# Sample Output
Cron Task :: Execution Time - 11:03:00
Cron Task :: Execution Time - 11:04:00
Cron Task :: Execution Time - 11:05:00
默认情况下,所有 @Scheduled
任务都在 Spring 创建的大小为 1 的默认线程池中执行。
您可以通过在所有方法中记录当前线程的名称来验证这一点 -
logger.info("Current Thread : {}", Thread.currentThread().getName());
所有方法都将打印如下内容 -
Current Thread : pool-1-thread-1
但是,您可以创建自己的线程池并配置 Spring 以使用该线程池来执行所有的计划任务。
在 com.example.schedulerdemo
内创建一个新包 config
,然后在 config
包内创建一个名为 SchedulerConfig
的新类,其内容如下 -
package com.example.schedulerdemo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
private final int POOL_SIZE = 10;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
threadPoolTaskScheduler.setThreadNamePrefix("my-scheduled-task-pool-");
threadPoolTaskScheduler.initialize();
scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
}
这就是配置 Spring 使用自己的线程池而不是默认线程池。
如果您现在在预定方法中记录当前线程的名称,您将获得如下输出 -
Current Thread : my-scheduled-task-pool-1
Current Thread : my-scheduled-task-pool-2
# etc...
在本文中,您学习了如何在 Spring Boot 中使用 @Scheduled
注解调度任务。您还学习了如何使用自定义线程池来运行这些任务。
您可以在 my github repository 中找到我们在本文中构建的项目的完整代码。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.callicoder.com/spring-boot-task-scheduling-with-scheduled-annotation/
内容来源于网络,如有侵权,请联系作者删除!