下面給出一個簡單的示例:SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();?? Scheduler sched = schedFact.getScheduler();?? sched.start();?? // define the job and tie it to our HelloJob class? JobDetail job = newJob(HelloJob.class)????? .withIdentity("myJob", "group1")????? .build();?? // Trigger the job to run now, and then every 40 seconds? trigger = newTrigger()????? .withIdentity("myTrigger", "group1")????? .startNow()????? .withSchedule(simpleSchedule()????????? .withIntervalInSeconds(40)????????? .repeatForever())????? .build();?? // Tell quartz to schedule the job using our trigger? sched.scheduleJob(job, trigger);下面的截圖是Java源碼給出的示例: 我們在使用Quartz時,一般的操作步驟為:步驟1:自定義Job類(如:MyJob),實現Job接口;步驟2:使用JobBuilder生成JobDetail;步驟3:定義Trigger類,一般使用TriggerBuilder生成;步驟4:定義Scheduler類,使用Scheduler .scheduleJob(job, trigger)將job和trigger進行關聯;步驟5:Scheduler.start();步驟6:當需要關閉Scheduler時,使用Scheduler.shutdown();下面將具體進行講解。 回到頂部
二、Job、JobDetail、JobBuilder
官方定義:
Job - an interface to be implemented by components that you wish to have executed by the scheduler.
JobDetail - used to define instances of Jobs.
JobBuilder - used to define/build JobDetail instances, which define instances of Jobs.
Job
Job是一個接口類,下面是它的源碼: 如果要實現自己的作業(任務),最簡單的方式就是實現該接口,并實現接口中的execute()方法,當觸發該作業時,就是執行execute()方法;public class HelloJob implements Job {public HelloJob() {}public void execute(JobExecutionContext context)throws JobExecutionException{System.err.println("Hello! HelloJob is executing.");}}
JobDetail?
JobDetail :用于定義Job的實例,一般該類都是通過JobBuilder生成; JobBuilder:用于定義或創建JobDetail實例,使用了建造者模式;JobDetail job = newJob(HelloJob.class).withIdentity("myJob", "group1") // name "myJob", group "group1".build();//帶參數JobDetail job = JobBuilder.newJob(clazz).withIdentity(new JobKey(jobName, groupName)).usingJobData(new JobDataMap(params)).build();從上面的接口類中我們可以知道,只要有了JobDetail的實現類,我們就可以獲取到:
官方定義:Trigger - a component that defines the schedule upon which a given Job will be executed.TriggerBuilder - used to define/build Trigger instances.下面是觸發器層次結構,常用的有SimpleTrigger、CronTrigger等;
說明:只有在scheduler.start();之后,trigger以及Job才能有效運行;shutdown用于關閉;schedeleJob方法介紹 // Tell quartz to schedule the job using our trigger? sched.scheduleJob(job, trigger);官方介紹如下: 現在以StdScheduler作為示例進行介紹:start()方法介紹 使用start()方法來激活Trigger,執行定時任務;
回到頂部
五、JobListener、SchedulerListener、TriggerListener
對監聽進行注冊: ? ? ??回到頂部
六、Cron表達式
語法格式: 示例:
*?("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.
-?- used to specify ranges. For example, "10-12" in the hour field means?"the hours 10, 11 and 12".
,?- used to specify additional values. For example, "MON,WED,FRI" in the day-of-week field means?"the days Monday, Wednesday, and Friday".
/?- 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". You can also specify an offset from the last day of the month, such as "L-3" which would mean the third-to-last day of the calendar month.?When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing/unexpected 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.
The 'L' and 'W' characters can also be combined in the day-of-month field to yield 'LW', which translates to *"last weekday of the month"*.
#?- 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.