quartz學習總結:
一、關于job:
?? 用Quartz的行話講,作業是一個執行任務的簡單Java類。任務可以是任何Java代碼。只需你實現org.quartz.Job接口并且在出現嚴重錯誤情況下拋出JobExecutionException異常即可。Job接口包含唯一的一個方法execute(),作業從這里開始執行。一旦實現了Job接口和execute()方法,當Quartz確定該是作業運行的時候,它將調用你的作業。Execute()方法內就完全是你要做的事情。要注意,自己實現job時必須有一個public 的無參數的構造方法.對于job,大多數情況下都要依賴于某些具體的條件,這時,就要用到JobDataMap了。JobDataMap是Map的一個子類,獲取時很簡單,直接用get方法就ok了,基于參數,我們就可以定制不同的job任務了。下面是一個簡單的job,用來列舉出所有的參數并且獲得參數名為name的值.
?? public class HelloJob implements Job {
?private static Log _log = LogFactory.getLog(HelloJob.class);
?public HelloJob() {
?}
?public void execute(JobExecutionContext context)
???throws JobExecutionException {
??JobDataMap jobDataMap =? context.getJobDetail().getJobDataMap();
??_log.info("要執行的參數如下:");
??Iterator i = jobDataMap.entrySet().iterator();??
??while(i.hasNext()) {
??Map.Entry me = (Map.Entry)i.next();
??_log.info(me.getKey() + ": "+me.getValue());
??}??
??_log.info("U Are Welcome:"+jobDataMap.get("name"));
?}
???? }
二、關于jobdetail:
??? Quartz并不存儲一個真正的Job實例,相反的,它通過jobdetail來定義job,并指定job的name和group,在一個調度器(Scheduler)中,name和group是唯一被定義的,一個觸發器(trigger)只能指定一個job,但多個觸發器可以指定同一個job.
三、關于Scheduler
??? Scheduler的作用就是調用任務,在指定的時間執行指定的任務。主要方法如下:
??? scheduleJob方法:
??? scheduleJob(JobDetail jobDetail, Trigger trigger):把jobDetail添加到調度器中,并指定觸發器trigger.在這里要注意,在同一個調度器中,jobDetail的name和group是唯一的;Trigger的name和group也必須是唯一的。如果在trigger中指定job的name,則該name必須和jobDetail的name保持一致,否則會拋出異常。
??? scheduleJob(Trigger trigger):指定的trigger中必須包含jobdetai的name.以便于讓quartz知道要執行的任務,如果指定的jobdetail的name不在調度器中的任務列表中,則會拋出JobPersistenceException異常。
??? deleteJob(String jobName,String groupName)方法:
??? 刪除指定的job,并且刪除所有相關聯的觸發器。(Delete the identified Job from the Scheduler - and any associated Triggers.)
四、關于作業存儲
??? Quartz提供兩種基本作業存儲類型。
???
??? 第一種類型叫做RAMJobStore,它利用通常的內存來持久化調度程序信息。這種作業存儲類型最容易配置、構造和運行。對許多應用來說,這種作業存儲已經足夠了。然而,因為調度程序信息是存儲在被分配給JVM的內存里面,所以,當應用程序停止運行時,所有調度信息將被丟失。
???
??? 第二種類型稱為JDBC作業存儲。Quartz提供兩種不同的實現,但兩種實現一般都需要JDBC驅動程序和后臺數據庫來持久化調度程序信息。這兩種類型的不同在于你是否想要控制數據庫事務或這釋放控制給應用服務器例如BEA's WebLogic或Jboss。(這類似于J2EE領域中,Bean管理的事務和和容器管理事務之間的區別)這兩種JDBC作業存儲是:
??? · JobStoreTX:當你想要控制事務或工作在非應用服務器環境中是使用 (注:自己控制事務)。
??? · JobStoreCMT:當你工作在應用服務器環境中和想要容器控制事務時使用 (web服務器控制事務)。
五、關于觸發器
????? Quartz中的觸發器用來告訴調度程序作業什么時候觸發。框架提供了一把觸發器類型,但兩個最常用的是SimpleTrigger和CronTrigger。SimpleTrigger為需要簡單打火調度而設計。
????? 典型地,如果你需要在給定的時間和重復次數或者兩次打火之間等待的秒數打火一個作業,那么SimpleTrigger適合你。
?????
????? 另一方面,如果你有許多復雜的作業調度,那么或許需要CronTrigger。
????
????? CronTrigger很強大,使用復雜的時間判斷來使用,效果很好。
六、關于Quartz中的幾個表:
??? QRTZ_TRIGGERS?????????????????? 存放Trigger(包括SIMPLE_TRIGGERS和CRON_TRIGGERS)和jobDetail的對應關系
??? QRTZ_TRIGGER_LISTENERS
??? QRTZ_SIMPLE_TRIGGERS??????????? 存儲簡單觸發器?
??? QRTZ_SCHEDULER_STATE
??? QRTZ_PAUSED_TRIGGER_GRPS
??? QRTZ_LOCKS
??? QRTZ_JOB_LISTENERS
??? QRTZ_JOB_DETAILS???????????????? 存儲jobDetail
??? QRTZ_FIRED_TRIGGERS
??? QRTZ_CRON_TRIGGERS?????????????? 存儲復雜觸發器CRON_TRIGGERS
??? QRTZ_CALENDARS
??? QRTZ_BLOB_TRIGGERS
??? 注:這幾個表網上并沒有相關說明,等研究一段后補充()。
七、把quartz集成到web應用中
??? 1、根據quartz中提供的建表文檔,建立數據庫.
??? 2、把如下quartz.properties文件放置到classes目錄下.文件內容如下:
???
?#============================================================================
?# Configure Main Scheduler Properties?
?#============================================================================
???????
?#調度器名,無關緊要,名字任意定
?org.quartz.scheduler.instanceName = ZXScheduler
?org.quartz.scheduler.instanceId = AUTO
?#============================================================================
?# Configure ThreadPool?? 配置數據庫連接池
?#============================================================================
?org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
?org.quartz.threadPool.threadCount = 12
?org.quartz.threadPool.threadPriority = 5
?#============================================================================
?# Configure JobStore? 配置做業存儲方式
?#============================================================================
???????
?#相當于掃描頻率,如果系統基于秒級,應培植成1000,quartz默認為分級(60000)
?org.quartz.jobStore.misfireThreshold = 1000
?#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
???????
?#在這里自己控制事務
?org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
?org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
?org.quartz.jobStore.useProperties = false
?#配置dataSource名
?org.quartz.jobStore.dataSource = myDS
?#表前綴
?org.quartz.jobStore.tablePrefix = QRTZ_
?org.quartz.jobStore.isClustered = false
?#============================================================================
?# Configure Datasources? 配置數據庫的連接,不用解釋
?#============================================================================
?org.quartz.dataSource.myDS.driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
?org.quartz.dataSource.myDS.URL = jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=quartzTest
?org.quartz.dataSource.myDS.user = sa
?org.quartz.dataSource.myDS.password =?sa
?org.quartz.dataSource.myDS.maxConnections = 5
??? 3、配置web.xml,啟動quartz的初試化類,添加初始化servlet
?? <servlet>
??? <servlet-name>QuartzInitializer</servlet-name>
??? <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
??? <init-param>
????? <param-name>shutdown-on-unload</param-name>
????? <param-value>true</param-value>
??? </init-param>
??? <init-param>
???? <param-name>config-file</param-name>
???? <param-value>quartz.properties</param-value>
??? </init-param>
??? <load-on-startup>2</load-on-startup>
? </servlet>???
?
???? 4、系統配置完畢。
八、構造cron觸發器(個人翻譯,英文不好,莫見笑)
??? cron 觸發器規范:
??? Seconds Minutes Hours Day-of-month Month Day-of-Week Year
??? 秒?????? 分????? 時???? 天?????????? 月??? 周????????? 年??
???
??? Seconds??????????? 0-59??? , - * /
??? Minutes??????????? 0-59??? , - * /
??? Hours????????????? 0-23??? , - * /
??? Day-of-month?????? 1-31??? , - * ? / L C
??? Month????????????? 1-12 or JAN-DEC??? , - * /
??? Day-of-Week??????? 1-7 or SUN-SAT??? , - * ? / L C #
??? Year (Optional)??? empty, 1970-2099??? , - * /
??? 關于字符串的設置(在cron expression中所有字符不區分大小寫):
??? The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
??? "*"字符被用來指定所有的值,例如,"*"在分鐘字段時表示每一分鐘
??? The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
??? "?"字符在天和周字段中使用。表示沒有指定值,天和周字段指定一個,但不能兩個都指定為"?"
??? The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".
??? '-'字符被用來設置范圍,比如"10-12"在小時字段的意義為"10點,11點,12點"
??? The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".
??? ','字符被用來設置添加的值,例如在周字段設置"MON,WED,FRI"的意義即為:在周一、周三、周五激活
??? The '/' character is 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 '/'.
??? '/'字符被用來設置增量。例如秒字段設置"0/15"的意思為從0開始,每15秒觸發,即在0、15、30、45秒觸發,秒字段設置"5/15"的意思為,從5開始,第15秒觸發,即在5、20、35、50秒觸發.你還可以在'*'后面使用'/'字符,在這種情況下'*'與字符'0'意義相同。
??? The 'L' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "last", but it has different meaning in each of the two fields. 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.
??? 'L'在天字段和周字段中使用。'L'字符是'last'的縮寫,但在兩個字段中,它有不同的意義。例如,'L'在天字段的意思為月的最后一天,在一月為31,非閏月2月為28。如果在'L'用在周字段中,意思為'7'(周六),即周末,一周最后一天。但如果在周字段的另一個值后面,他意味著最后一個星期幾在指定月。例如'6L'意味著月的最后一個周五.在使用'L'選項時,指定列表或者范圍是非常重要的,不然容易被結果搞亂。
??? The '#' character is allowed for the day-of-week field. This character is 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.
??? '#'被用在周字段。它用來指定第幾個周幾中激活。如:"6#3"-->月的第三個周五;"2#1"-->月的第一個周一;"4#5"-->月的第五個周三。要注意,如果要使用#后面跟5,但當月并沒有第五周相應的周天,那么job將不被執行(激活);
??? The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".
??? Support for the features described for the 'C' character is not complete
??? 'C'被用在天和周字段中,'C'是'calendar'的縮寫.(不太明白,關于日歷的支持還不完善)
??? Support for specifying both a day-of-week and a day-of-month value is not complete (you'll need to use the '?' character in on of these fields).
??? 同時在周、天中使用'?'還不完善,目前只在兩者中使用一個。
??? Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!
??? 要注意'?'和'*'在周和天字段帶來的影響。
??? 注意以下例子:
??? 1、"0 15 10 * * 6L 2002-2005"?? 在2002至2005年的每月每天的10:15觸發。
??? 2、"0 15 10 ? * 6L 2002-2005"?? 在2002至2005年的每月的最后一個周五觸發。
??? 1中*表示每天,覆蓋了6L(最后一個周五)
?
?
?
?
???