初识Quartz之Job组件
? ? ? ? Quartz是一個開源的任務調度框架,它有別于Timer,有比Timer更好的性能。由于故障切換以及負載均衡能力使得Quartz框架具有如下特點:
1.強大的調度功能。
2.靈活的應用方式。
3.分布式和集群能力。
以上三個特點使得Quartz在多任務調度以及分布式中具有很大的作用,以下通過Quart框架中幾個重要的組件來了解Quartz框架是如何工作的。關于Quartz框架,我們需要了解的幾個組件的概念是:Job組件系列、Trigger組件系列以及Scheduler系列。本文主要講述的是Job組件系列。
? ? ? ? 首先Job是一個接口,如下代碼所示,該接口只有一個方法,因此Job實現類只需要實現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;}? ? ? ? 創建Job實現類之后,需要將Job實例綁定到JobDetail對象中,其中JobDetail中是一個接口,并繼承了Serializable和Cloneable接口。
public interface JobDetail extends Serializable, Cloneable? ? ? ? 創建JobDetail實例需要調用JobBuilder類中的靜態方法newJob方法,該方法封裝了創建JobBuilder對象的過程。
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;}? ? ? ? 創建JobBuilder對象之后需要綁定Job實現類,通過調用實例方法withIdentity方法來進行綁定的,其中參數group可以缺省。可以看到該方法中通過創建一個JobKey對象,并返回JobBuilder對象,從而實現綁定的。
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>一個泛型類,并且有兩個構造函數:
public JobKey(String name) {super(name, null);}public JobKey(String name, String group) {super(name, group);}? ? ? ?可以看出這兩個構造函數都繼承了父類構造函數,接下來看看Key的源碼,這也解釋了前面withIdentity方法的group參數為何可以缺省,因為它一旦缺省就會默認為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創建JobDetail對象的最核心的方法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方法可以看出,內部創建了一個JobDetail實現類對象,并且將一些相關的信息保存到該對象中并返回。從而實現JobDetail的創建。
? ? ? ? 從上面整個過程來說,首先實現Job接口創建實現類。然后需要創建JobDetail對象,該對象的創建比較復雜,首先調用JobBuilder類的類方法newJob創建對象,并且調用實例方法withIdentity方法該方法是通過內部創建一個JobKey實例,將Job實現類的實例對象綁定其中,最后調用build方法則是為了創建JobDetail實現類的對象,并為對象初始化一些信息。
? ? ? ? 以上就是Job組件系列的關系以及工作流程。
總結
以上是生活随笔為你收集整理的初识Quartz之Job组件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在ArcGIS中自定义坐标系与投影转
- 下一篇: 士兰mos管SVF4N65/7N65/1