Javatpoint Logo
Javatpoint Logo

Quartz scheduler java

Quartz scheduler java

Scheduler plays an important role in building Java applications. QuartzJobScheduling is an open-source job scheduling library. It has a rich set of features that can integrate into our Java applications virtually. We can integrate it with either a stand-alone application or the largest e-commerce system.

Quartz is used for creating complex schedules having tens-of-thousands of jobs. The quartz scheduler supports several enterprise-class features, including clustering and JTA transaction.

If our Java application has some tasks that are required to occur at a specified time or if our system recurring maintenance jobs, Quartz may be our ideal solution. These are the following uses of the Quartz scheduler in Java:

  1. Quartz scheduler allows an enterprise to schedule a job at a specified date and time.
  2. It allows us to perform the operations to schedule or unschedule the jobs.
  3. It provides operations to start or stop or pause the scheduler.
  4. It also provides reminder services.
  5. It provides maintenance of the system every business day at 11:30 PM.

Features of Quartz scheduler

The Quartz scheduler has the following features which make it so useful to integrate into Java applications:

Quartz scheduler java

1) Runtime Environment

We can run the Quartz embedded in another free-standing application and can be instantiated within the application server. As a cluster of stand-alone programs, it can also be run to execute the jobs. The Quartz can also participate in XA transactions. As a stand-alone application, the quarts can be run to use via RMI.

2) Job Scheduling

Jobs scheduling is another feature of Quartz. We can schedule the jobs by using Quartz. We schedule the jobs to run when the specified trigger is fired. The jobs are added to the scheduler once, but they are registered with multiple triggers.

3) Job execution

Any Java class that implements the Job interface is referred to as Job interface. These classes are executed by creating an instance of that Job class. The instance of that class can be instantiated by our application frameworks or Quartz. The scheduler notifies Java objects implementing TriggerListener and JobListener interfaces when a trigger is fired, and after the execution of the Jobs, these listeners are also notified.

The Jobs returns the JobCompletionCode to notify the scheduler about the success or failure of the Job.

4) Job Persistence

Quartz jobs are persistent because Quartz library has a JobStore interface, which is very helpful to store the jobs. It provides a list of mechanisms to store them in the database. Jobs and triggers that are configured as "non-volatile" are stored in the relational database by using JDBCJobStore. The Quartz library provides the RAMJobStore interface that stores all the triggers and jobs in RAM.

5) Transaction

With the help of the JobStoreCMT, Quartz participates in the JTA transaction. It manages JTA transactions to automatically happens the work performed by the Job within a JTA transaction.

6) Clustering

  • Load balancing.
  • Fail-over.

Let's take an example to understand how we can implement the scheduler application with Quartz. In our example, we fire a job that invokes a business-related task every time. The program starts the Quartz scheduler and schedule job to run every minute.

In order to create the Quartz scheduler program, we have to add the following dependencies in our POM.xml file.

CreateQuartzJob.java

Now, in order to schedule a Job, we have to create a scheduler application or Quartz scheduler application. There are five steps to schedule a job to run the task every time. These steps are as follows:

  1. In the first step, we have to initialize the scheduler instance from Quartz by using StdSchedulerFactory() method.
  2. After that, we start the scheduler instance with the Quartz API start() method.
  3. Start the scheduler instance with Quartz API start()
  4. Next, we create a scheduler trigger by using the SimpleScheduleBuilder from the Quartz TriggerBuilder with an interval of 1 minute.
  5. For the CreateQuartzJob, we build an instance of JobDetail from the Quartz JobBuilder.
  6. In the last step, we schedule a job with the instance of the JobDetail and trigger created before.

Let's follow the steps discussed above and implement the code to understand how the Quartz scheduler actually works.

QuartzScheduler.java

When we run the QuartzScheduler.java class, we will see the output like as:

Quartz scheduler java

The CreateQuartzJob repeatedly runs every 60 seconds within a different thread. The QuartzScheduler schedules the Job and gives the respective result.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA