在应用程序启动后和午夜之后运行的Spring Scheduler [英] Spring scheduler which is run after application is started and after midnight

查看:58
本文介绍了在应用程序启动后和午夜之后运行的Spring Scheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何描述在应用程序启动后和00:00之后运行的Spring调度程序?

How to describe Spring scheduler which is run after application is started and after 00:00 ?

推荐答案

我将使用两个单独的结构来做到这一点.

I would do this with two separate constructs.

对于在应用程序启动之后,请使用 @PostConstuct ,对于每天午夜的午夜,请使用设置了 cron 值的 @Scheduled .两者都应用于方法.

For after the application starts, use @PostConstuct, and for every night at midnight use @Scheduled with the cron value set. Both are applied to a method.

public class MyClass {
    @PostConstruct
    public void onStartup() {
        doWork();
    }

    @Scheduled(cron="0 0 0 * * ?")
    public void onSchedule() {
        doWork();
    }

    public void doWork() {
        // your work required on startup and at midnight
    }
}

这篇关于在应用程序启动后和午夜之后运行的Spring Scheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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