How to use Shed Lock with Java 11?

May 16, 2025

Leave a message

Tom Zhang
Tom Zhang
I’m the head of manufacturing at Lianhu, overseeing production processes to ensure efficiency and sustainability. My goal is to maintain our reputation for high-quality hardware products while reducing environmental impact.

Hey there! As a Shed Lock supplier, I'm stoked to share with you how to use Shed Lock with Java 11. Shed Lock is an awesome tool that helps you handle distributed locks in a Java application. It's super useful when you've got multiple instances of your app running and you need to make sure that certain tasks are executed by only one instance at a time.

What is Shed Lock?

Before we dive into the how - to, let's quickly talk about what Shed Lock is. Shed Lock is a Java library that provides a simple way to implement distributed locks. It supports various storage mechanisms like JDBC, Redis, MongoDB, etc. This flexibility makes it a great choice for different types of projects.

Setting up the Project

First things first, you need to set up your Java 11 project. If you're using Maven, add the Shed Lock dependencies to your pom.xml. Here's an example of how to add the JDBC implementation:

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

If you're using Gradle, add these lines to your build.gradle:

implementation 'net.javacrumbs.shedlock:shedlock-provider-jdbc-template:4.42.0'
implementation 'net.javacrumbs.shedlock:shedlock-spring:4.42.0'

Configuring the Lock Provider

Once you've added the dependencies, you need to configure the lock provider. Let's assume we're using a JDBC database. You'll need to create a LockProvider bean in your Spring configuration.

7-3
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.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()
        );
    }
}

This code creates a LockProvider bean that uses the JDBC template to interact with the database. The usingDbTime() method tells Shed Lock to use the database's time for lock management.

7-2

Using Shed Lock in Your Service

Now that the configuration is done, let's see how to use Shed Lock in your service. Suppose you have a service method that you want to lock. You can use the @SchedulerLock annotation provided by Shed Lock.

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

@Service
public class MyService {

    @Scheduled(fixedRate = 60000)
    @SchedulerLock(name = "myTaskLock", lockAtMostFor = "59s", lockAtLeastFor = "30s")
    public void myTask() {
        // Your task logic goes here
        System.out.println("Running my task...");
    }
}

In this example, the @Scheduled annotation makes the myTask() method run every 60 seconds. The @SchedulerLock annotation ensures that only one instance of the application can execute this method at a time. The name attribute is a unique identifier for the lock. The lockAtMostFor attribute specifies the maximum time the lock can be held, and the lockAtLeastFor attribute specifies the minimum time the lock should be held.

Different Lock Providers

As I mentioned earlier, Shed Lock supports different lock providers. Let's take a look at using Redis as a lock provider.

First, add the Redis dependency to your project. If you're using Maven:

<dependency>
    <groupId>net.javacrumbs.shedlock</groupId>
    <artifactId>shedlock-provider-redis-spring</artifactId>
    <version>4.42.0</version>
</dependency>

And for Gradle:

implementation 'net.javacrumbs.shedlock:shedlock-provider-redis-spring:4.42.0'

Then, configure the Redis lock provider in your Spring configuration:

import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

@Configuration
public class RedisShedLockConfig {

    @Bean
    public LockProvider lockProvider(RedisConnectionFactory redisConnectionFactory) {
        return new RedisLockProvider(redisConnectionFactory);
    }
}

You can then use the same @SchedulerLock annotation in your service methods, and Shed Lock will use Redis to manage the locks.

8-3

Troubleshooting

Sometimes, things might not go as planned. If you're having issues with Shed Lock, here are some common problems and solutions.

  • Lock not being released: Check the lockAtMostFor attribute. If it's set too long, the lock might not be released in a timely manner. You can also check if there are any exceptions in your code that prevent the lock from being released properly.
  • Multiple instances executing the locked task: Make sure that all instances are using the same lock provider and configuration. Also, check if the database or Redis server is accessible from all instances.

Other Types of Locks We Offer

We're not just about Shed Locks. We also offer a variety of other locks that might be useful for different applications. For example, check out our Side Door Lock, which is perfect for securing side doors. Our Self Locking Door is a great option if you want a door that locks automatically. And if you need to secure a storeroom, our Storeroom Lock is just what you need.

Conclusion

Using Shed Lock with Java 11 is a great way to manage distributed locks in your application. It's easy to set up and provides a lot of flexibility with different lock providers. Whether you're using JDBC, Redis, or another supported storage mechanism, Shed Lock has got you covered.

If you're interested in purchasing our Shed Locks or any of our other locking solutions, we'd love to have a chat with you. Reach out to us to start the procurement discussion and find the best locking solution for your needs.

7-6

References

  • Shed Lock official documentation
  • Spring Framework documentation
Send Inquiry