程式扎記: [ Java 代碼範本 ] Quartz - Execute job every minute

標籤

2015年11月16日 星期一

[ Java 代碼範本 ] Quartz - Execute job every minute

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 crontabCronTrigger 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
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") 
Used to select all values within a field. For example, "" in the minute field means *"every minute".

? ("no specific value") 
Useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put "10" in the day-of-month field, and "?" in the day-of-week field. See the examples below for clarification.

("range")
Used to specify ranges. For example, "10-12" in the hour field means "the hours 10, 11 and 12".

, (Multiple)
Used to specify additional values. For example, "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".

/ (increment)
Used to specify increments. For example, "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". You can also specify '/' after the '' character - in this case '' is equivalent to having '0' before the '/'. '1/3' in the day-of-month field means "fire every 3 days starting on the first day of the month".

L ("last") 
Has different meaning in each of the two fields in which it is allowed. For example, the value "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. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

W ("weekday") 
Used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days.

#(nth)
Used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means "the third Friday of the month" (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.

Examples
Here are some full examples:
 


Sample Code
Below is the sample code to execute job every minute:
  1. package demo.quartz  
  2.   
  3. import static org.quartz.CronScheduleBuilder.*  
  4. import static org.quartz.JobBuilder.newJob  
  5. import static org.quartz.SimpleScheduleBuilder.simpleSchedule  
  6. import static org.quartz.TriggerBuilder.newTrigger  
  7.   
  8. import java.text.SimpleDateFormat  
  9.   
  10. import org.apache.log4j.PropertyConfigurator  
  11. import org.quartz.Job  
  12. import org.quartz.JobDetail  
  13. import org.quartz.JobExecutionContext  
  14. import org.quartz.JobExecutionException  
  15. import org.quartz.Scheduler  
  16. import org.quartz.impl.StdSchedulerFactory  
  17.   
  18. class HelloJob implements Job{  
  19.     static int cnt=0  
  20.     SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");  
  21.       
  22.     @Override  
  23.     public void execute(JobExecutionContext ctx) throws JobExecutionException {  
  24.         printf("Hello Job %d (%s)!\n", ++cnt, sdf.format(new Date()))     
  25.     }  
  26. }  
  27.   
  28. PropertyConfigurator.configure("log4j.properties");  
  29.   
  30. // Grab the Scheduler instance from the Factory  
  31. Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();  
  32.   
  33. // and start it off  
  34. scheduler.start();  
  35.   
  36. // define the job and tie it to our HelloJob class -> JobBuilder.newJob()  
  37. JobDetail job = newJob(HelloJob.class)  
  38.                 .withIdentity("job1""group1")  
  39.                 .build();  
  40.                   
  41. // Trigger the job to run now, and then repeat every minutes -> TriggerBuilder.newTrigger()  
  42. def trigger = newTrigger()    
  43.     .withIdentity("trigger3""group1")    
  44.     .withSchedule(cronSchedule("0 */1 * * * ?"))    
  45.     .forJob("job1""group1")    
  46.     .build();                 
  47.   
  48. // Tell quartz to schedule the job using our trigger  
  49. scheduler.scheduleJob(job, trigger);  
  50. Thread.sleep(1000*60*5); // Sleep 5 minutes  
  51. scheduler.shutdown();  
Execution result:
Hello Job 1 (2015/11/16 08:03:00)!
Hello Job 2 (2015/11/16 08:04:00)!
Hello Job 3 (2015/11/16 08:05:00)!
Hello Job 4 (2015/11/16 08:06:00)!
Hello Job 5 (2015/11/16 08:07:00)!

Supplement
Quartz Tutorial - Lesson 6: CronTrigger
CronTrigger is often more useful than SimpleTrigger, if you need a job-firing schedule that recurs based on calendar-like notions, rather than on the exactly specified intervals of SimpleTrigger...

Quartz - CronTrigger Tutorial
cron 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 cron...


沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!