Load balancing for spring boot scheduler service

Viewed 676

I created a java spring boot scheduler service using @Enablescheduler and @scheduler(cron=) and need to run this on cluster. Can some one help how can I create cluster load balancing the application.

1 Answers

You would actually need to make sure that once scheduled task has started on one node, other nodes in the cluster are blocked to execute the same task.

You can use Shedlock for this purpose. You would need to create database table called shedlock, and it should contain attributes: name, lock_at, lock_until, locked_by(node).

Then create a scheduled task like this:

@Scheduled(chron="..")
@SchedulerLock(name="..", lockAtLeast="..")
public void scheduledTask() {
    LockAssert.assertLocked();
    //do something
}

For more documentation try Shedlock

Related