Scheduling: Cron - Quartz
scheduler
===============================
CronTrigger used to trigger the Job of Quartz
CronTrigger uses expression to schedule
Exp Syntax: CronTrigger ct=new
CronTrigger("cronTrigger","group2","0 0/1 * * *
?");
" * * * * * ? * "
2. Minutes
3. Hours
4. Day-of-Month
5. Month
6. Day-of-Week
7. Year (optional field)
All of the fields have a set of valid values that can be
specified.
These values should be fairly obvious - such as the numbers
0 to 59 for seconds and minutes, and the values 0 to 23 for hours.
Day-of-Month can be any value 0-31, but you need to be careful
about how many days are in a given month!
Months can be specified as values between 0 and 11, or by
using the strings JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and
DEC.
Days-of-Week can be specified as values between 1 and 7 (1 =
Sunday) or by using the strings SUN, MON, TUE, WED, THU, FRI and SAT.
/ - used to specify increments on value. 0/35 - it means on
35th min of every hour & not every 35 min.
? - used to specify no specific value for the field
L - if given in Day-of-Month means last day of the month or
if given in Day-of-Week means last day of the week. u can use this with Day of
Week as 'FRIL' it means last friday of the month.
# - used in day of week field as, FRI#3 or 6#3, it means 3rd
friday of the month.
* - used in all fields to specify every possible value of
the field.
Note: Both day-of-week
or day-of-month parameter should not be *. That means any of the parameter
should be implemented with specific value not with every possible value of the
field
Quartz Jar Download Link - http://quartz-scheduler.org/downloads/catalog
Sample Code:
public class CronTest implements Job{
public static void main(String
a[]){
SchedulerFactory sf=new
StdSchedulerFactory();
Scheduler sched =
sf.getScheduler();
sched.deleteJob("job","detail");
JobDetail jd=new JobDetail("job","detail",CronTest.class);
JobDataMap dataMap = new
JobDataMap();
jd.setJobDataMap(dataMap);
CronTrigger ct=new CronTrigger("cronTrigger","group","0/10
* * ? * *");
sched.scheduleJob(jd,ct);
sched.start();
}
@Override
public void
execute(JobExecutionContext arg) {
//
implement your logic here
//
Will be invoked for every 10 seconds, starting from 0th second
}
}