【@Scheduled定时任务】

本文介绍了如何在SpringBoot中配置和使用@Scheduled定时任务,包括在启动类添加@EnableScheduling注解,创建定时任务类并使用@Component和@Scheduled注解,以及详细解释了cron表达式的用法,并提供了多个示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、SpringBoot项目的启动类上加@EnableScheduling注解,开启定时任务

@EnableScheduling
@SpringBootApplication
public class TasktestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TasktestApplication.class, args);
    }

}

2、创建一个定时任务类,在类上加@Component注解,在方法上加@Scheduled注解

@Component
public class TimingTaskService {
        @Scheduled(cron = "0 0/2 * * * ?")
        public void test(){
               System.out.println("开启定时任务,每两分钟执行一次任务");
        }

        //12月15日17点00-03分10-20秒执行
        @Scheduled(cron = "10-20 00-03 17 15 12 ?")
        public void test(){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("成功"+simpleDateFormat.format(new Date()));
        }
}

3、cron表达式

常用cron表达式例子

  (1)0/2 * * * * ?   表示每2秒 执行任务

  (1)0 0/2 * * * ?    表示每2分钟 执行任务

  (1)0 0 2 1 * ?   表示在每月的1日的凌晨2点调整任务

  (2)0 15 10 ? * MON-FRI   表示周一到周五每天上午10:15执行作业

  (3)0 15 10 ? 6L 2002-2006   表示2002-2006年的每个月的最后一个星期五上午10:15执行作

  (4)0 0 10,14,16 * * ?   每天上午10点,下午2点,4点 

  (5)0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时 

  (6)0 0 12 ? * WED    表示每个星期三中午12点 

  (7)0 0 12 * * ?   每天中午12点触发 

  (8)0 15 10 ? * *    每天上午10:15触发 

  (9)0 15 10 * * ?     每天上午10:15触发 

  (10)0 15 10 * * ?    每天上午10:15触发 

  (11)0 15 10 * * ? 2005    2005年的每天上午10:15触发 

  (12)0 * 14 * * ?     在每天下午2点到下午2:59期间的每1分钟触发 

  (13)0 0/5 14 * * ?    在每天下午2点到下午2:55期间的每5分钟触发 

  (14)0 0/5 14,18 * * ?     在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 

  (15)0 0-5 14 * * ?    在每天下午2点到下午2:05期间的每1分钟触发 

  (16)0 10,44 14 ? 3 WED    每年三月的星期三的下午2:10和2:44触发 

  (17)0 15 10 ? * MON-FRI    周一至周五的上午10:15触发 

  (18)0 15 10 15 * ?    每月15日上午10:15触发 

  (19)0 15 10 L * ?    每月最后一日的上午10:15触发 

  (20)0 15 10 ? * 6L    每月的最后一个星期五上午10:15触发 

  (21)0 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发 

  (22)0 15 10 ? * 6#3   每月的第三个星期五上午10:15触发


如果写的有不对的地方,请大家指出来,我们一起进步,谢谢。 

### Spring Boot 中 `@Scheduled` 注解的使用方法 #### 启用调度功能 为了使定时任务生效,在应用程序的主要配置类或启动类上需添加 `@EnableScheduling` 注解[^3]。 ```java import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableScheduling public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` #### 定义定时任务 定义一个组件类,其中包含被标记为 `@Scheduled` 的无参方法。该注解支持多种方式来设定执行频率: - **Cron 表达式**: 使用复杂的日程安排模式。 - **Fixed Rate/Interval**: 设置固定的间隔时间触发任务。 ##### Cron 表达式的例子 下面的例子展示了每分钟的第一秒运行一次的任务设置[^2]: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { @Scheduled(cron = "0 * * * * ?") // 每整点的第 0 秒执行 public void reportCurrentTime() { System.out.println("The time is now"); } } ``` ##### Fixed Delay 或者 Fixed Rate 的例子 这里展示了一个每隔两秒钟执行一次的方法实例: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class SimpleTaskScheduler { private int count = 0; @Scheduled(fixedRate = 2000L) public void performTaskWithFixedRate() { System.out.printf("Executing task with fixed rate %d\n", ++count); } } ``` 当采用 `fixedDelay` 参数时,则是在前次调用完成后等待指定毫秒数再开始下一轮;而 `fixedRate` 则无视上次执行耗时长短,严格按照给定周期重复执行[^1].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值