Spring Boot – 调度

Spring Boot提供了在@Scheduled注解的帮助下安排任务在给定时间段内执行的能力。本文提供了关于我们如何安排任务在 Spring Boot 应用程序中运行的分步指南

执行:

下面逐步描述如下: 

第一步:使用 Spring Initializer 创建一个 Spring Boot 应用程序,可以参考创建 Spring 类的基础知识

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

// 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 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 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 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 应用程序时,我们可以在控制台中看到如下输出:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值