![](/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.