1.@scheduled注解定义
简单来说, @scheduled
是 spring 提供的一个注解,用于在方法上标记定时任务。通过它,我们可以轻松地在指定的时间间隔或特定的时间点执行某些代码,而不需要引入额外的定时任务库。
举个例子:
假设你有一个方法需要每隔5分钟执行一次,你只需要在方法上加上 @scheduled
注解,并设置相应的属性即可。
2.配置 @scheduled
在开始使用 @scheduled
之前,我们需要做一些配置工作。首先,确保你的 spring 项目中引入了 spring-boot-starter
,因为它已经包含了必要的依赖。
2.1 开启定时任务支持
在你的主类(通常标注了 @springbootapplication
的类)上添加@enablescheduling
注解,以启用定时任务的支持。
import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.scheduling.annotation.enablescheduling; @springbootapplication @enablescheduling public class scheduleddemoapplication { public static void main(string[] args) { springapplication.run(scheduleddemoapplication.class, args); } }
2.2 创建定时任务
接下来,我们创建一个服务类,并在其中定义一个定时任务方法。例如,每隔5秒打印一条消息:
import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.component; @component public class scheduledtasks { @scheduled(fixedrate = 5000) public void reportcurrenttime() { system.out.println("每5秒执行一次任务,当前时间:" + system.currenttimemillis()); } }
3. 常用属性
@scheduled
注解提供了多种方式来配置定时任务的执行时间,主要包括以下几种
3.1 fixedrate
指定一个固定的时间间隔,以毫秒为单位,表示上一次任务开始执行后,多久再次执行。
@scheduled(fixedrate = 5000) // 每5秒执行一次 public void fixedratetask() { system.out.println("fixed rate task - " + system.currenttimemillis()); }
3.2 fixeddelay
指定一个固定的时间间隔,表示上一次任务执行完成后,等待多久再次执行。
@scheduled(fixeddelay = 5000) // 上一次任务完成后5秒执行一次 public void fixeddelaytask() { system.out.println("fixed delay task - " + system.currenttimemillis()); }
3.3 cron
使用 cron 表达式确地指定任务的执行时间。cron 表达式可以让你定义复杂的时间计划。
@scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次 public void crontask() { system.out.println("cron task - " + system.currenttimemillis()); }
4.工作原理
了解了如何使用 @scheduled
,那么它背后到底是如何运作的呢?让我们来深入探讨一下。
4.1 基于taskscheduler
spring 的定时任务是基于 taskscheduler 接口实现的。当我们在方法上使用 @scheduled 注解时,spring 会自动为其创建一个调度器,并按照我们定义的时间计划来执行任务
4.2 使用 threadpooltaskscheduler
默认情况下,spring 使用 threadpooltaskscheduler
作为taskscheduler
的实现类。它内部维护了一个线程池,用于执行定时任务。这样可以确保多个定时任务能够并发执行,而不会阻塞主线程。
注意: 如果你的应用中有多个定时任务,或者某些任务执行时间较长,建议自定义threadpooltaskscheduler
的线程池大小,以避免任务堆积或资源浪费。
4.3 定时任务的执行流程
1、初始化阶段:
启动 spring 应用时,@enablescheduling 注解会触发 spring 的配置,扫描所有被 @scheduled 注解标记的方法。
2、注册任务:
所有符合条件的定时任务方法会被注册到 taskscheduler 中。
3、执行任务:
根据配置的时间计划,taskscheduler 会调度并在合适的线程中执行相应的任务方法。
5. 延时执行的定时任务
为了更好地理解 @scheduled
的使用,我们来实现一个稍微复杂些的示例——延时执行任务。假设我们有一个任务需要在应用启动后延时10秒执行一次,然后每隔5秒重复执行。
5.1 创建定时任务类
import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.component; @component publicclass delayedscheduledtasks { privateboolean firstrun = true; @scheduled(fixedrate = 5000, initialdelay = 10000) public void delayedtask() { if (firstrun) { system.out.println("延时10秒后首次执行任务,当前时间:" + system.currenttimemillis()); firstrun = false; } else { system.out.println("每5秒执行一次任务,当前时间:" + system.currenttimemillis()); } } }
5.2 解释
fixedrate = 5000
: 任务每5秒执行一次。initialdelay = 10000
: 应用启动后,延时10秒首次执行任务。
6.自定义taskscheduler
有时候,默认的 threadpooltaskscheduler
可能无法满足我们的需求,比如需要更高的并发能力或特定的线程名称模式。这时候,我们可以自定义一个 taskscheduler bean
。
import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.scheduling.concurrent.threadpooltaskscheduler; @configuration publicclass schedulerconfig { @bean public threadpooltaskscheduler taskscheduler() { threadpooltaskscheduler scheduler = new threadpooltaskscheduler(); scheduler.setpoolsize(10); // 设置线程池大小 scheduler.setthreadnameprefix("myscheduler-"); // 设置线程名称前缀 scheduler.initialize(); return scheduler; } }
通过上述配置,我们创建了一个拥有10个线程的线程池,并为每个线程命名,方便日志追踪和调试。
到此这篇关于spring @scheduled注解详解的文章就介绍到这了,更多相关spring @scheduled注解内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论