如何在 fixedDelay & 中给出分钟@scheduled spring boot 中的initialDelay? [英] How to give minute in fixedDelay & initialDelay in @scheduled spring boot?

查看:95
本文介绍了如何在 fixedDelay & 中给出分钟@scheduled spring boot 中的initialDelay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是春季调度程序的新手.我阅读了很多关于 @schedule 的文章,但在每个示例中,他们都以秒和毫秒为单位给出了时间.

I am new for scheduler in spring. I read so many articles on @schedule but in every example they gave time in seconds and milliseconds.

问题陈述:根据我的要求,在我的程序启动后,我的调度程序将在 15 分钟(初始延迟)后启动,然后每隔 5 分钟(FixedRate)执行一次任务.为了实现这一点,我如何才能以分钟为单位给出时间是他们解决此问题的最佳方法?

Problem Statement : As per my requirement, after my program start my scheduler will start after 15 minutes (initial Delay ) and then it executes the task every after 5 minutes (FixedRate) . To achieve this how can I give time in minutes is their any best way to achieve this problem ?

代码:

@Configuration
@EnableScheduling
public class ScheduledConfiguration {
    @Scheduled(fixedDelay = 300000, initialDelay = 900000)
    public void scheduleFixedRateWithInitialDelayTask() {

        long now = System.currentTimeMillis() / 1000;
        System.out.println("Fixed rate task with one second initial delay - " + now);
    }
}

通过使用上面的程序,我将实现,但我想避免 300000/900000 毫秒.其他方式

By using above program i will achive but I want to avoid 300000 / 900000 milliseconds . Other way

@Scheduled(fixedDelay = 5 * 60 * 1000, initialDelay = 15 * 60 * 1000)

推荐答案

虽然参加聚会有点晚,但是Spring

Although little bit late to the party, nevertheless another approach from Spring

@Scheduled(fixedDelayString = "PT15M", initialDelayString = "PT2H")

在这里您可以找到 详细信息 语法.

Here you can find the details about the syntax.

这篇关于如何在 fixedDelay & 中给出分钟@scheduled spring boot 中的initialDelay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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