且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何配置Spring Scheduled Task在特定延迟的时间范围内运行?

更新时间:2023-10-10 09:18:46

Maven依赖

春季上下文

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
  </dependency>

启用调度功能的配置

参考从文档中

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class MyAppConfig {
//..
}

在您指定的Cron上触发的方法

参考文档

// 0/15 -> Every 15 minutes on the clock
// 17-20 -> Between 5pm and 8pm on the JVM timezone
// if you want timezone specific there is a 'zone' parameter: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html#zone--
@Scheduled(cron="0 0/15 17-20 * * ?")
public void doSomething() {
    // ..
}

文档

有关计划的Spring文档

Spring Boot

为上述cron运行的安装程序的Spring Boot示例