How to use Shed Lock with Quartz Scheduler?

Jun 23, 2025

Leave a message

Michael Yang
Michael Yang
I’m the CEO of Hunan Lianhu Hardware Co., leading the company into new markets while maintaining our core values of innovation and quality. It’s an exciting time to be part of this growing organization!

Hey there! As a Shed Lock supplier, I'm super stoked to walk you through how to use Shed Lock with Quartz Scheduler. It's gonna be a wild ride, but by the end of this, you'll be a pro at integrating these two powerful tools.

First off, let's talk about what Shed Lock and Quartz Scheduler are. Quartz Scheduler is a pretty well - known open - source job scheduling library in the Java world. It lets you schedule jobs at specific intervals, like running a data backup every night at 2 AM or sending out newsletters every Friday. On the other hand, Shed Lock is a library that helps you handle distributed locks. In a distributed system, you might have multiple instances of your application running, and you need to make sure that a particular job doesn't run concurrently on different instances. That's where Shed Lock comes in.

Why Combine Them?

You might be wondering, "Why do I need to use Shed Lock with Quartz Scheduler?" Well, in a distributed environment, if you have Quartz Scheduler running on multiple instances, the same job can fire up on all of them at the same time. This can lead to a whole bunch of issues, like data inconsistency or resource over - utilization. Shed Lock solves this problem by ensuring that only one instance of a job can run at a time across all your application instances.

Setting Up the Environment

Before we start integrating Shed Lock with Quartz Scheduler, we need to set up our project. If you're using Maven, you'll need to add the necessary dependencies to your pom.xml file. Here's what it might look like:

<dependency>
    <groupId>net.javacrumbs.shedlock</groupId>
    <artifactId>shedlock-spring</artifactId>
    <version>4.40.0</version>
</dependency>
<dependency>
    <groupId>net.javacrumbs.shedlock</groupId>
    <artifactId>shedlock-provider-jdbc-template</artifactId>
    <version>4.40.0</version>
</dependency>
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.3.2</version>
</dependency>

If you're using Gradle, your build.gradle file should have these dependencies:

implementation 'net.javacrumbs.shedlock:shedlock-spring:4.40.0'
implementation 'net.javacrumbs.shedlock:shedlock-provider-jdbc-template:4.40.0'
implementation 'org.quartz-scheduler:quartz:2.3.2'

Configuring Shed Lock

Once we've added the dependencies, we need to configure Shed Lock. We'll start by creating a lock provider. For simplicity, let's use a JDBC - based lock provider. Here's how you can do it in Java:

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 using a JdbcTemplate and the provided DataSource. The usingDbTime() method ensures that we use the database's time for lock management.

Integrating with Quartz Scheduler

Now comes the fun part - integrating Shed Lock with Quartz Scheduler. We'll create a simple job using Quartz and then use Shed Lock to make sure it runs only once across all instances.

First, let's create a Quartz job class:

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyQuartzJob implements Job {
    private static final Logger logger = LoggerFactory.getLogger(MyQuartzJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        logger.info("MyQuartzJob is running!");
        // Your job logic goes here
    }
}

Next, we'll schedule this job using Quartz and add Shed Lock to it. We'll use Spring's @Scheduled annotation along with Shed Lock's @SchedulerLock annotation.

Self Locking Door6-3

import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduledJobService {

    @Autowired
    private Scheduler scheduler;

    @Scheduled(fixedRate = 60000) 
    @SchedulerLock(name = "myQuartzJobLock", lockAtMostFor = "59s", lockAtLeastFor = "30s")
    public void scheduleQuartzJob() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(MyQuartzJob.class)
               .withIdentity("myQuartzJob", "group1")
               .build();

        Trigger trigger = TriggerBuilder.newTrigger()
               .withIdentity("myQuartzJobTrigger", "group1")
               .startAt(new Date())
               .build();

        scheduler.scheduleJob(jobDetail, trigger);
    }
}

In this code, the @Scheduled annotation schedules the scheduleQuartzJob method to run every 60 seconds. The @SchedulerLock annotation ensures that only one instance of this job can run at a time. The name attribute is the name of the lock, lockAtMostFor is the maximum time the lock can be held, and lockAtLeastFor is the minimum time the lock should be held.

Other Considerations

There are a few other things you should keep in mind when using Shed Lock with Quartz Scheduler.

  • Lock Timeout: Make sure to set appropriate values for lockAtMostFor and lockAtLeastFor. If lockAtMostFor is set too short, the lock might be released before the job is finished, leading to concurrent execution. If it's set too long, it can cause unnecessary delays.
  • Error Handling: You should have proper error handling in place for both Quartz and Shed Lock. If there's an error in acquiring the lock or running the job, your application should handle it gracefully.

Related Products

If you're interested in other types of locks, we also have some great options. Check out our Wooden Door Lock, Quarter Turn Lock, and Self Locking Door products. They're high - quality and perfect for various security needs.

Contact for Purchase

If you're looking to integrate Shed Lock with your Quartz Scheduler setup or interested in any of our other lock products, we'd love to have a chat. Reach out to us to discuss your requirements and get a quote. Our team of experts is ready to assist you in finding the best solutions for your business.

References

  • Quartz Scheduler official documentation
  • Shed Lock official documentation

So, that's it! You now know how to use Shed Lock with Quartz Scheduler. It might seem a bit complex at first, but with a little practice, you'll be able to handle distributed job scheduling like a champ.

Send Inquiry