SpringBoot实现定时任务-@Scheduled

在Java编程中,@Scheduled 注解是 Spring Framework 提供的一个用于定时任务的注解

@Scheduled可以将一个方法配置为定时执行的任务

以下是一些基本用法:

1.固定速度

  • fixedRate指两次任务的开始时间间隔。
  • 所以存在第二次任务开始时,第一次任务可能还没结束。
// 每5秒执行一次任务
@Scheduled(fixedRate = 5000)
public void fixedRateTask() {
    System.out.println("fixedRateTask: The time is now " + new Date());
}

2.固定延迟

  • fixedDelay指本次任务结束到下次任务开始之间的时间间隔。
// 在上一次任务执行完成后,延迟5秒执行下一次任务。
@Scheduled(fixedDelay = 5000)
public void fixedDelayTask() {
    System.out.println("fixedDelayTask: The time is now " + new Date());
}

3.初始延迟

  • initialDelay指首次任务启动的延迟时间。
// 应用启动后延迟1秒开始执行任务,之后每5秒执行一次。
@Scheduled(initialDelay = 1000, fixedRate = 5000)
public void initialDelayTask() {
    System.out.println("initialDelayTask: The time is now " + new Date());
}

4.cron 表达式

使用 cron表达式来定义任务的执行时间。

// 每分钟执行一次。
@Scheduled(cron = "0 * * * * ?")
public void cronTask() {
    System.out.println("cronTask: The time is now " + new Date());
}

@EnableScheduling

使用 @Scheduled 注解时,需要配置@EnableScheduling 注解,配置后Spring 的调度器会检测并自动管理定时任务的执行。

注意:时间单位是毫秒

@EnableScheduling
@Configuration
public class ScheduledConfig {
}

Component
public class ScheduledTasksTest {
    // 每5秒执行一次任务
    @Scheduled(fixedRate = 5000)
    public void fixedRateTask() {
        System.out.println("fixedRateTask: The time is now " + new Date());
    }

    // 在上一次任务执行完成后,延迟5秒执行下一次任务。
    @Scheduled(fixedDelay = 5000)
    public void fixedDelayTask() {
        System.out.println("fixedDelayTask: The time is now " + new Date());
    }

    // 应用启动后延迟1秒开始执行任务,之后每5秒执行一次。
    @Scheduled(initialDelay = 1000, fixedRate = 5000)
    public void initialDelayTask() {
        System.out.println("initialDelayTask: The time is now " + new Date());
    }

    // 每分钟执行一次。
    @Scheduled(cron = "0 * * * * ?")
    public void cronTask() {
        System.out.println("cronTask: The time is now " + new Date());
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot通过@EnableScheduling注解来开启定时任务的功能。下面是实现动态定时任务的步骤: 1. 首先,在你的Spring Boot应用的主类上添加@EnableScheduling注解,启用定时任务的支持。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 2. 接下来,在需要执行定时任务的方法上添加@Scheduled注解,并设置定时任务的执行规则。例如,我们可以使用cron表达式来定义定时任务的执行时间。以下是一个示例: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyScheduledTask { @Scheduled(cron = "0 0/5 * * * *") // 每5分钟执行一次 public void doSomething() { // 定时任务要执行的操作 } } ``` 3. 现在,当应用启动后,定时任务会按照定义的规则自动执行。 如果你想在运行时动态地修改定时任务的执行规则,可以借助Spring的ScheduledTaskRegistrar类。可以在应用程序中注入ScheduledTaskRegistrar对象,然后使用其方法来注册、取消和修改定时任务。这样你就可以实现动态的定时任务调度了。 希望这个回答对你有帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值