日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

初识Quartz之Job组件

發布時間:2024/3/24 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初识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组件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。