初识Quartz之Job组件
? ? ? ? Quartz是一個(gè)開源的任務(wù)調(diào)度框架,它有別于Timer,有比Timer更好的性能。由于故障切換以及負(fù)載均衡能力使得Quartz框架具有如下特點(diǎn):
1.強(qiáng)大的調(diào)度功能。
2.靈活的應(yīng)用方式。
3.分布式和集群能力。
以上三個(gè)特點(diǎn)使得Quartz在多任務(wù)調(diào)度以及分布式中具有很大的作用,以下通過Quart框架中幾個(gè)重要的組件來了解Quartz框架是如何工作的。關(guān)于Quartz框架,我們需要了解的幾個(gè)組件的概念是:Job組件系列、Trigger組件系列以及Scheduler系列。本文主要講述的是Job組件系列。
? ? ? ? 首先Job是一個(gè)接口,如下代碼所示,該接口只有一個(gè)方法,因此Job實(shí)現(xiàn)類只需要實(shí)現(xiàn)execute方法即可。
public interface Job {/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~* * Interface.* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*//*** <p>* Called by the <code>{@link Scheduler}</code> when a <code>{@link Trigger}</code>* fires that is associated with the <code>Job</code>.* </p>* * <p>* The implementation may wish to set a * {@link JobExecutionContext#setResult(Object) result} object on the * {@link JobExecutionContext} before this method exits. The result itself* is meaningless to Quartz, but may be informative to * <code>{@link JobListener}s</code> or * <code>{@link TriggerListener}s</code> that are watching the job's * execution.* </p>* * @throws JobExecutionException* if there is an exception while executing the job.*/void execute(JobExecutionContext context)throws JobExecutionException;}? ? ? ? 創(chuàng)建Job實(shí)現(xiàn)類之后,需要將Job實(shí)例綁定到JobDetail對(duì)象中,其中JobDetail中是一個(gè)接口,并繼承了Serializable和Cloneable接口。
public interface JobDetail extends Serializable, Cloneable? ? ? ? 創(chuàng)建JobDetail實(shí)例需要調(diào)用JobBuilder類中的靜態(tài)方法newJob方法,該方法封裝了創(chuàng)建JobBuilder對(duì)象的過程。
public static JobBuilder newJob() {return new JobBuilder();}/*** Create a JobBuilder with which to define a <code>JobDetail</code>,* and set the class name of the <code>Job</code> to be executed.* * @return a new JobBuilder*/public static JobBuilder newJob(Class <? extends Job> jobClass) {JobBuilder b = new JobBuilder();b.ofType(jobClass);return b;}? ? ? ? 創(chuàng)建JobBuilder對(duì)象之后需要綁定Job實(shí)現(xiàn)類,通過調(diào)用實(shí)例方法withIdentity方法來進(jìn)行綁定的,其中參數(shù)group可以缺省。可以看到該方法中通過創(chuàng)建一個(gè)JobKey對(duì)象,并返回JobBuilder對(duì)象,從而實(shí)現(xiàn)綁定的。
public JobBuilder withIdentity(String name) {key = new JobKey(name, null);return this;} /*** Use a <code>JobKey</code> with the given name and group to* identify the JobDetail.* * <p>If none of the 'withIdentity' methods are set on the JobBuilder,* then a random, unique JobKey will be generated.</p>* * @param name the name element for the Job's JobKey* @param group the group element for the Job's JobKey* @return the updated JobBuilder* @see JobKey* @see JobDetail#getKey()*/public JobBuilder withIdentity(String name, String group) {key = new JobKey(name, group);return this;}/*** Use a <code>JobKey</code> to identify the JobDetail.* * <p>If none of the 'withIdentity' methods are set on the JobBuilder,* then a random, unique JobKey will be generated.</p>* * @param jobKey the Job's JobKey* @return the updated JobBuilder* @see JobKey* @see JobDetail#getKey()*/public JobBuilder withIdentity(JobKey jobKey) {this.key = jobKey;return this;}? ? ? ? 再來看看JobKey的源碼,它繼承了Key<T>一個(gè)泛型類,并且有兩個(gè)構(gòu)造函數(shù):
public JobKey(String name) {super(name, null);}public JobKey(String name, String group) {super(name, group);}? ? ? ?可以看出這兩個(gè)構(gòu)造函數(shù)都繼承了父類構(gòu)造函數(shù),接下來看看Key的源碼,這也解釋了前面withIdentity方法的group參數(shù)為何可以缺省,因?yàn)樗坏┤笔【蜁?huì)默認(rèn)為DEFAULT_GROUP。
public Key(String name, String group) {if(name == null)throw new IllegalArgumentException("Name cannot be null.");this.name = name;if(group != null)this.group = group;elsethis.group = DEFAULT_GROUP;}? ? ? ? 最后回到JobBuilder創(chuàng)建JobDetail對(duì)象的最核心的方法build方法:
public JobDetail build() {JobDetailImpl job = new JobDetailImpl();job.setJobClass(jobClass);job.setDescription(description);if(key == null)key = new JobKey(Key.createUniqueName(null), null);job.setKey(key); job.setDurability(durability);job.setRequestsRecovery(shouldRecover);if(!jobDataMap.isEmpty())job.setJobDataMap(jobDataMap);return job;}? ? ? ? 從上面build方法可以看出,內(nèi)部創(chuàng)建了一個(gè)JobDetail實(shí)現(xiàn)類對(duì)象,并且將一些相關(guān)的信息保存到該對(duì)象中并返回。從而實(shí)現(xiàn)JobDetail的創(chuàng)建。
? ? ? ? 從上面整個(gè)過程來說,首先實(shí)現(xiàn)Job接口創(chuàng)建實(shí)現(xiàn)類。然后需要?jiǎng)?chuàng)建JobDetail對(duì)象,該對(duì)象的創(chuàng)建比較復(fù)雜,首先調(diào)用JobBuilder類的類方法newJob創(chuàng)建對(duì)象,并且調(diào)用實(shí)例方法withIdentity方法該方法是通過內(nèi)部創(chuàng)建一個(gè)JobKey實(shí)例,將Job實(shí)現(xiàn)類的實(shí)例對(duì)象綁定其中,最后調(diào)用build方法則是為了創(chuàng)建JobDetail實(shí)現(xiàn)類的對(duì)象,并為對(duì)象初始化一些信息。
? ? ? ? 以上就是Job組件系列的關(guān)系以及工作流程。
總結(jié)
以上是生活随笔為你收集整理的初识Quartz之Job组件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在ArcGIS中自定义坐标系与投影转
- 下一篇: 士兰mos管SVF4N65/7N65/1