Quartz Job Scheduler
- Posted by Yomna Anwar
- On February 21, 2022
Quartz is a job scheduling open-source library that can be used within any Java application of various scales ranging from small projects to large scale systems. Quartz can be used to schedule any number of jobs even thousands of them; jobs that are defined as Java classes to be executed.
When to use it?
Quartz would be a good choice if your application needs to execute recurrent jobs or if it needs to execute a specific task at a particular time instance.
Uses of job scheduling with Quartz:
• Schedule a job to update status in the database based on time.
• Firing job execution at a certain time instance used in reminder services.
• Parallel processing: when performing operations on a large amount of data, data can be divided into chunks where each chunk can be processed individually then quartz can schedule these jobs concurrently.
Quartz scheduler components
The main interfaces of the API are:
• Scheduler – the main API for interacting with the scheduler of the framework.
• Job – an interface implemented by components to be executed.
• JobDetail – used to declare instances of Jobs.
• Trigger – a component that informs the schedule when to execute a given Job.
• JobBuilder – used to build JobDetail instances, which declare instances of Jobs.
• TriggerBuilder – used to build Trigger instances.
Quartz relies on a multi-threaded architecture. When started, the framework initializes a group of threads that are used by the Scheduler to execute Jobs concurrently.
Features
Job Scheduling
Jobs can be scheduled for execution to run on firing Triggers. Triggers can be instantiated with the subsequent triggering time options:
• At a particular time of day, week, month, or year.
• Can be scheduled periodically, allowing you to exclude specific days (for example can be triggered daily except Friday and Saturday).
• Repeatedly for a fixed number of times.
• Repeatedly till reaching a particular time/date.
• Repeatedly infinitely.
• Triggers can be delayed for a certain amount of time.
Jobs can also be named and may be grouped together by giving them a certain group, as well as triggers, in order to categorize them within the scheduler. A job can be added once in quartz schedular, but it can be fired multiple times using multiple Triggers.
Job Execution
• Jobs logic can be implemented in the Java class that implements the simple Job interface.
• Jobs can be instantiated manually through the application before adding them to the job queue to be scheduled.
• When a Trigger fires, the scheduler broadcasts the events to JobListener and TriggerListener. These listeners can also be notified when the Job has finished execution.
• After the job completion, each job returns a Job completion status code which indicates the success or failure of the job execution. Based on the Job completion status code, the scheduler can decide the following action it should perform. For example, the scheduler could restart executing a job.
Job Persistence
• Quartz is also capable of storing jobs, which can be done by implementing JobStore interface.
• Triggers and jobs can be stored in the database by setting them as “non-volatile”, they will be stored in a database.
• Jobs and Triggers can be stored in RAM, when saving them in the database is not needed.
Quartz scheduling vs java scheduler annotation
@Scheduler annotation can be used to execute functions every X second or on a cron schedule whereas Quartz can provide some more complex features that don’t exist in @Scheduler. For example:
1 – We can set a scheduler in stand-by mode and reschedule it.
2 – Shutting down a scheduler before execution of a job or after execution by configuring the scheduler shutdown property scheduler. shutdown(true); and scheduler. shutdown(false);
3 – Job can be triggered immediately or can be stored for later use and can be triggered at any instance of time.
4 – Add a replacement job to the scheduler, instructing it to “replace” the currently running job with the given name and group.
Conclusion
In this blog, we have shown the uses of Quartz scheduler, when to be used and its additional features over java scheduler.
Quartz can be used to create simple or complex schedules for executing hundreds or thousands of jobs.