Sunday, February 15, 2015

Basic Quartz Scheduler Tutorial

In this post the basic usage of quartz scheduler is shown. You can use quartz scheduler to perform and monitor repetitive tasks easily in Java.

Basic steps for executing jobs using quartz scheduler

  1. Step 1: Create a Job Class that implements the quartz job interface
  2. Step 2: Create a handle to the job in a separate class, e.g. Main
  3. Step 3: Create a Trigger, independent of the job, specify how often you want a job to be executed
  4. Step 4: Create a Scheduler and associate a trigger with a job
  5. Step 5: Start the scheduler which will trigger the job based on the configuration present in the trigger
In short, you (i) create a Job(s), (ii) create a Trigger(s), and (iii) create a Scheduler = Job + Trigger. You start the scheduler to run the jobs.

Sample Triggers,

This trigger will run any job, once, now.
Trigger triggerNow = TriggerBuilder.newTrigger()                .withIdentity("NowTrigger").startNow().build();
 This trigger will run any job every 1 second, indefinitely.
Trigger triggerEverySecond = TriggerBuilder.newTrigger().withIdentity("NameOneSecondTrigger", "GroupForever").withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever()).build();

No comments:

(c) Jyotirmaya Nanda 2012