Thursday 30 October 2014

Cron Triggers


Cron triggers use Cron Expressions to create firing schedules.

Cron Expressions
Cron Expressions are strings that made up of seven sub expressions. The sub expressions are separated with space and represent
1.   Seconds
2.   Minutes
3.   Hours
4.   Day-Of-Month
5.   Month
6.   Day-Of-week
7.   Year(Optional)

Examples
Expression
Meaning
"0 0 12 * * ?"
Fires at 12 pm on every day
"0 16 11 ? * *"
Fires at 11.16 AM on every day
"0 16 11 * * ? 2014"
Fires at 11.16 AM on every day during the year 2014
"0 16 11 ? * MON-FRI"
Fires at 11.16 AM on every Monday, Tuesday, Wednesday, Thursday and Friday
"0 16 11 ? * 6L 2014-2019"
Fire at 11:16am on every last Friday of every month during the years 2014, 2015, 2016, 2017, 2018 and 2019
"0 16 11 ? * 6#3"
Fire at 11:16am on the third Friday of every month

*, ?, -, L, # are special characters.

Special Character
Description
*
Represents all values, for Ex: * in hour field means "every hour".
?
Allowed for the day-of-month and day-of-week fields. For Ex: "0 16 11 ? * 6#3" : fire at 11:16am on the third Friday of every month
-
Represents range. For Ex: MON-FRI represents Monday to Friday. 10-12 in hours field represents the hours 10, 11 and 12.
,
Represents additional values. For Ex: "MON, TUE, SAT" in the day-of-week field means "the days Monday, Tuesday, Saturday".
/
Represents increments. For Ex: "0/10" in the seconds field means "the seconds 0, 10, 20, and 30". And "5/10" in the seconds field means "the seconds 5, 15, 25, and 35".
L
stands for last. It is allowed for the day-of-month and day-of-week fields.

For Ex:
1. "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years.
2. "6L" in the day-of-week field represents "the last Friday of the month".
3. "0 16 11 ? * 6L 2014-2019" represents fire at 11:16am on every last Friday of every month during the years 2014, 2015, 2016, 2017, 2018 and 2019
W
W stands for week day, represents (MON-FRI). It is allowed for the day of the month field.

For Ex: "11W" represents "the nearest weekday to the 11th of the month". So if the 11th is a Saturday, the trigger will fire on Friday the 10th. If the 11th is a Sunday, the trigger will fire on Monday the 12th. If the 11th is a Tuesday, then it will fire on Tuesday the 11th.
#
It is allowed for the day-of-week field. Represents "the nth" XXX day of the month.

For Ex:
The value of "6#3" in the day-of-week field means the third Friday of the month. 6 stands for Friday and #3 stands for 3rd of the month.

Complete Information on Cron Expression fields
Field Name
Required
Allowed Values
Special Characters allowed
Seconds
Yes
0-59
,-*
Minutes
Yes
0-59
,-*
Hours
Yes
0-23
,-*
Day-Of-Month
Yes
1-31
,-*?/LW
Month
Yes
1-12 OR JAN-DEC
,-*
Day-Of-week
Yes
1-7 OR SUN-SAT
,-*?/L#
Year
No
Empty, 2014-2019
,-*

Example
Task.java
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;

public class Task implements Job{
    @Override
    public void execute(JobExecutionContext jec) throws JobExecutionException {

        /* Get Job Details */
        JobDetail jobDetails = jec.getJobDetail();

        /* Get JobDataMap */
        JobDataMap jobDataMap = jobDetails.getJobDataMap();
        String author = jobDataMap.getString("author");
        String topic= jobDataMap.getString("topic");
        String about = jobDataMap.getString("about");

        /* Get Trigger handle */
        Trigger trigger = jec.getTrigger();

        /* Get DataMap associated with Trigger */
        JobDataMap triggerDataMap =trigger.getJobDataMap();
        String repeatInterval = triggerDataMap.getString("repeatInterval");
        String triggerName = triggerDataMap.getString("triggerName");

        System.out.println("***************************************");
        System.out.println("Information in JobDataMap");
        System.out.println("author : " + author);
        System.out.println("topic : " + topic);
        System.out.println("about : " + about);
        System.out.println("Information in triggerDataMap");
        System.out.println("Repeat Interval is : " + repeatInterval);
        System.out.println("Trigger Name is : " + triggerName);
        System.out.println("***************************************\n\n");
    }
}


CronTriggerEx.java
import java.text.ParseException;
import java.util.Date;
import org.quartz.JobDataMap;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

public class CronTriggerEx {
    public static void main(String args[]) throws SchedulerException, ParseException{
         // Initiate a Schedule Factory
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        // Retrieve a scheduler from schedule factory
        Scheduler scheduler = schedulerFactory.getScheduler();

        //Creating JobDetailImpl and link to our Job class
  JobDetailImpl jobDetail = new JobDetailImpl();
  jobDetail.setName("First Job");
  jobDetail.setJobClass(Task.class);
        jobDetail.setDescription("Simple Task Application");
        jobDetail.setRequestsRecovery(true);
        jobDetail.setDurability(true);

        /* Set JobDataMap for the Job */
        JobDataMap jobDataMap = new JobDataMap ();
        jobDataMap.put("author", "Krishna");
        jobDataMap.put("topic", "Quartz Scheduler");
        jobDataMap.put("about", "JobDataMap");
        jobDetail.setJobDataMap(jobDataMap);

        //Creating schedule time with trigger
  CronTriggerImpl cronTrigger = new CronTriggerImpl();
  cronTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
        cronTrigger.setCronExpression("0 50 12 * * ? 2014");
  cronTrigger.setName("FirstTrigger");
        cronTrigger.setDescription("Simple Trigger");

        /* Set triggerDataMap for the Job */
        JobDataMap triggerDataMap = new JobDataMap ();
        triggerDataMap.put("repeatInterval", "3000");
        triggerDataMap.put("triggerName", "firstTrigger");
        cronTrigger.setJobDataMap(triggerDataMap);

  //Start scheduler
  scheduler.start();
  scheduler.scheduleJob(jobDetail,cronTrigger);
    }
}


Cron Expression "0 50 12 * * ? 2014", fires at 12.50 PM on every day during the year 2014.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment