Working with Quartz Scheduler using Spring Boot

Shubham Wankhede
5 min readMar 18, 2023

if you are working in Software Development for long period of time it’s most likely that you might have heard about quartz scheduler, yes as the name says it is used to schedule and execute a task/job at a specific date and time.

well if you have heard about quartz scheduler it’s impossible that you have not heard about spring boot scheduler, so what’s the difference between them and when to use which scheduler

before we get into any heated discussion about which scheduler to use either Spring Boot Scheduler or Quartz Scheduler let me highlight few things

as we know spring boot provides abstraction on most of the open source libraries available to make simplest use of them, same thing is true with Spring Boot Scheduler as well

Yes, Spring Boot Scheduler library uses Quartz Scheduler library internally for scheduling a task, if you look at the TaskScheduler or @Scheduled annotation for which spring creates a Scheduler object and this scheduler object is implemented using SchedulerFactoryBean which uses Quartz Library

so the main point here is instead of comparing which library is better, it’s best that we look into which one to use when i.e. either Spring Boot Scheduler or to use Quartz Library directly

Simply putting in words you can use Spring Boot Scheduler most of time for interval job execution

we can use Quartz Scheduler when

  1. when the in scheduling you have to execute task at a irregular interval
  2. when you have to decide the specific date and time to start the task
  3. you want misfire handling, job persistance etc.

on how to work with spring boot scheduler you can follow this blog :

now let’s understand how to directly work with Quartz Scheduler library using Spring Boot, before we get started let’s look at different components which we need or we can use while working with Quartz Scheduler

we are going to use QuartzScheduler to schedule a job/task.

the steps we need to follow and components we need to get started with most schedule a job is

  1. writing job store configuration
  2. Creating Job Bean
  3. Creating Trigger and Job Details
  4. Scheduler Job using Quartz Scheduler

let’s understand each steps

Step 1: Job Store Configuration

configure below configurations in

#store type either in-memory or jdbc to store in DB
#to create tables used to store job and other scheduling activities

#datasource to store jobs in DB if store type is jdbc



# if you want to provide multiple threads to run jobs

let’s understand few of these configuration

spring.quartz.job-store-type=jdbc property is used to define the job store we want to use in this case the value jdbcrepresents that we want to store job, triggers etc in Datasource configured

we can also use value as memory which is used to store job in in-memory

we need Quartz tables to store jobs in database we can use initilize property spring.quartz.jdbc.initialize-schema=always to automatically create the tables required is used to automatic

if you want to create tables manually you can use the official SQL script provided by quartz (

we can also configure the number of concurrent threads we can use using

aside from this we have to configure DB properties where we are going to store the jobs it can H2 DB or others.

Step 2: Creating Job Bean

to create a job we have to create a .java file extends it with QuartzJobBean

public class RecordCreationJob extends QuartzJobBean {

private RecordRepository recordRepository;

protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
String dataPassed = context.getMergedJobDataMap().get("dataPassed").toString();
System.out.println("Data Passed From Job Details : " + dataPassed);

System.out.println("Print Records : ");

in executeInternal method we have to write the logic we want to execute at scheduled period

Step 3: Creating Trigger and Job Details

we are going to create a service class from where we are going to schedule a job and since to schedule a job we need Trigger and Job Details we are going to create them in service class only


public JobDetail buildJobDetails(){
//create map to store key-value (can be received from request) which can be passed to job
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("dataPassed", "Data can be passed to job it can be request Data or other" );

return JobBuilder.newJob(RecordCreationJob.class)
.withIdentity(UUID.randomUUID().toString(),"record-jobs") // unique job key
.withDescription("Record Job Description")
.usingJobData(jobDataMap) // values to pass to job
.storeDurably(true) //false - delete job once job executed

JobDataMap contains some properties we want to pass to our job which we are going to execute on schedule, these properties can be received from request when we are scheduling an event.

Identity of Job need to unique so as to uniquely identify them in table

if we want to delete the job from Job Store once we are done executing it then we can keep storeDurably(false)


public Trigger buildTrigger(JobDetail jobDetail){
ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDate.parse("2023-04-01"),
LocalTime.of(8,30), ZoneId.of("Asia/Kolkata"));

return TriggerBuilder.newTrigger()
.withIdentity("Records Trigger Description")
// Some Scheduler Example

--> cron scheduler
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 16 * * ?")



since the job can be executed in any time zone we are providing the zoned time to start time of job

Note: one job can have multiple trigger but one trigger can only have one job

we can schedule job using cron expression or in interval for Quartz library has provided different schedules

Step 4: Scheduler Job using Quartz Scheduler

last but not least is to schedule a job using JobDetails and Trigger

public class RecordService {

private Scheduler quartzScheduler;

public void scheduleRecordPrinter(){
try {
JobDetail jobDetail = .buildJobDetails();
Trigger trigger = buildTrigger(jobDetail);
} catch (SchedulerException e) {
throw new RuntimeException(e);

//Implementation of trigger and jobdetails are mentioned above
public Trigger buildTrigger(JobDetail jobDetail){ .... }
public JobDetail buildJobDetails(){ ... }


here you can see I’ve autowired the Scheduler spring bean which is QuartzScheduler implementation

we need to use scheduleJob method and then pass trigger and job detail to schedule a job

Calling Scheduler

now we can call this scheduler from main method or if you are scheduling an event and are passing details start date and job specific data we call it from rest endpoint method

we are going to test it from main method

public class QuartzSchedulerApplication {

public static void main(String[] args) {
ApplicationContext context =, args);

RecordService recordService = context.getBean(RecordService.class);

with this we are done with simple quartz scheduler application.