欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > Spring Boot教程之十四:Spring Boot 调度(Scheduling)

Spring Boot教程之十四:Spring Boot 调度(Scheduling)

2025/2/24 0:07:24 来源:https://blog.csdn.net/xt14327/article/details/144144615  浏览:    关键词:Spring Boot教程之十四:Spring Boot 调度(Scheduling)

Spring Boot – 调度

Spring Boot借助@Scheduled注释提供了在给定时间段内安排任务执行的功能。本文提供了有关如何在 Spring Boot 应用程序中安排任务运行的分步指南

实现

下面分步描述如下: 

步骤 1:使用 Spring Initializer 创建一个 Spring Boot 应用程序,相关内容可以参考创建 Spring 类的基础知识

第2步:在Spring Boot应用程序类中指定@EnableScheduling注释。

Java

// Java Program to Illustrate Specifying
// @EnableScheduling annotation

package com.Scheduler;

// Importing required classes
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

// Annotation
@SpringBootApplication
@EnableScheduling

// Class
public class SchedulerApplication {

    // Main driver method
    public static void main(String[] args)
    {
        SpringApplication.run(SchedulerApplication.class,
                              args);
    }
}

@EnableScheduling注解为Spring Boot提供了计划任务执行能力。

步骤 3:创建一个@ComponentScheduler,它定义方法scheduleTask()以使用@Scheduled注释来安排任务。

Scheduler 类中的方法scheduleTask()只是打印任务运行的日期和时间。

使用 cron 表达式安排任务

Java

// Java Program to Illustrate Scheduling Task
// using a cron expression

package com.Scheduler;

// Importing required classes
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

// Annotation
@Component
// Class
public class Scheduler {

    // Method
    // To trigger the scheduler every one minute
    // between 19:00 PM to 19:59 PM
    @Scheduled(cron = "0 * 19 * * ?")
    public void scheduleTask()
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
            "dd-MM-yyyy HH:mm:ss.SSS");

        String strDate = dateFormat.format(new Date());

        System.out.println(
            "Cron job Scheduler: Job running at - "
            + strDate);
    }
}

@Scheduled注释中指定的cron元素允许定义类似 cron 的表达式以包括在秒、分钟、小时、月日、月和星期几触发。cron元素中此处指定的表达式指示 spring boot 在 19:00.00 至 19:59.00 之间每隔一分钟触发一次调度程序。

运行 Spring Boot 应用程序时,我们可以在控制台中看到以下输出:

按固定费率安排任务

Java

// Java Program to Illustrate Scheduling Task
// At a Fixed Rate

package com.Scheduler;

// Importing required classes
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

// Annotation
@Component
// Class
public class Scheduler {

    // Method
    // To trigger the scheduler to run every two seconds
    @Scheduled(fixedRate = 2000) public void scheduleTask()
    {

        SimpleDateFormat dateFormat = new SimpleDateFormat(
            "dd-MM-yyyy HH:mm:ss.SSS");

        String strDate = dateFormat.format(new Date());

        System.out.println(
            "Fixed rate Scheduler: Task running at - "
            + strDate);
    }
}

@Scheduled注释中指定的fixedRate元素在调用之间以固定的时间间隔执行注释的方法。它不会等待上一个任务完成。为此元素指定的时间值以毫秒为单位。

这里定义了一个固定速率调度程序,从 19:11:58 开始每 2 秒运行一次。

运行 Spring Boot 应用程序时,我们可以在控制台中看到以下输出:

安排任务以固定延迟运行

Java

// Java Program to Illustrate Scheduling Task
// at a Fixed Delay

package com.Scheduler;

// Importing required classes
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

// Annotation
@Component
// Class
public class Scheduler {

    // Method
    // To trigger the scheduler every 3 seconds with
    // an initial delay of 5 seconds.
    @Scheduled(fixedDelay = 3000, initialDelay = 5000)

    public void scheduleTask()
    {

        SimpleDateFormat dateFormat = new SimpleDateFormat(
            "dd-MM-yyyy HH:mm:ss.SSS");

        String strDate = dateFormat.format(new Date());

        System.out.println(
            "Fixed Delay Scheduler: Task running at - "
            + strDate);
    }
}

@Scheduled注释中指定的fixedDelay元素会在上一次调用结束和下一次调用开始之间的固定时间段内执行注释的方法。它基本上等待上一个任务完成。此元素指定的时间值以毫秒为单位。

此处指定的initialDelay元素允许提及在调用第一个任务之前等待的时间量。此元素指定的时间值以毫秒为单位。

这里定义的调度程序以 5 秒的初始延迟开始,并以 3 秒的固定延迟执行任务。

运行 Spring Boot 应用程序时,我们可以在控制台中看到以下输出:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com