Quartz Scheduler - SimpleScheduleBuilder CronScheduleBuilder In Java - Java @ Desk

Saturday, November 29, 2014

Quartz Scheduler - SimpleScheduleBuilder CronScheduleBuilder In Java

Quartz Scheduler - SimpleScheduleBuilder CronScheduleBuilder In Java

Quartz Scheduler in Java helps in scheduling a job that run at specific intervals. The interval can vary from seconds to minutes to years.

Some examples of time scheduling:
1) To run a job every 5 minutes
2) To run a job everday at 3 PM
3) To run a job at 5:45 AM only every Saturday and Monday
4) To run a job at 10 AM and 5 PM every Monday

This is commonly used to run background application that need to run automatically without any manual intervention once started. As the schedule hits, the job gets activated and perform the task.

There are 2 variations
1) SimpleScheduleBuilder - Used to run job every with simple expressions. For example, run every 30 seconds or 45 minutes, etc.
2) CronScheduleBuilder - Used to run the job with complex scheduling as mentioned in above examples.

Sample Code :
JobImplementation.java

package com.quartz.scheduler;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class JobImplementation implements Job {

 @Override
 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
  System.out.println(" Job Detail - " + new Date());
 }

}


Example of Simple Scheduler

package com.quartz.scheduler;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzTest {
 public static void main(String[] args) throws SchedulerException {

  // specify the job' s details..
  JobDetail job = JobBuilder.newJob(JobImplementation.class)
    .withIdentity("JobImplementation").build();

  // Run the Job every 30 seconds
  Trigger simpleScheduleTrigger = TriggerBuilder.newTrigger().withIdentity(
    "JobImplementation Trigger").withSchedule(
    SimpleScheduleBuilder.simpleSchedule()
      .withIntervalInSeconds(30).repeatForever()).build();
  
  SchedulerFactory schFactory = new StdSchedulerFactory();
  Scheduler sch = schFactory.getScheduler();
  sch.start();
  sch.scheduleJob(job, simpleScheduleTrigger);
 }
}


Output SimpleSchedule :

log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
 Job Detail - Sat Nov 29 13:59:28 GMT+05:30 2014
 Job Detail - Sat Nov 29 13:59:58 GMT+05:30 2014
 Job Detail - Sat Nov 29 14:00:28 GMT+05:30 2014




Example of CornScheduler

JobImplementation Every Minute Using CronSchedule

package com.quartz.scheduler;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzTest {
 public static void main(String[] args) throws SchedulerException {

  // specify the job' s details..
  JobDetail job = JobBuilder.newJob(JobImplementation.class)
    .withIdentity("JobImplementation Every Minute Using Cron Schedule").build();

  // Run the Job every minute
  Trigger cronScheduleTrigger = TriggerBuilder.newTrigger().withIdentity(
    "JobImplementation Trigger").withSchedule(
    CronScheduleBuilder.cronSchedule("0 0/1 * * * ?")).build();
  //schedule the job
  
  SchedulerFactory schFactory = new StdSchedulerFactory();
  Scheduler sch = schFactory.getScheduler();
  sch.start();
  sch.scheduleJob(job, cronScheduleTrigger);
 }
}


Output - CronScheduleBuilder

log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
 Job Detail - Sat Nov 29 14:03:00 GMT+05:30 2014
 Job Detail - Sat Nov 29 14:04:00 GMT+05:30 2014


For Cron Expression examples, click Cron Expressions For Scheduling in Java







No comments:

Post a Comment