![](/img/trans.png)
[英]How to start @scheduled cron only when method is explicitly called?
[英]How to define when my @Scheduled method should be called first time?
我的项目刚开始时第一次调用@Scheduled 方法(它没有完全启动)。我可以定义我的@Scheduled 方法何时应该第一次调用(不使用初始延迟)。我希望我的所有@Scheduled 方法开始执行我的服务器第一次启动时。所以我的启动时间会减少。
我使用了固定延迟调度程序:
@Scheduled(fixedDelay = 1800000) // runs in every 30min
public void schedulerFunction(){}
您可以实现ApplicationListener
并等待ContextReadyEvent
:
@Component
public class YourClassHavingScheduledMethodimplements ApplicationListener<ContextRefreshedEvent> {
private boolean contextInitialized = false;
@Scheduled(...)
public void someScheduledMethod() {
if(this.contextInitialized) {
// Execute logic here
}
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
this.contextInitialized = true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.