Introduction
crontab is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of crontab. CronTrigger uses "cron expressions", which are able to create firing schedules such as: "At 8:00am every Monday through Friday" or "At 1:30am every last Friday of the month".
Crontab Format
A crontab expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:
Special characters
* ("all values")
? ("no specific value")
- ("range")
, (Multiple)
/ (increment)
L ("last")
W ("weekday")
#(nth)
Examples
Here are some full examples:
Sample Code
Below is the sample code to execute job every minute:
Execution result:
Supplement
* Quartz Tutorial - Lesson 6: CronTrigger
* Quartz - CronTrigger Tutorial
crontab is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of crontab. CronTrigger uses "cron expressions", which are able to create firing schedules such as: "At 8:00am every Monday through Friday" or "At 1:30am every last Friday of the month".
Crontab Format
A crontab expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:
Special characters
* ("all values")
? ("no specific value")
- ("range")
, (Multiple)
/ (increment)
L ("last")
W ("weekday")
#(nth)
Examples
Here are some full examples:
Sample Code
Below is the sample code to execute job every minute:
- package demo.quartz
- import static org.quartz.CronScheduleBuilder.*
- import static org.quartz.JobBuilder.newJob
- import static org.quartz.SimpleScheduleBuilder.simpleSchedule
- import static org.quartz.TriggerBuilder.newTrigger
- import java.text.SimpleDateFormat
- import org.apache.log4j.PropertyConfigurator
- import org.quartz.Job
- import org.quartz.JobDetail
- import org.quartz.JobExecutionContext
- import org.quartz.JobExecutionException
- import org.quartz.Scheduler
- import org.quartz.impl.StdSchedulerFactory
- class HelloJob implements Job{
- static int cnt=0
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
- @Override
- public void execute(JobExecutionContext ctx) throws JobExecutionException {
- printf("Hello Job %d (%s)!\n", ++cnt, sdf.format(new Date()))
- }
- }
- PropertyConfigurator.configure("log4j.properties");
- // Grab the Scheduler instance from the Factory
- Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
- // and start it off
- scheduler.start();
- // define the job and tie it to our HelloJob class -> JobBuilder.newJob()
- JobDetail job = newJob(HelloJob.class)
- .withIdentity("job1", "group1")
- .build();
- // Trigger the job to run now, and then repeat every minutes -> TriggerBuilder.newTrigger()
- def trigger = newTrigger()
- .withIdentity("trigger3", "group1")
- .withSchedule(cronSchedule("0 */1 * * * ?"))
- .forJob("job1", "group1")
- .build();
- // Tell quartz to schedule the job using our trigger
- scheduler.scheduleJob(job, trigger);
- Thread.sleep(1000*60*5); // Sleep 5 minutes
- scheduler.shutdown();
Supplement
* Quartz Tutorial - Lesson 6: CronTrigger
* Quartz - CronTrigger Tutorial
沒有留言:
張貼留言