Distributed job scheduler for spring boot using Quartz scheduler

Quartz. http://www.quartz-scheduler.org/

Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application — from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as support for JTA transactions and clustering.

Hope you guys are well aware of Spring boot.

Prerequisites:
Java 11, MySql, mvn, favourite IDE

Let’s head to the implementation.

Clone the above example from GITHUB.

Import it to your favourite IDE, in my case it’s IntelliJ IDE.

Distributed quartz is synchronised using database, in this case mysql. So head to mysql and create a database. Run the below query. <quartaz_tables.sql> can be found in the above repository.

“mysql -u<db_user_name> -p <quartz_database> < quartz_tables.sql”

it prompts database password, enter the password.

in the IDE terminal. Run “mvn clean install”

Run the above application to quickly try out.

curl — location — request POST ‘localhost:8100/messages/schedule-visibility’ \

— header ‘Content-Type: application/json’ \

— data-raw ‘{

“content”:”how are you”,

“visible”:”false”,

“makeVisibleAt”:”1626684420000"}’

https://www.epochconverter.com/
Head to this site and get the time at which you want to run the job and change makeVisibleAt parameter.

2021–07–19 14:17:00.007 INFO 20573 — — [eduler_Worker-3] com.quartz.quartzeg.Job.MessageJob : Executing job for message id <id from DB>

You will be able to see something like this at the scheduled interval.

So, Congratulations you’ve set up your distributed quartz scheduler, however, you’ve tried this in one instance scenario.

Head to this and run service on two different ports.
https://preetham-umarani.medium.com/run-two-configurations-on-intellij-ide-76334b0b1a3a

Ok, getting to the basics now.

In resources, you’ll find .properties files.

  • Quartz.properties
  • application.properties, which we’ll are aware of.

Quartz.properties has self-explanatory properties from setting up datasources to configuring scheduler.

Overview of Quartz Scheduler’s APIs and Terminologies

1. Scheduler

The Primary API for scheduling, unscheduling, adding, and removing Jobs.

2. Job

The interface to be implemented by classes that represent a ‘job’ in Quartz. It has a single method called execute() where you write the work that needs to be performed by the Job.

3. JobDetail

A JobDetail represents an instance of a Job. It also contains additional data in the form of a JobDataMap that is passed to the Job when it is executed.

Every JobDetail is identified by a JobKey that consists of a name and a group. The name must be unique within a group.

4. Trigger

A Trigger, as the name suggests, defines the schedule at which a given Job will be executed. A Job can have many Triggers, but a Trigger can only be associated with one Job.

Every Trigger is identified by a TriggerKey that comprises of a name and a group. The name must be unique within a group.

Just like JobDetails, Triggers can also send parameters/data to the Job.

5. JobBuilder

JobBuilder is a fluent builder-style API to construct JobDetail instances.

6. TriggerBuilder

TriggerBuilder is used to instantiate Triggers.

Happy Coding! Till next article, Have fun. BBYE!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store