Spring Boot 教程:调度

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

  调度用来处理特定时间周期的任务。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 启动之后如何每隔一分钟执行一次:

固定频度

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

@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 秒后,固定延迟调度器任务每秒执行一次的情况。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值