CronTrigger supports more specific scheduling than SimpleTrigger, and is not very complex. Based on cron expressions, CronTrigger supports calendar-like repetition intervals, rather than a single interval-which is a major improvement for SimpleTrigger.
Cron expressions include the following seven fields:
Seconds
Minute
Hours
Date in month
Month
Week date
Year (optional)
Special characters
The Cron trigger uses a series of special characters, as shown below:
The backslash (/) character indicates the increment value. For example, in the second field, "5/15" indicates that the value starts from 5th seconds, every 15 seconds.
Question mark (?) The characters and letters are only available in the Date Field of the month and the date field of the week. The question mark indicates that this field does not contain specific values. Therefore, if you specify a date in the month, you can insert "?" In the Date Field of the week, Indicates that the date value of the week is irrelevant. The L character is short for last. In the Date Field of the month, the task is executed on the last day of the month. In the week Date Field, if "L" exists separately, it is equal to "7". Otherwise, it represents the last instance of the week date in the current month. Therefore, "0L" indicates that it will be executed on the last Sunday of the month.
The letter (W) in the date field within the month is executed on the workday closest to the specified value. Place "1 W" in the Date Field of the month, which means that the execution is arranged within the first business day of the month.
The pound sign (#) specifies a specific workday instance for a given month. Put "MON #2" in the Date Field of the week, indicating that the task is scheduled for the second Monday of the month.
The asterisk (*) character is a wildcard character, indicating that this field can accept any possible value.
All these definitions may seem a little scary, but after a few minutes of practice, the cron expression will look very simple.
Listing 3 shows an example of CronTrigger. Note that the instantiation of SchedulerFactory, schedgger, and JobDetail is the same as that of SimpleTrigger. In this example, the trigger is modified. The cron expression specified here ("0/5 ****?") Schedule task execution every 5 seconds.
Listing 3. CronTriggerRunner. java
Public void task () throws SchedulerException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory ();
// Retrieve a scheduler from schedule factory
Scheduler schedfactory = schedulerFactory. getScheduler ();
// Current time
Long ctime = System. currentTimeMillis ();
// Initiate JobDetail with job name, job group, and executable job class
JobDetail jobDetail =
New JobDetail ("jobDetail2", "jobDetailGroup2", SimpleQuartzJob. class );
// Initiate CronTrigger with its name and group name
CronTrigger cronTrigger = new CronTrigger ("cronTrigger", "triggerGroup2 ");
Try {
// Setup CronExpression
CronExpression cexp = new CronExpression ("0/5 ****? ");
// Assign the CronExpression to CronTrigger
CronTrigger. setCronExpression (cexp );
} Catch (Exception e ){
E. printStackTrace ();
}
// Schedule a job with JobDetail and Trigger
Scheduler. scheduleJob (jobDetail, cronTrigger );
// Start the scheduler
Scheduler. start ();
}
Author: Tender001