1. Overview

Spring provides an easy to implement API for scheduling jobs. It works great until we deploy multiple instances of our application. Spring, by default, cannot handle scheduler synchronization over multiple instances – it executes the jobs simultaneously on every node instead.

In this short tutorial, we'll look at ShedLock – a Java library that makes sure our scheduled tasks run only once at the same time and is an alternative to Quartz.

2. Maven Dependencies

To use ShedLock with Spring, we need to add the shedlock-spring dependency:


3. Configuration

Note that ShedLock works only in environments with a shared database by declaring a proper LockProvider. It creates a table or document in the database where it stores the information about the current locks.

Currently, ShedLock supports Mongo, Redis, Hazelcast, ZooKeeper, and anything with a JDBC driver.

For this example, we'll use an in-memory H2 database. To make it work, we need to provide the H2 database and the ShedLock's JDBC dependency:


Next, we need to create a database table for ShedLock to keep information about scheduler locks:

CREATE TABLE shedlock (
  name VARCHAR(64),
  lock_until TIMESTAMP(3) NULL,
  locked_at TIMESTAMP(3) NULL,
  locked_by VARCHAR(255),
  PRIMARY KEY (name)

We should declare the data source in our Spring Boot application's properties file so that the DataSource bean can be Autowired. In this example, we use the application.yml to define the data source of the H2 database:

    driverClassName: org.h2.Driver
    username: sa

Let's config the LockProvider with the data source configuration above. Spring can make it pretty straightforward:

public class SchedulerConfiguration {
    public LockProvider lockProvider(DataSource dataSource) {
        return new JdbcTemplateLockProvider(dataSource);

Another configuration requirement we have to provide are the @EnableScheduling and @EnableSchedulerLock annotations on our Spring configuration class:

@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringApplication.class, args);

The defaultLockAtMostFor parameter specifies the default amount of time the lock should be kept in case the executing node dies. It uses the ISO8601 Duration format.

In the next section, we'll see how to override this default.

4. Creating Tasks

To create a scheduled task handled by ShedLock, we simply put the @Scheduled and @SchedulerLock annotations on a method:

class BaeldungTaskScheduler {

    @Scheduled(cron = "0 0/15 * * * ?")
    @SchedulerLock(name = "TaskScheduler_scheduledTask", 
      lockAtLeastForString = "PT5M", lockAtMostForString = "PT14M")
    public void scheduledTask() {
        // ...

First, let's look at @Scheduled. It supports the cron format, with this expression meaning “every 15 minutes”.

Next, taking a look at @SchedulerLock, the name parameter has to be unique, and ClassName_methodName is typically enough to achieve that. We don't want more than one run of this method happening at the same time, and ShedLock uses the unique name to achieve that.

We've also added a couple of optional parameters.

First, we've added lockAtLeastForString so that we can put some distance between method invocations. Using “PT5M” means that this method will hold the lock for 5 minutes, at a minimum. In other words, that means that this method can be run by ShedLock no more often than every five minutes.

Next, we added lockAtMostForString to specify how long the lock should be kept in case the executing node dies. Using “PT14M” means that it will be locked for no longer than 14 minutes.

In normal situations, ShedLock releases the lock directly after the task finishes. Now, we didn't have to do that because there is a default provided in @EnableSchedulerLock, but we've chosen to override that here.

5. Conclusion

In this article, we've learned how to create and synchronize scheduled tasks using ShedLock.

As always, all source code is available over on GitHub.

Spring bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Newest Most Voted
Inline Feedbacks
View all comments
1 year ago


Great article. Small remark. The cron pattern you defined is not every 15 minutes, but every 15 seconds 🙂

Loredana Crusoveanu
1 year ago
Reply to  Tobias

Hey Tobias,

Thanks, fixed. There was an extra * there 🙂


Rohit Kumar
Rohit Kumar
1 year ago

can we take cron value dynamic , my requirement is that i have capture value for crone and scheduler will run.
@Scheduled(cron = “*/15 * * * * *”)
like @Scheduled(cron = “*/x * * * * *”)

Thanks in Advance

Loredana Crusoveanu
1 year ago
Reply to  Rohit Kumar

Hi Rohit,

As I mentioned over email, you have several options. You can set different time expressions in the cron: https://www.baeldung.com/cron-expressions

Also, you can load the expression from a properties file as shown in section 8 here: https://www.baeldung.com/spring-scheduled-tasks

Otherwise, if you want more control over the scheduling, have a look at the TaskScheduler class: https://www.baeldung.com/spring-task-scheduler Using this, you can schedule a task programmatically, instead of using the @Scheduled annotation.


Comments are closed on this article!