Configuring Quartz Scheduler to Run in Clustered Environment

The goal of running a Quartz job in the clustered environment is NOT to have duplicate running jobs. The triggered job should run just one time regardless of the number of nodes in the clustered environment.

  1. Download Quartz and extract the file.
  2. Navigate to quartz-x.x.x -> docs -> dbTables and run the database SQL script to create the Quartz tables.

    |- docs
       |- dbTables
          |- tables_<database>.sql <- Pick one that matches your database
       |- images
    |- examples
    |- javadoc
    |- lib
    |- licenses
    |- src

  3. Add the Quartz dependency in pom.xml:-


  4. Create file under src/main/resources with the following configuration:-

    # Configure Main Scheduler Properties
    org.quartz.scheduler.instanceId = AUTO
    org.quartz.scheduler.makeSchedulerThreadDaemon = true
    # Configure ThreadPool
    org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount = 1
    org.quartz.threadPool.makeThreadsDaemons = true
    # Configure JobStore
    org.quartz.jobStore.isClustered = true

    Remember to change org.quartz.jobStore.driverDelegateClass value to match your database type. In my case, I’m using MS SQL Server.

  5. I want to be able to autowire Spring beans in my Quartz job. So, I created a custom job factory called AutowiringSpringBeanJobFactory:-

    public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory 
        implements ApplicationContextAware {
        private transient AutowireCapableBeanFactory beanFactory;
        public void setApplicationContext(final ApplicationContext context) {
            beanFactory = context.getAutowireCapableBeanFactory();
        protected Object createJobInstance(final TriggerFiredBundle bundle) 
            throws Exception {
            final Object job = super.createJobInstance(bundle);
            return job;

  6. In my Quartz job, I can reuse my existing Spring bean by autowiring it:-

    public class MyJob implements Job {
        private MyService myService;
        public void execute(JobExecutionContext jobExecutionContext) 
            throws JobExecutionException {
            System.out.println("Message: " + myService.getHelloWorld());

  7. In this example, I’m creating a Java-based Spring configuration called QuartzConfig:-

    public class QuartzConfig {
        // this data source points to the database that contains Quartz tables
        private DataSource dataSource;
        private PlatformTransactionManager transactionManager;
        private ApplicationContext applicationContext;
        public SchedulerFactoryBean quartzScheduler() {
            SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
            // Custom job factory of spring with DI support for @Autowired
            AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
            Trigger[] triggers = {
            return quartzScheduler;
        public JobDetailFactoryBean processMyJob() {
            JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
            return jobDetailFactory;
        // Configure cron to fire trigger every 1 minute
        public CronTriggerFactoryBean processMyJobTrigger() {
            CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
            cronTriggerFactoryBean.setCronExpression("0 0/1 * * * ?");
            return cronTriggerFactoryBean;
        public Properties quartzProperties() {
            PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
            propertiesFactoryBean.setLocation(new ClassPathResource(""));
            Properties properties;
            try {
                properties = propertiesFactoryBean.getObject();
            catch (IOException e) {
                throw new RuntimeException("Unable to load", e);
            return properties;

3 thoughts on “Configuring Quartz Scheduler to Run in Clustered Environment

    1. The database manages the semaphore to ensure only one running node is executing the triggerred job. If you run this SQL query select * from dbo.QRTZ_TRIGGERS, you will see which job is currently running, when it will run again, etc.

Leave a Reply to Choon-Chern Lim Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s