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

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

问题描述

如何描述在应用程序启动后和 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,每晚午夜使用@Scheduled 并设置cron 值.两者都应用于一个方法.

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 调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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