Spring Boot 简明教程

Spring Boot - Scheduling

调度是一个针对特定时间段执行任务的过程。Spring 引导为在 Spring 应用程序上编写调度器提供了良好的支持。

Scheduling is a process of executing the tasks for the specific time period. Spring Boot provides a good support to write a scheduler on the Spring applications.

Java Cron Expression

Java Cron 表达式用于配置 CronTrigger 的实例,CronTrigger 是 org.quartz.Trigger 的子类。有关 Java cron 表达式的更多信息,您可以参考此链接 −

Java Cron expressions are used to configure the instances of CronTrigger, a subclass of org.quartz.Trigger. For more information about Java cron expression you can refer to this link −

@EnableScheduling 注解用于为应用程序启用调度程序。此注解应添加到主 Spring Boot 应用程序类文件中。

The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file.

@SpringBootApplication
@EnableScheduling

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

@Scheduled 注解用于在特定时间段内触发调度程序。

The @Scheduled annotation is used to trigger the scheduler for a specific time period.

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

以下是一个示例代码,展示了如何从早上 9:00 开始到早上 9:59 结束的每天每分钟执行一次任务

The following is a sample code that shows how to execute the task every minute starting at 9:00 AM and ending at 9:59 AM, every day

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 启动,并且从该时间开始的每分钟 Cron 作业调度器任务都已执行。

The following screenshot shows how the application has started at 09:03:23 and for every one minute from that time the cron job scheduler task has executed.

cron job scheduler

Fixed Rate

固定频率调度程序用于在特定时间执行任务。它不会等待上一个任务完成。值应以毫秒为单位。示例代码如下所示:

Fixed Rate scheduler is used to execute the tasks at the specific time. It does not wait for the completion of previous task. The values should be in milliseconds. The sample code is shown here −

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

这里有一个在应用程序启动后每秒执行一次任务的示例代码:

A sample code for executing a task on every second from the application startup is shown here −

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 启动的应用程序,之后每秒执行一次固定频率调度器任务。

Observe the following screenshot that shows the application that has started at 09:12:00 and after that every second fixed rate scheduler task has executed.

fixed rate scheduler task executed

Fixed Delay

固定延迟调度程序用于在特定时间执行任务。它应该等到上一个任务完成。值应以毫秒为单位。示例代码如下所示:

Fixed Delay scheduler is used to execute the tasks at a specific time. It should wait for the previous task completion. The values should be in milliseconds. A sample code is shown here −

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

在此处,initialDelay 是任务在初始延迟值后第一次执行后的时间。

Here, the initialDelay is the time after which the task will be executed the first time after the initial delay value.

下面展示了一个从应用程序启动后 3 秒内每秒执行一次任务的示例:

An example to execute the task for every second after 3 seconds from the application startup has been completed is shown below −

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 秒的每秒都执行固定延迟调度器任务。

Observe the following screenshot which shows the application that has started at 09:18:39 and after every 3 seconds, the fixed delay scheduler task has executed on every second.

fixed delay scheduler task executed