简体   繁体   中英

How wait @Scheduled till previous task is not finished?

I want to wail my scheduler till my task is complete.if there is time for second schedule execution, it has to wait till previous task is not complete. I m using @Schedule in java Boot application. I want to insert data into data base in every 5 minutes but i want to hold my schedule till the inset data is not complete still there is a time for second execution.Demo Code

@Scheduled(fixedRate = 2000)
    public void scheduleTaskWithFixedRate() {
        logger.info("Fixed Rate Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()) );
    }

Use fixedDelay

fixedRate : makes Spring run the task on periodic intervals even if the last invocation may still be running.

fixedDelay : specifically controls the next execution time when the last execution finishes. In code:

@Scheduled(fixedDelay=5000)
public void updateEmployeeInventory(){

}    

@Scheduled(fixedRate=5000)
public void updateEmployeeInventory(){

}

Instead of fixedRate , use fixedDelay :

@Scheduled(fixedDelay = 2000)

The task will run fixedDelay milliseconds one after another

Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.

@Scheduled(fixedDelay = 1000, initialDelay = 1000)

您应该使用带有 @scheduled 批注的initialDelay道具,以便等待一段时间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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