In the realm of high - availability systems, ensuring that certain critical tasks are executed with proper synchronization and without race conditions is of utmost importance. This is where Shed Lock comes into play. As a Shed Lock supplier, I'm here to guide you through the process of configuring Shed Lock for high - availability environments.
Understanding Shed Lock
Shed Lock is a library that provides distributed locks for scheduled tasks in Java applications. In high - availability systems, multiple instances of an application may try to execute the same scheduled task simultaneously. This can lead to data integrity issues, duplicate processing, and other problems. Shed Lock solves this problem by using a shared storage (such as a database) to manage locks. Only one instance can acquire the lock at a time, ensuring that the task is executed only once.
Prerequisites
Before we start configuring Shed Lock, make sure you have the following:
- A Java application running on a high - availability infrastructure. This could be a set of servers in a cluster, deployed in a cloud environment like AWS or Google Cloud.
- A database that can be used as a shared storage for the locks. Popular choices include MySQL, PostgreSQL, and H2.
- Maven or Gradle configured to manage dependencies.
Step 1: Add Dependencies
The first step is to add the Shed Lock dependencies to your project. If you're using Maven, add the following to your pom.xml:
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>4.42.0</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>4.42.0</version>
</dependency>
For Gradle, add these lines to your build.gradle:
implementation 'net.javacrumbs.shedlock:shedlock-spring:4.42.0'
implementation 'net.javacrumbs.shedlock:shedlock-provider-jdbc-template:4.42.0'
Step 2: Configure the Lock Provider
The lock provider is responsible for interacting with the shared storage (the database in our case) to manage locks. Here's an example of how to configure a lock provider using Spring's JdbcTemplate:
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbc.template.JdbcTemplateLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class ShedLockConfig {
@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(
JdbcTemplateLockProvider.Configuration.builder()
.withJdbcTemplate(new JdbcTemplate(dataSource))
.usingDbTime()
.build()
);
}
}
In this example, we create a JdbcTemplateLockProvider bean that uses the provided DataSource to interact with the database. The usingDbTime() method indicates that we want to use the database's system time for lock management.
Step 3: Configure Shed Lock in Spring
Next, we need to enable Shed Lock in our Spring application. We can do this by adding the @EnableSchedulerLock annotation to our main configuration class:
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
public class AppConfig {
// Configuration code here
}
The @EnableScheduling annotation enables Spring's scheduling capabilities, and @EnableSchedulerLock enables Shed Lock. The defaultLockAtMostFor attribute specifies the maximum time a lock can be held. In this case, the lock will be automatically released after 30 seconds.
Step 4: Apply Locks to Scheduled Tasks
Now, we can apply locks to our scheduled tasks. Here's an example:
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledTaskService {
@Scheduled(fixedRate = 60000)
@SchedulerLock(name = "myScheduledTask", lockAtMostFor = "PT10M", lockAtLeastFor = "PT1M")
public void myScheduledTask() {
// Task logic here
System.out.println("Executing scheduled task...");
}
}
The @SchedulerLock annotation is used to apply a lock to the myScheduledTask method. The name attribute is a unique identifier for the lock. The lockAtMostFor attribute specifies the maximum time the lock can be held, and lockAtLeastFor specifies the minimum time the lock will be held.
Step 5: Database Setup
We need to create a table in the database to store lock information. For a MySQL database, the SQL statement to create the table is as follows:
CREATE TABLE shedlock(
name VARCHAR(64) NOT NULL,
lock_until TIMESTAMP(3) NOT NULL,
locked_at TIMESTAMP(3) NOT NULL,
locked_by VARCHAR(255) NOT NULL,
PRIMARY KEY (name)
);
For other databases like PostgreSQL, the SQL statement is similar, but the data types may need to be adjusted according to the database's specifications.
Additional Considerations for High - Availability
In high - availability systems, we need to consider a few additional factors:
- Database Replication: If you're using a replicated database, make sure that the lock table is replicated correctly. Any delay in replication can lead to multiple instances acquiring the lock simultaneously.
- Network Latency: High network latency between the application instances and the database can affect lock acquisition and release. Consider using a database that is geographically close to your application servers.
- Failure Recovery: Implement proper failure recovery mechanisms. If an instance fails while holding a lock, the lock should be released automatically after the
lockAtMostFortime has elapsed.
Related Product Links
If you're looking for high - quality locks for other applications, you might be interested in our Push Lock, Wooden Door Lock, and Round Door Lock.
Contact Us for Procurement
We understand the importance of reliable lock solutions in high - availability systems. As a Shed Lock supplier, we are committed to providing you with the best products and support. If you're interested in procuring Shed Lock for your high - availability projects or have any questions about its configuration, we encourage you to reach out. Start a conversation with us to discuss your specific needs and explore how our Shed Lock can enhance the performance and reliability of your systems.


References
- Shed Lock official documentation, "https://www.javacrumbs.net/shedlock/"
- Spring Framework documentation, "https://docs.spring.io/spring-framework/docs/current/reference/html/"
