Wednesday, April 9, 2014

Quartz - Quick guide

Quartz is an easy to use open source job scheduling library.
In this tutorial I am going to show you how to use this great tool to
  1. Schedule a job to run at a given time
  2. Repetitively run a job at given intervals

In this tutorial I'm using quartz-2.2.1.jar because currently it is the laterst stable version. You can download the library from http://quartz-scheduler.org/downloads.

Create a new Java project and add the quarts library to it. Since this version of Quartz depends on slf4j-1.6.6, you should add the slf4j-api-1.6.6.jar to your project as well. You can find this jar inside the zip file you downloaded from Quartz download page.
First I am going to write the method which should be run at the scheduled time. In order to do this we need to create a class which implements the org.quartz.Job interface. Then we should override the execute() method and include our logic there.

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;

public class PrintJob implements Job {
  @Override
  public void execute(JobExecutionContext context) throws JobExecutionException {
    System.out.println("Job Started at " + new Date());
  }
}

Below is the class which schedule the above job at a future time.
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class PrintSchedule {
  public static void main(String[] args) throws SchedulerException {
    JobBuilder jobBuilder = JobBuilder.newJob(PrintJob.class);
    JobDetail job = jobBuilder.withIdentity("myJobName", "myGroup1").build();
    Date sheduleTime = getSheduleTime();
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTriggerName",
                  "myGroup1").startAt(sheduleTime).build();
    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);
  }

  private static Date getSheduleTime() {
    //job will run after 5 seconds from current time
    return new Date(new Date().getTime() + 5000);
  }
}

Now run the PrintSchedule class and wait for 5 seconds. You will see a line similar to this printed on console.
Job Started at Wed Apr 09 17:30:42 IST 2014

Now we see how to shedule a job to run at certain intervals. Its very simple. Assume that you need to print the above line in every five seconds. Then just replace the below line in above code with
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTriggerName",
                  "myGroup1").startAt(sheduleTime).build();
with
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTriggerName", "myGroup1").withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();

That is all. Now run the PrintSchedule class again and you will see the the above line printed on console in every five seconds.

No comments:

Post a Comment