ShedLock with Spring

Ali Tokgönül
Sep 19, 2021 · 2 min read

If you use Spring Schedule, it can make your life easier when you want to schedule a job. However, when you deploy multiple instances of your project, you have to manage the schedules because Spring Schedule cannot deal with schedule synchronization with multiple instances. In this point, ShedLock helps us to lock the instances.

Github for ShedLock

https://github.com/lukas-krecan/ShedLock

Maven Dependency

<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>4.0.3</version>
</dependency>

Lock Provider Bean

First, we need to create a bean in order to create a lock table. The default lock table name is ‘shedlock’. If you want to create a different name you have to create the bean and give a name.

EnableSchedulerLock

You can add EnableSchedulerLock to either the scheduler class or the main spring class.

SchedulerLock

This is the ScheduleLock example :

I need to mention about three different parameters you can use with SchedulerLock

  • name : The name should be unique because it is stored as pk in table created for the lock system.
  • lockAtLeastFor : In the example, it’s ‘15M’, meaning it cannot be triggered again for 15 minutes.
  • lockAtMostFor: In the example, it’s ‘30M’. It is a precaution in case the batch completion time exceeds 15 minutes. If the batch is not completed in 15 minutes, the batch will not be re-triggered until 30 minutes.

Alternative

If you search for an alternative you can have a look at Quartz Scheduler

Reference

https://www.baeldung.com/shedlock-spring

adessoTurkey

Stay tuned for the latest insights on innovation, business, people and technology.