繁体   English   中英

如何等待@Scheduled 直到上一个任务未完成?

[英]How wait @Scheduled till previous task is not finished?

我想哭我的调度程序直到我的任务完成。如果有时间执行第二个调度程序,它必须等到上一个任务未完成。 我在 java Boot 应用程序中使用 @Schedule 。 我想每 5 分钟将数据插入到数据库中,但我想保持我的日程安排,直到插入数据不完整仍然有时间进行第二次执行。 演示代码

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

使用fixedDelay

fixedRate :使 Spring 定期运行任务,即使最后一次调用可能仍在运行。

fixedDelay :具体控制上次执行完成后的下一次执行时间。 在代码中:

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

}    

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

}

而不是fixedRate ,使用fixedDelay

@Scheduled(fixedDelay = 2000)

任务将一个接一个地运行fixedDelay毫秒

在上次调用结束和下一次调用开始之间以毫秒为单位的固定时间段内执行带注释的方法。

@Scheduled(fixedDelay = 1000, initialDelay = 1000)

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

暂无
暂无

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

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