How to use Shed Lock with Spring Boot Actuator?

Aug 14, 2025

Leave a message

Sophia Wu
Sophia Wu
As the marketing manager, I work on branding and market expansion for Lianhu. I’m dedicated to showcasing our innovative spirit and commitment to excellence in the global market.

ShedLock is a library that allows you to synchronize the execution of scheduled tasks in a distributed environment, ensuring that only one instance of a task runs at a time across multiple nodes. Spring Boot Actuator, on the other hand, provides production-ready features to help you monitor and manage your application. In this blog post, we'll explore how to use Shed Lock with Spring Boot Actuator, and as a Shed Lock supplier, I'll guide you through the process step - by - step.

Prerequisites

Before we start, make sure you have a Spring Boot project set up. You'll need to add the following dependencies to your pom.xml if you're using Maven:

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Setting up Shed Lock

First, we need to configure Shed Lock. We'll use the JDBC template as the lock provider in this example.

11-22-2

Database Setup

Create a table in your database to store the lock information. For a MySQL database, the SQL script would be:

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

Configuration Class

Create a configuration class to set up Shed Lock:

import net.javacrumbs.shedlock.core.LockProvider;
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 LockProvider lockProvider(DataSource dataSource) {
        return new JdbcTemplateLockProvider(
                JdbcTemplateLockProvider.Configuration.builder()
                        .withJdbcTemplate(new JdbcTemplate(dataSource))
                        .usingDbTime() 
                        .build()
        );
    }
}

In the above code, @EnableSchedulerLock enables the Shed Lock functionality, and defaultLockAtMostFor sets the default maximum time a lock can be held. The lockProvider bean is responsible for providing the lock mechanism using the JDBC template.

Creating a Scheduled Task

Now, let's create a simple scheduled task that will be locked using Shed Lock.

import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTask {

    @Scheduled(fixedRate = 5000) 
    @SchedulerLock(name = "scheduledTask", lockAtMostFor = "PT10S", lockAtLeastFor = "PT5S")
    public void runTask() {
        System.out.println("Running scheduled task...");
    }
}

The @Scheduled annotation defines the schedule of the task, and the @SchedulerLock annotation ensures that only one instance of the task runs at a time. The name attribute is used to identify the lock, lockAtMostFor sets the maximum time the lock can be held, and lockAtLeastFor sets the minimum time the lock should be held.

Integrating with Spring Boot Actuator

Spring Boot Actuator provides several endpoints that can be used to monitor and manage the application. We can use the health endpoint to check if the Shed Lock is working correctly.

Custom Health Indicator

Create a custom health indicator to check the status of Shed Lock:

import net.javacrumbs.shedlock.core.LockProvider;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class ShedLockHealthIndicator implements HealthIndicator {

    private final LockProvider lockProvider;

    public ShedLockHealthIndicator(LockProvider lockProvider) {
        this.lockProvider = lockProvider;
    }

    @Override
    public Health health() {
        try {
            boolean isLockable = lockProvider.lock("healthCheckLock", java.time.Duration.ofSeconds(10)).isPresent();
            if (isLockable) {
                return Health.up().build();
            } else {
                return Health.down().build();
            }
        } catch (Exception e) {
            return Health.down(e).build();
        }
    }
}

This custom health indicator tries to acquire a lock named healthCheckLock. If the lock can be acquired, it means the Shed Lock mechanism is working correctly, and the health status is set to UP. Otherwise, it is set to DOWN.

Testing the Setup

To test the setup, start your Spring Boot application. You can access the Actuator endpoints at http://localhost:8080/actuator. Check the health endpoint to see if the Shed Lock is working as expected.

Additional Considerations

  • Lock Providers: Besides the JDBC template, Shed Lock supports other lock providers such as Redis, Zookeeper, and more. You can choose the one that best fits your application's needs.
  • Error Handling: Make sure to handle errors properly when using Shed Lock. For example, if the database connection is lost, the lock mechanism may fail.

Related Products

As a Shed Lock supplier, we also offer a range of high - quality door locks. Check out our Round Door Lock, Push Lock, and Push Door Lock for your security needs.

Contact for Purchase

If you're interested in our Shed Lock products or any of our door locks, feel free to reach out to us for a purchase consultation. We're here to help you find the best solutions for your applications and security requirements.

References

  • Shed Lock official documentation
  • Spring Boot Actuator official documentation
  • Spring Framework official documentation
Send Inquiry