摘要:是一個(gè)任務(wù)日程管理系統(tǒng),一個(gè)在預(yù)先確定被納入日程的時(shí)間到達(dá)時(shí),負(fù)責(zé)執(zhí)行或者通知其他軟件組件的系統(tǒng)。核心接口核心調(diào)度器任務(wù)任務(wù)描述觸發(fā)器和是同時(shí)相互依賴存在的,和觸發(fā)器一起注冊(cè)到核心調(diào)度器。
一、Quartz簡(jiǎn)介 1. Quartz
Quartz是OpenSymphony開(kāi)源組織在Job scheduling領(lǐng)域又一個(gè)開(kāi)源項(xiàng)目,它可以與J2EE與J2SE應(yīng)用程序相結(jié)合也可以多帶帶使用。Quartz可以用來(lái)創(chuàng)建簡(jiǎn)單或?yàn)檫\(yùn)行十個(gè),百個(gè),甚至是好幾萬(wàn)個(gè)Jobs這樣復(fù)雜的日程序表。Jobs可以做成標(biāo)準(zhǔn)的Java組件或 EJBs。
Quartz是一個(gè)任務(wù)日程管理系統(tǒng),一個(gè)在預(yù)先確定(被納入日程)的時(shí)間到達(dá)時(shí),負(fù)責(zé)執(zhí)行(或者通知)其他軟件組件的系統(tǒng)。
Quartz用一個(gè)小Java庫(kù)發(fā)布文件(.jar文件),這個(gè)庫(kù)文件包含了所有Quartz核心功能。這些功能的主要接口(API)是Scheduler接口。
2. Quartz核心接口Scheduler – 核心調(diào)度器
Job – 任務(wù)
JobDetail – 任務(wù)描述
Trigger -- 觸發(fā)器
Job和JobDetail是同時(shí)相互依賴存在的,和觸發(fā)器一起注冊(cè)到核心調(diào)度器。3. Tigger
Quartz的執(zhí)行過(guò)程: Scheduler--> Trigger --> JobDetail --> Job
SimpleTrigger
CronTrigger
SimpleTriggerSimpleTrigger用來(lái)觸發(fā)只需執(zhí)行一次或者在給定時(shí)間觸發(fā)并且重復(fù)N次且每次執(zhí)行延遲一定時(shí)間的任務(wù)。
CronTrigger像日歷那樣按日程來(lái)觸發(fā)任務(wù),而不是像SimpleTrigger 那樣每隔特定的間隔時(shí)間觸發(fā),CronTriggers通常比SimpleTrigger更有用。
二、Cron Expressions 1. CronTriggerCronTriggers往往比SimpleTrigger更有用,如果您需要基于日歷的概念,而非SimpleTrigger完全指定的時(shí)間間隔,復(fù)發(fā)的發(fā)射工作的時(shí)間表。
CronTrigger,你可以指定觸發(fā)的時(shí)間表如“每星期五中午”,或“每個(gè)工作日9:30時(shí)”,甚至“每5分鐘一班9:00和10:00逢星期一上午,星期三星期五“。
即便如此,SimpleTrigger一樣,CronTrigger擁有的startTime指定的時(shí)間表時(shí)生效,指定的時(shí)間表時(shí),應(yīng)停止(可選)結(jié)束時(shí)間。
cron的表達(dá)式被用來(lái)配置CronTrigger實(shí)例。 cron的表達(dá)式是字符串,實(shí)際上是由七子表達(dá)式,描述個(gè)別細(xì)節(jié)的時(shí)間表。這些子表達(dá)式是分開(kāi)的空白,代表:
1 . 1. Seconds
2 . 2. Minutes
3 . 3. Hours
4 . 4. Day-of-Month
5 . 5. Month
6 . 6. Day-of-Week
7 . 7. Year (可選字段)
例: "0 0 12 ? * WED" 在每星期三下午12:00 執(zhí)行。
個(gè)別子表達(dá)式可以包含范圍, 例如,在前面的例子里("WED")可以替換成 "MON-FRI", "MON, WED, FRI"甚至"MON-WED,SAT".
“*” 代表整個(gè)時(shí)間段.
每一個(gè)字段都有一套可以指定有效值,如
Seconds (秒):可以用數(shù)字0-59 表示,
Minutes(分):可以用數(shù)字0-59 表示,
Hours(時(shí)):可以用數(shù)字0-23表示,
Day-of-Month(天):可以用數(shù)字1-31 中的任一一個(gè)值,但要注意一些特別的月份
Month(月):可以用0-11 或用字符串 “JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC” 表示
Day-of-Week(每周):可以用數(shù)字1-7表示(1 = 星期日)或用字符口串“SUN, MON, TUE, WED, THU, FRI and SAT”表示
“/”:為特別單位,表示為“每”如“0/15”表示每隔15分鐘執(zhí)行一次,“0”表示為從“0”分開(kāi)始, “3/20”表示表示每隔20分鐘執(zhí)行一次,“3”表示從第3分鐘開(kāi)始執(zhí)行
“?”:表示每月的某一天,或第周的某一天
“L”:用于每月,或每周,表示為每月的最后一天,或每個(gè)月的最后星期幾如“6L”表示“每月的最后一個(gè)星期五”
“W”:表示為最近工作日,如“15W”放在每月(day-of-month)字段上表示為“到本月15日最近的工作日”
““#”:是用來(lái)指定“的”每月第n個(gè)工作日,例 在每周(day-of-week)這個(gè)字段中內(nèi)容為"6#3" or "FRI#3" 則表示“每月第三個(gè)星期五”
字段名 | 允許的值 | 允許的特殊字符 |
---|---|---|
秒 | 0-59 | , - * / |
分 | 0-59 | , - * / |
小時(shí) | 0-23 | , - * / |
日 | 1-31 | , - * ? / L W C |
月 | 1-12 or JAN-DEC | , - * / |
周幾 | 1-7 or SUN-SAT | , - * ? / L C # |
年(可選字段) | empty, 1970-2099 | , - * / |
“?”字符:表示不確定的值 “,”字符:指定數(shù)個(gè)值 “-”字符:指定一個(gè)值的范圍 “/”字符:指定一個(gè)值的增加幅度。n/m表示從n開(kāi)始,每次增加m “L”字符:用在日表示一個(gè)月中的最后一天,用在周表示該月最后一個(gè)星期X “W”字符:指定離給定日期最近的工作日(周一到周五) “#”字符:表示該月第幾個(gè)周X。6#3表示該月第3個(gè)周五2). Cron表達(dá)式范例:
每隔5秒執(zhí)行一次:/5 * ?
每隔1分鐘執(zhí)行一次:0 /1 ?
每天23點(diǎn)執(zhí)行一次:0 0 23 ?
每天凌晨1點(diǎn)執(zhí)行一次:0 0 1 ?
每月1號(hào)凌晨1點(diǎn)執(zhí)行一次:0 0 1 1 * ?
每月最后一天23點(diǎn)執(zhí)行一次:0 0 23 L * ?
每周星期天凌晨1點(diǎn)實(shí)行一次:0 0 1 ? * L
在26分、29分、33分執(zhí)行一次:0 26,29,33 * ?
每天的0點(diǎn)、13點(diǎn)、18點(diǎn)、21點(diǎn)都執(zhí)行一次:0 0 0,13,18,21 ?
前面對(duì)Tigger進(jìn)行過(guò)簡(jiǎn)單描述,現(xiàn)在用Demo進(jìn)行演示Quartz的Maven依賴
org.quartz-scheduler quartz 2.2.1
1 . 實(shí)現(xiàn)一個(gè)Job接口
/* * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * */ package org.ouhei.quartz.example; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** ** This is just a simple job that says "Hello" to the world. *
* * @author Bill Kratzer */ public class HelloJob implements Job { private static Logger _log = LoggerFactory.getLogger(HelloJob.class); /** ** Empty constructor for job initilization *
** Quartz requires a public empty constructor so that the * scheduler can instantiate the class whenever it needs. *
*/ public HelloJob() { } /** ** Called by the
* * @throws JobExecutionException * if there is an exception while executing the job. */ public void execute(JobExecutionContext context) throws JobExecutionException { // Say Hello to the World and display the date/time _log.info("Hello World! - " + new Date()); } }{@link org.quartz.Scheduler}
when a *{@link org.quartz.Trigger}
fires that is associated with * theJob
. *
2 . SimpleTrigger觸發(fā)器進(jìn)行模擬
SimpleExample代碼/* * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * */ package org.ouhei.quartz.example; import static org.quartz.DateBuilder.evenMinuteDate; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; /** * This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule * a job to run in Quartz. * * @author Bill Kratzer */ public class SimpleExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleExample.class); log.info("------- Initializing ----------------------"); // 定義調(diào)度器 SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete -----------"); // 獲取當(dāng)前時(shí)間的下一分鐘 Date runTime = evenMinuteDate(new Date()); log.info("------- Scheduling Job -------------------"); // 定義job // 在quartz中,有組的概念,組+job名稱 唯一的 JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build(); // 定義觸發(fā)器,在下一分鐘啟動(dòng) Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build(); // 將job注冊(cè)到調(diào)度器 sched.scheduleJob(job, trigger); log.info(job.getKey() + " will run at: " + runTime); // 啟動(dòng)調(diào)度器 sched.start(); log.info("------- Started Scheduler -----------------"); // 等待65秒 log.info("------- Waiting 65 seconds... -------------"); try { // wait 65 seconds to show job Thread.sleep(65L * 1000L); // executing... } catch (Exception e) { // } // 關(guān)閉調(diào)度器 log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); } public static void main(String[] args) throws Exception { SimpleExample example = new SimpleExample(); example.run(); } }
3 . 添加日志文件log4j.properties
log4j.rootLogger=DEBUG,A1 log4j.logger.com.taotao = DEBUG log4j.logger.org.mybatis = DEBUG log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
運(yùn)行main方法,觀察控制臺(tái)。
如果模擬CronTrigger觸發(fā)器,把上述的SimpleExample代碼用SimpleCronExample代碼替換SimpleCronExample代碼:
/* * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * */ package org.ouhei.quartz.example; import static org.quartz.CronScheduleBuilder.cronSchedule; import static org.quartz.DateBuilder.evenMinuteDate; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; /** * This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule * a job to run in Quartz. * * @author Bill Kratzer */ public class SimpleCronExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleCronExample.class); log.info("------- Initializing ----------------------"); // 定義調(diào)度器 SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete -----------"); // 獲取當(dāng)前時(shí)間的下一分鐘 Date runTime = evenMinuteDate(new Date()); log.info("------- Scheduling Job -------------------"); // 定義job JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build(); // 定義觸發(fā)器,每2秒執(zhí)行一次 Trigger trigger = newTrigger().withIdentity("trigger1", "group1") .withSchedule(cronSchedule("0 0/1 * * * ?")).build(); // 將job注冊(cè)到調(diào)度器 sched.scheduleJob(job, trigger); log.info(job.getKey() + " will run at: " + runTime); // 啟動(dòng)調(diào)度器 sched.start(); log.info("------- Started Scheduler -----------------"); // 等待1分鐘 log.info("------- Waiting 60 seconds... -------------"); try { Thread.sleep(60L * 1000L); } catch (Exception e) { // } // 關(guān)閉調(diào)度器 log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); } public static void main(String[] args) throws Exception { SimpleCronExample example = new SimpleCronExample(); example.run(); } }四、spring整合Quartz
前面用案例介紹過(guò)SimpleTrigger和CronTrigger,
下面用spring整合Quartz,分四步:
導(dǎo)入依賴
編寫(xiě)Job
編寫(xiě)spring配置文件
啟動(dòng)spring容器(啟動(dòng)調(diào)度器)
1. 導(dǎo)入maven依賴2. 編寫(xiě)Job4.0.0 org.ouhei.quartz ouhei-quartz 1.0.0-SNAPSHOT org.springframework spring-context-support 4.0.6.RELEASE org.quartz-scheduler quartz 2.2.1 org.slf4j slf4j-log4j12 1.7.7 org.springframework spring-tx 4.0.6.RELEASE
package org.ouhei.quartz; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.QuartzJobBean; /** * QuartzJobBean實(shí)現(xiàn)了Job接口 * */ public class MyJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("myJob 執(zhí)行了............." + context.getTrigger().getKey().getName()); ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap() .get("applicationContext"); System.out.println("獲取到的Spring容器是: " + applicationContext); } }3. 編寫(xiě)spring配置文件applicationContext-scheduler.xml
4. 啟動(dòng)spring容器(啟動(dòng)調(diào)度器)
package org.ouhei.quartz; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { new ClassPathXmlApplicationContext("classpath:applicationContext-scheduler.xml"); } }5. 添加日志文件
log4j.rootLogger=DEBUG,A1 log4j.logger.com.taotao = DEBUG log4j.logger.org.mybatis = DEBUG log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/69308.html
摘要:觸發(fā)器也可以給予名稱和放置在組中,以方便地將它們調(diào)度內(nèi)組織。作業(yè)可以被添加到所述調(diào)度器一次,而是具有多個(gè)觸發(fā)器注冊(cè)。調(diào)度類(lèi)鏈接工作和觸發(fā)器到一起,并執(zhí)行它。 簡(jiǎn)介 Quartz是一個(gè)開(kāi)源的作業(yè)調(diào)度框架,可以讓計(jì)劃的程序任務(wù)一個(gè)預(yù)定義的日期和時(shí)間運(yùn)行。Quartz可以用來(lái)創(chuàng)建簡(jiǎn)單或復(fù)雜的日程安排執(zhí)行幾十,幾百,甚至是十萬(wàn)的作業(yè)數(shù)。官方鏈接,戳這里 Quartz是什么? 作業(yè)調(diào)度庫(kù) Qua...
摘要:教程二十一使用定時(shí)任務(wù)文章目錄環(huán)境一簡(jiǎn)單使用二配合數(shù)據(jù)庫(kù)環(huán)境一簡(jiǎn)單使用二配合數(shù)據(jù)庫(kù) ??Spring Boot教程(二十一):Spring Boot使用Quartz定時(shí)任務(wù)?? 文章目錄??環(huán)境????一、簡(jiǎn)單使用????二、配合數(shù)據(jù)庫(kù)??環(huán)境??quartz 2.3??一、簡(jiǎn)單使用...
摘要:花了將近兩個(gè)星期完成了功能,期間我編寫(xiě)的能力也算是有所提升了。所以能看到這篇文章的同學(xué)都是大佬如果想看更多的原創(chuàng)技術(shù)文章,歡迎大家關(guān)注我的微信公眾號(hào)??赡芨信d趣的鏈接文章的目錄導(dǎo)航微信公眾號(hào)端文章的目錄導(dǎo)航端海量精美腦圖 前言 只有光頭才能變強(qiáng) 2018年8月30日,今天我辭職了。在6月25號(hào)入職,到現(xiàn)在也有兩個(gè)月時(shí)間了。 感受: 第一天是期待的:第一次將項(xiàng)目拉到本地上看的時(shí)候,代碼...
摘要:多作業(yè)例子在這個(gè)例子中,我們將介紹如何通過(guò)多個(gè)作業(yè)。在調(diào)度框架中,每個(gè)作業(yè)將被連接到一個(gè)唯一的觸發(fā),并且由調(diào)度器運(yùn)行它。備注說(shuō)明在中,一個(gè)觸發(fā)器觸發(fā)多個(gè)作業(yè)是不可以的。第一步創(chuàng)建個(gè)作業(yè),,和。 多作業(yè)例子 在這個(gè)例子中,我們將介紹如何通過(guò)Quartz API 多個(gè)作業(yè)。在Quartz調(diào)度框架中,每個(gè)作業(yè)將被連接到一個(gè)唯一的觸發(fā),并且由調(diào)度器運(yùn)行它。 備注說(shuō)明:在 Quartz 中,一個(gè)...
閱讀 1433·2019-08-30 15:55
閱讀 1692·2019-08-26 10:21
閱讀 3476·2019-08-23 18:28
閱讀 3405·2019-08-23 15:38
閱讀 771·2019-08-23 15:24
閱讀 2164·2019-08-23 13:59
閱讀 805·2019-08-23 11:31
閱讀 2897·2019-08-23 10:53