如何配置Spring Scheduled Task在特定延迟的时间范围内运行? [英] How can I configure Spring Scheduled Task to run in a time range with specific delay?

查看:824
本文介绍了如何配置Spring Scheduled Task在特定延迟的时间范围内运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置春季计划时间,从下午5点到上午8点每15分钟执行一次,如何指定这种表达方式?而且我也希望任务在工作日不仅执行MON-FRI,而且要根据我对isBusinessDay逻辑的执行。

I need set up spring scheduled time to be executed every 15 minutes from 5 p.m till 8 a.m, how to specify such expression? And also I'd like task be executed on weekdays not just MON-FRI, but according to my implementation of isBusinessDay logic.

推荐答案

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示例

这篇关于如何配置Spring Scheduled Task在特定延迟的时间范围内运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆