Distributed scheduling in runtime Spring Boot Schedlock

Daniel Zielinski
4 min readApr 24, 2023

In today’s fast-paced and ever-evolving world of technology, efficient task scheduling is a critical component for any system or application. However, scheduling tasks in a distributed environment can be a complex and challenging task, especially when multiple instances of the same application need to coordinate and synchronize their activities. This is where a distributed scheduler like Spring Boot Schedlock comes into play, providing a reliable and scalable solution for distributed task scheduling. In this article, we will explore the key features and benefits of Schedlock with runtime declaration of jobs.

Standard configuration of Schedlock in Spring Boot.

@Component
public class TaskScheduler {

@Scheduled(cron = "0/15 * * * * ?")
@SchedulerLock(name = "myTask1", lockAtLeastFor = "PT10S", lockAtMostFor = "PT15S")
public void task1() {

}
}

To add another task you need to change your code, add new class or new method. In real life scenarios, you do not want to change your code, you want to add some configuration.

@Component
public class TaskScheduler {

@Scheduled(cron = "0/15 * * * * ?")
@SchedulerLock(name = "myTask1", lockAtLeastFor = "PT10S", lockAtMostFor = "PT15S")
public void task1() {

}

@Scheduled(cron = "0/15 * *…

--

--