Reputation: 25790
In my Spring Boot application, based on the Cron job(runs every 5 minutes) I need to process 2000 products in my database.
Right now the process time of these 2000 products takes more than 5 minutes. I ran into the issue where the second Cron job runs when the first one is not completed yet.
Is there in Spring/Cron out of the box functionality that will allow to synchronize these jobs and wait for the previous job completion before starting the next one?
Please advise how to properly implement such kind of system. Anyway, the following technologies are also available Neo4j, MongoDB, Kafka. Please advise how to properly design/implement this functionality using the Spring/Cron separately or even together with the mentioned technologies.
Upvotes: 3
Views: 3591
Reputation: 7521
1) You may try to use @Scheduled(fixedDelay = 5*60*1000)
. It will guarantee that next invocation will happen strictly in 5 minutes after previous one is finished. But this may break your scheduling requirements
2) You can limit the underlying ThreadExecutor's pool size to 1 thread, so next invocation will have to wait until previous is finished, but this, again, can break the logic, since it would affect all periodic tasks invoked by @Scheduled
3) You can use Quartz instead of spring's native @Scheduled
. It's more complicated to configure, but allows to achieve the desired behaviour via @DisallowConcurrentExecution
annotation or via setting JobDetail::isConcurrentExectionDisallowed
in your job details
Upvotes: 6