简体   繁体   中英

Possible to check if `@Scheduled` method has finished executing?

I'm using @Scheduled for one of my methods.

Is there anyway to check if the prior @Scheduled task has finished executing?

For example if we have a @Scheduled to run every 60 seconds, but sometimes the method takes 80 seconds to complete. Is there anyway to prevent the method from running again unless it has finish executing?

I suppose you're using @Scheduled(fixedRate = 60000)

In this case, you shouldn't worry about possible overlapping of tasks because they're executing consistently in one thread.

ScheduledAnnotationBeanPostProcessor uses ScheduledTaskRegistrar under the hood, which, in turn, uses ScheduledExecutorService (see docs and source code ).

And from the docs of scheduleAtFixedRate method of ScheduledExecutorService , we can clearly see, that

If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

fixedDelay executes the method with a fixed period of milliseconds between the end of one invocation and the start of the next.

Try with this

@Scheduled(fixedDelay = 60*1000)

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