简体   繁体   English

@scheduled注释如何在Spring Boot中工作

[英]How @scheduled annotation works in Spring Boot

I have an Spring Boot application in which I have a class with two methods likes below. 我有一个Spring Boot应用程序,其中有一个类有两种方法,如下所示。

@Component
public class ClassA {
  @Scheduled(fixedDelay = 900000)
  public void methodA(){
    //do task "A"
  }

 @Scheduled(fixedDelay = 600000)
 public void methodB(){
    //do task "B"
 }

}

As per my understanding methodA should execute every 15 minutes and methodB should execute every 10 minutes. 根据我的理解方法,A应每15分钟执行一次,methodB应每10分钟执行一次。 But as soon as applications starts both the jobs get invoked. 但是一旦应用程序启动,就会调用这些作业。 Is this the right behavior ? 这是正确的行为吗? If yes, then how can I stop them from execution immediately after application startup and run methodB after methodA? 如果是,那么如何在应用程序启动后立即停止执行它们并在methodA之后运行methodB?

As form the documentation: Scheduled (Spring Framework 5.0.4.RELEASE API) 作为文档的形式: Scheduled(Spring Framework 5.0.4.RELEASE API)

fixedDelay : Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next. fixedDelay :在最后一次调用结束和下一次调用开始之间以固定周期(以毫秒为单位)执行带注释的方法。

What you want is 你想要的是什么

initialDelay : Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task. initialDelay :在第一次执行fixedRate()或fixedDelay()任务之前延迟的毫秒数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM