Getting started with Quartz and Spring Boot

This quick tutorial will teach you how to use Quartz Scheduler in a Spring Boot application in less than 5 minutes!

Start by creating your project with the spring shell so to include quartz, jpa and h2 dependencies (replace with your actual JDBC driver implementation):

spring init -dquartz,h2,jpa demo-quartz  

Here is the list of dependencies that will be added:

<?xml version="1.0" encoding="UTF-8"?><project>
      
   <dependencies>
               
      <dependency>
                      
         <groupId>org.springframework.boot</groupId>
                      
         <artifactId>spring-boot-starter-data-jpa</artifactId>
                  
      </dependency>
               
      <dependency>
                      
         <groupId>org.springframework.boot</groupId>
                      
         <artifactId>spring-boot-starter-quartz</artifactId>
                  
      </dependency>
                
      <dependency>
                      
         <groupId>org.quartz-scheduler</groupId>
                      
         <artifactId>quartz</artifactId>
                  
      </dependency>
                 
      <dependency>
                      
         <groupId>com.h2database</groupId>
                      
         <artifactId>h2</artifactId>
                  
      </dependency>
               
      <dependency>
                      
         <groupId>org.springframework.boot</groupId>
                      
         <artifactId>spring-boot-starter-test</artifactId>
                      
         <scope>test</scope>
                  
      </dependency>
           
   </dependencies>
     
</project>

Now the classes. Let’s add first two Job classes that will be triggered by Quartz scheduler:

package com.masterspringboot.quartz.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class JobOne implements Job {
  @Override
  public void execute(JobExecutionContext context) {
    System.out.println("Hello from Job One!");
  }
}

And the other one:

package com.masterspringboot.quartz.job;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

public class JobTwo implements Job {
  @Override
  public void execute(JobExecutionContext context) {
    JobDataMap jobDataMap = context.getMergedJobDataMap();
    String data = jobDataMap.getString("somedata");
    System.out.println("Job Two fired with data: " + data);
  }
}

Now a @Configuration class that will create the triggers for the execution of Jobs:

package com.masterspringboot.quartz.job;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.UUID;

@Configuration
public class ConfigureQuartzJob {
  @Bean
  public JobDetail jobADetails() {
    return JobBuilder.newJob(JobOne.class).withIdentity("sampleJobA").storeDurably().build();
  }

  @Bean
  public Trigger jobATrigger(JobDetail jobADetails) {
    return TriggerBuilder.newTrigger()
        .forJob(jobADetails)
        .withIdentity("TriggerA")
        .withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * ? * * *"))
        .build();
  }

  @Bean
  public JobDetail jobBDetails() {
    return JobBuilder.newJob(JobTwo.class).withIdentity("sampleJobB").storeDurably().build();
  }

  @Bean
  public Trigger jobBTrigger(JobDetail jobBDetails) {
    JobDataMap jobDataMap = new JobDataMap();
    jobDataMap.put("somedata", UUID.randomUUID().toString());
    return TriggerBuilder.newTrigger()
        .forJob(jobBDetails)
        .withIdentity("TriggerB")
        .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * ? * * *"))
        .usingJobData(jobDataMap)
        .build();
  }
}

Please notice we are using a CronScheduleBuilder with a Cron expression to fire the jobs. We also added for the TriggerB a sample JobData into it.

That’s all. Include a Main class as well:

package com.masterspringboot.quartz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SampleQuartzApplication {
  public static void main(String[] args) {
    SpringApplication.run(SampleQuartzApplication.class, args);
  }
}

In order to choose where jobs are stored, we will define “jdbc” as storage. It will use H2 as option:

spring.quartz.job-store-type=jdbc 
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO 
spring.quartz.properties.org.quartz.threadPool.threadCount = 5

If you prefer an in-memory storage, you can opt for:

org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore 
spring.quartz.job-store-type=memory

That’s all! run your application and you will see in the Console the logs from the jobs:

Hello from Job One! Hello from Job One! Job data: b5df5e15-de9e-47d4-aa43-28cb0ee8e754 Hello from Job One! Hello from Job One! Hello from Job One!

Using another database for Quartz

Using another database requires adding the right dependency in your project, for example “mysql”. Here is a sample Quartz configuration which uses mysql:

spring.datasource.platform=org.hibernate.dialect.MySQL5Dialect 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
spring.datasource.url=jdbc:mysql://localhost:3306/quartz_demo_db 
spring.datasource.username=root 
spring.datasource.password=admin 
spring.jpa.open-in-view=false 
spring.jpa.show-sql=true 
spring.quartz.job-store-type=jdbc 
spring.quartz.jdbc.initialize-schema=never

Source code for this article: https://github.com/fmarchioni/masterspringboot/tree/master/quartz/quartz-example-master

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon