美文网首页
Spring Boot 教程:调度

Spring Boot 教程:调度

作者: 码者无疆 | 来源:发表于2020-09-11 14:49 被阅读0次

    【注】本文译自: https://www.tutorialspoint.com/spring_boot/spring_boot_scheduling.htm

    image
      调度用来处理特定时间周期的任务。Spring Boot 为 Spring 应用编写调度器提供了良好的支持。

    Java Cron 表达式

      Java Cron 表达式用于配置 CronTrigger 实例,是 org.quartz.Trigger 的子类。关于 Java cron 表达式的更多信息可参考:https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm
      @EnableScheduling 注解用于使你的应用能够使用调度器。这个注解应当被加在主 Spring Boot 应用类文件中。

    @SpringBootApplication
    @EnableScheduling
    
    public class DemoApplication {
       public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
       }
    }
    

      @Scheduled 注解用于触发一个特定时间周期的调度器。

    @Scheduled(cron = "0 * 9 * * ?")
    public void cronJobSch() throws Exception {
    }
    

      以下代码展示了如何在每天的早上 9:00 到 9:59 之间每分钟执行任务:

    package com.tutorialspoint.demo.scheduler;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Scheduler {
       @Scheduled(cron = "0 * 9 * * ?")
       public void cronJobSch() {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
          Date now = new Date();
          String strDate = sdf.format(now);
          System.out.println("Java cron job expression:: " + strDate);
       }
    }
    

      以下截图展示了应用在 09:03:23 启动之后如何每隔一分钟执行一次:


    image

    固定频度

      固定频度调度器被用于在特定时间执行任务。它不等待前一个任务完成,时间单位为毫秒。示例代码如下:

    @Scheduled(fixedRate = 1000)
    public void fixedRateSch() { 
    }
    

      以下代码示例是应用启动后的每秒钟执行一个任务:

    package com.tutorialspoint.demo.scheduler;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Scheduler {
       @Scheduled(fixedRate = 1000)
       public void fixedRateSch() {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    
          Date now = new Date();
          String strDate = sdf.format(now);
          System.out.println("Fixed Rate scheduler:: " + strDate);
       }
    }
    

      观看以下截屏,可以看出应用在 09:12:00 启动后以每隔一秒钟的固定频度执行任务:

    固定延迟

      固定延迟调度器用于在指定时间执行任务。它应当等待上一个任务完成,单位为毫秒。示例代码如下:

    @Scheduled(fixedDelay = 1000, initialDelay = 1000)
    public void fixedDelaySch() {
    }
    

      这里,initialDelay 是在初始化之后到首次执行间的延迟值。
      下面的例子中,是从应用启动完成后 3 秒后执行每秒一次的任务:

    package com.tutorialspoint.demo.scheduler;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Scheduler {
       @Scheduled(fixedDelay = 1000, initialDelay = 3000)
       public void fixedDelaySch() {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
          Date now = new Date();
          String strDate = sdf.format(now);
          System.out.println("Fixed Delay scheduler:: " + strDate);
       }
    }
    

      下面看到的截屏显示的是应用在 09:18:39 启动完成 3 秒后,固定延迟调度器任务每秒执行一次的情况。


    image

    相关文章

      网友评论

          本文标题:Spring Boot 教程:调度

          本文链接:https://www.haomeiwen.com/subject/hexbektx.html