In modern software development, concurrent access to shared resources can lead to various issues such as data inconsistency and race conditions. Shed Lock is a powerful tool that helps developers manage these concurrency problems effectively. As a Shed Lock supplier, I am here to guide you on how to integrate and use Shed Lock with a Lift Framework application.
Understanding Shed Lock
Shed Lock is a library that provides distributed locks for scheduled tasks in a Java application. It ensures that only one instance of a scheduled task runs at a time across multiple nodes in a distributed system. This is crucial when dealing with shared resources or performing tasks that should not be executed concurrently.
The basic concept behind Shed Lock is simple: it uses a database or other storage systems to manage locks. When a scheduled task starts, it tries to acquire a lock. If the lock is available, the task proceeds; otherwise, it waits or skips the execution.
Prerequisites
Before we start integrating Shed Lock with a Lift Framework application, make sure you have the following:
- A working Lift Framework application. If you don't have one, you can create a new Lift project using SBT (Simple Build Tool).
- A database that is supported by Shed Lock. Popular choices include MySQL, PostgreSQL, and H2.
- Basic knowledge of Java and Lift Framework concepts such as scheduled tasks and database access.
Step 1: Add Shed Lock Dependencies
First, you need to add the Shed Lock dependencies to your Lift project. If you are using SBT, add the following lines to your build.sbt file:
libraryDependencies ++= Seq(
"net.javacrumbs.shedlock" % "shedlock-spring" % "4.40.0",
"net.javacrumbs.shedlock" % "shedlock-provider-jdbc-template" % "4.40.0"
)
These dependencies include the core Shed Lock library and the JDBC template provider, which allows us to use a relational database for lock management.
Step 2: Configure the Database
Next, you need to configure your database connection. In a Lift application, you can use the Lift's built - in database management system or a third - party library like Spring JDBC.


Here is an example of configuring a JDBC data source using Spring Boot's DataSource configuration in a Lift application:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class DatabaseConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/your_database");
dataSource.setUsername("your_username");
dataSource.setPassword("your_password");
return dataSource;
}
}
This code configures a MySQL data source. You need to replace the URL, username, and password with your actual database credentials.
Step 3: Create a Lock Provider
Shed Lock uses a lock provider to manage locks. For a JDBC - based database, you can create a JdbcTemplateLockProvider as follows:
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
public class ShedLockConfig {
@Bean
public JdbcTemplateLockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(
JdbcTemplateLockProvider.Configuration.builder()
.withJdbcTemplate(new JdbcTemplate(dataSource))
.usingDbTime()
.build()
);
}
}
The @EnableSchedulerLock annotation enables Shed Lock in your application, and the defaultLockAtMostFor attribute sets the maximum time a lock can be held.
Step 4: Define Scheduled Tasks
In a Lift application, you can define scheduled tasks using Lift's S object or Spring's @Scheduled annotation. Here is an example of using the @Scheduled annotation with Shed Lock:
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
@Scheduled(fixedRate = 60000)
@SchedulerLock(name = "myScheduledTask", lockAtMostFor = "PT10S", lockAtLeastFor = "PT5S")
public void myScheduledTask() {
// Your task logic here
System.out.println("Running scheduled task...");
}
}
The @SchedulerLock annotation is used to specify the name of the lock, the maximum time the lock can be held (lockAtMostFor), and the minimum time the lock should be held (lockAtLeastFor).
Step 5: Run the Application
Now that you have configured Shed Lock and defined your scheduled tasks, you can run your Lift application. When the scheduled task is triggered, Shed Lock will try to acquire the lock. If the lock is available, the task will execute; otherwise, it will skip the execution.
Advanced Usage
- Multiple Scheduled Tasks: You can define multiple scheduled tasks in your application, each with its own lock. Just make sure to use different lock names for each task.
- Error Handling: You can add error handling code in your scheduled tasks to handle cases where the lock cannot be acquired or the task fails to execute.
Additional Lock Types
In addition to the basic Shed Lock functionality, there are other types of locks that can be useful in different scenarios. For example, Push Lock can be used in situations where you need a simple and quick - locking mechanism. Self Locking Door is suitable for applications where security and automatic locking are required. And Storeroom Lock can be used to protect storage areas and ensure that only authorized access is allowed.
Conclusion
Integrating Shed Lock with a Lift Framework application is a straightforward process that can significantly improve the reliability and consistency of your scheduled tasks. By following the steps outlined in this guide, you can ensure that your application runs smoothly even in a distributed environment.
If you are interested in using our Shed Lock solutions for your Lift Framework applications or have any questions about our products, we invite you to contact us for a detailed discussion. Our team of experts is ready to assist you in finding the best locking solutions for your specific needs.
References
- Shed Lock official documentation.
- Lift Framework official documentation.
- Spring Boot documentation for JDBC and scheduling.
