Arya
Arya

Reputation: 8965

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?

Upvotes: 0

Views: 788

Answers (2)

amseager
amseager

Reputation: 6391

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.

Upvotes: 1

GolamMazid Sajib
GolamMazid Sajib

Reputation: 9437

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)

Upvotes: 0

Related Questions