初始延迟的Cron表达式 - Quartz [英] Cron expression with initial delay - Quartz

查看:1955
本文介绍了初始延迟的Cron表达式 - Quartz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是可以弄清楚如何配置Cron作业在Quartz与初始延迟。
所以我需要每小时运行一次,初始延迟10分钟。

 * * 0/1 * *? 


解决方案

这是一个迟到的答案,希望这可以帮助别人。我通过在我的服务类中有2个计划的函数来解决这个问题:

  @EnableScheduling 
public class DeviceService {

@Scheduled(ini​​tialDelayString =$ {devices.update.initial},fixedDelay = 2592000000L)
public void initialUpdateDevices(){
updateDevices() ;
}

@Scheduled(cron =$ {devices.update.cron})
public void cronUpdateDevices(){
updateDevices();
}

private void updateDevices(){
...
}
}

初始延迟和cron表达式在application.properties中设置。 fixedDelay是因为Spring不允许initialDelay单独。我设置为2592000000ms,这是30天。在我们的应用程序中,潜在的额外更新不会造成任何损害。



在application.properties:



< =lang-java prettyprint-override> devices.update.initial = 600000
devices.update.cron = 0 30 1 * * *



最初在10分钟(60000ms)后运行,然后每天晚上在01:30运行。



在用于单元测试的application-test.properties中:

  devices.update.initial = 86400000 
devices.update.cron = 0 30 1 24 12 *

我们的单元测试都不需要1天执行这样86400000毫秒是一个安全的赌注。 cron0 30 1 24 12 *被设置为圣诞节前夕的夜晚,人们应该做梦的美好的事情。


I am just can figure out how to configure a Cron job in Quartz with initial delay. So i need something that runs every hour with an initial delay of 10 min.

"* * 0/1 * * ?"

解决方案

Here's a late answer, hopefully this helps others. I solved the issue by having 2 scheduled functions in my service class:

@EnableScheduling
public class DeviceService {

    @Scheduled(initialDelayString = "${devices.update.initial}", fixedDelay = 2592000000L)
    public void initialUpdateDevices() {
        updateDevices();
    }

    @Scheduled(cron = "${devices.update.cron}")
    public void cronUpdateDevices() {
        updateDevices();
    }

    private void updateDevices() {
        ...
    }
}

The initial delay and the cron expression are set in application.properties. The fixedDelay is there because Spring doesn't allow initialDelay alone. I set it to 2592000000ms, which is 30 days. In our application, the potential extra update doesn't do any harm.

In application.properties:

devices.update.initial = 600000
devices.update.cron = 0 30 1 * * *

Initially run after 10 minutes (60000ms) and then every night at 01:30.

In application-test.properties for unit testing:

devices.update.initial = 86400000
devices.update.cron = 0 30 1 24 12 *

None of our unit tests take 1 day to execute so 86400000 milliseconds is a safe bet. The cron "0 30 1 24 12 *" is set to Christmas Eve's night when people should be dreaming of nice things.

这篇关于初始延迟的Cron表达式 - Quartz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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