Java – Spring Scheduler in Clustered environment

cluster-computing, java, scheduler, spring

I am using spring scheduler using @Scheduled annotation to schedule a job that runs a file generation service. The application is deployed on 5 separate nodes of tomcat in clustered environment for load balancing and fail over. Because of this, service is scheduled 5 times which is never expected.
Is there a way to configure the scheduler to be run only on the current node?

There is an approach that uses database to figure out the current alive node and invoking the scheduler for that particular instance here

Another approach is to use quartz scheduler

Since I could not make major changes to the deployed application, is there a simple solution to the issue? Please advise.

Best Solution

You can use ShedLock project. You just annotate tasks which should be locked when executed

@Scheduled( ... )@SchedulerLock(name = "scheduledTaskName")public void scheduledTask() {   // do something}

Configure Spring and a LockProvider (SQL and Mongo currently supported)

@Beanpublic TaskScheduler taskScheduler(LockProvider lockProvider) {   return SpringLockableTaskSchedulerFactory             .newLockableTaskScheduler(poolSize, lockProvider);}