Spring Boot & Quartz Scheduler

Necmeddin Tapan
turkcell
Published in
3 min readOct 9, 2023

Quartz is an open source job scheduling framework that can be integrated within Java applications. It has powerful features and provides these features with simple usage and integration. You can run your jobs with simple or complex schedules, for unlimited or limited times.

In order to use Quartz in your Spring Boot project, you must add below dependency to your pom.xml.

Quartz can store job and scheduling information in a relational database or RAM. With RAM option, any information will be deleted with application restart. So, in this example, we will use database as jobStore, with job-store-type property.

Since jdbc is used as job store type, there must be quartz tables in your database. There are two options, in first one, quartz can automatically create tables with initialize-schema:always property. But, in this case, with every restart of application, all quartz tables dropped and created repeatedly.

In second option, you should create quartz tables manually. To do this, initialize-schema:never property should be set.

Now, you must run your database schema scripts, as seen one example above. You can find scripts in the below link. Scripts change according to the relational database you have chosen.

After you run the database scripts, Quartz table should be listed like this;

Quartz Tables

Note that, QRTZ_ prefix is default, it can be changed from quartz properties

You should also add jobStore driverDelegateClass to your properties file. This property changes according to chosen database.

“org.quartz.impl.jdbcjobstore.StdJDBCDelegate” option is applicable for nearly all database options, in case of any error, you should change according to your database in the below configuration link:

Basic configuration is enough to create scheduled jobs for Quartz. You can find other options in Configuration Reference page.

Now, configuration is ready for creating jobs and triggers for scheduled tasks. Firstly, you have to implement “Job” interface and override “execute” method. “execute” method in the jobExecutor class is fired in the scheduled time and runs the code for the scheduled job.

MyJobExecutor sample class, contains “JobDataMap” in the execute method. JobDataMap is responsible to keep data in order to use in your task. You can get stored data with the given key value, while creating the JobDataMap object. We will see in the below sample how to store data in JobDataMap.

It is time to create job and trigger to schedule. “Scheduler” interface can be used by just adding “Autowired” annotation.

Below code sample shows how to create “JobDetail” object. As you saw earlier, you should store data to “JobDataMap” and use this map when creating the JobDetail object. MyJobExecutor class which implements “Job” interface, should be used here. There is also “identity” for the job, like primary key for a job and will be used to call the created job. Samples about how to use will be shown in the next tutorial.

After creating Job, you must create “Trigger”, in order to define the schedule of the job. As seen in the below sample code, when creating the Trigger object, you can specify when to start for running the job, period and how many times you want to run the job. Like in the Job, there is also identity to specify unique trigger.

Now, you have Job and Trigger objects, and as seen above sample, you schedule by just “scheduler.scheduleJob(job,trigger)”. After that, job and trigger objects were saved to “Quartz tables”, and job will be fired in the scheduled times.

That is it, you can create Jobs and Triggers, and schedule dynamically your tasks with complex options.

Note that, in order to use “newJob()” and “newTrigger()”, you should use static import as seen below sample.

Hope, it will be helpful…

Note that, for advanced Trigger options you can visit tutorial for “More About Triggers” at Quartz-Scheduler:

Trigger Options for Quartz-Scheduler

--

--