有關(guān)Quartz.NET,與一線碼農(nóng)大佬對個線?

跟[一線碼農(nóng)大佬]翻譯的某技術(shù)文對個線
最近看到一線碼農(nóng)大佬翻譯的《如何在 ASP.NET Core 中使用 Quartz.NET 執(zhí)行任務(wù)調(diào)度》,
行文思路:
安裝Quartz.NET Quartz.NET 中的Job,triggers 和 Schedulers 創(chuàng)建 Scheduler 開啟和停止 scheduler 創(chuàng)建 job 工廠 創(chuàng)建 JobMetadata 存儲你的 job 元數(shù)據(jù)
不可否認(rèn),一線大佬的翻譯文還是相當(dāng)精準(zhǔn)的, 但個人認(rèn)為這篇文章的底稿有點(diǎn)硬輸出,并沒有以一個流暢、直觀的編碼思路來講述[如何在ASP.NET Core中使用Quartz.NET 執(zhí)行定時任務(wù)]。
尤其是下面這段:

想起我之前也寫了《ASP.NET Core+Quartz.Net實(shí)現(xiàn)web定時任務(wù)》, 文章以一個簡單的定時任務(wù)講述了Quartz.NET在ASP.NET Core中的應(yīng)用思路,遇河架橋,遇山開路。
這里我要解釋一下上圖中:為什么要自定義一個Job工廠?
先看下官方JobFactory的作用:
大意是說:
如果某觸發(fā)器被觸發(fā),該觸發(fā)器關(guān)聯(lián)的Job將被調(diào)度器上配置的JobFactory初始化;
Quartz.NET默認(rèn)的SimpleJobFactory工廠類,是利用反射+無參構(gòu)造函數(shù)構(gòu)造出Job實(shí)例。
翻源碼:
//----------------選自Quartz.Simpl.SimpleJobFactory類-------------
using?System;
using?Quartz.Logging;
using?Quartz.Spi;
using?Quartz.Util;
namespace?Quartz.Simpl
{
????///??
????///?The?default?JobFactory?used?by?Quartz?-?simply?calls?
????///?"ObjectUtils.InstantiateType{T}"?/>?on?the?job?class.
????///?
????///?"IJobFactory"?/>
????///?"PropertySettingJobFactory"?/>
????///?James?House
????///?Marko?Lahma?(.NET)
????public?class?SimpleJobFactory?:?IJobFactory
????{
????????private?static?readonly?ILog?log?=?LogProvider.GetLogger(typeof?(SimpleJobFactory));
????????///?
????????///?Called?by?the?scheduler?at?the?time?of?the?trigger?firing,?in?order?to
????????///?produce?a?"IJob"?/>?instance?on?which?to?call?Execute.
????????///?
????????///?
????????///?It?should?be?extremely?rare?for?this?method?to?throw?an?exception?-
????????///?basically?only?the?case?where?there?is?no?way?at?all?to?instantiate
????????///?and?prepare?the?Job?for?execution.??When?the?exception?is?thrown,?the
????????///?Scheduler?will?move?all?triggers?associated?with?the?Job?into?the
????????///?"TriggerState.Error"?/>?state,?which?will?require?human
????????///?intervention?(e.g.?an?application?restart?after?fixing?whatever
????????///?configuration?problem?led?to?the?issue?with?instantiating?the?Job).
????????///?
????????///?"bundle">The?TriggerFiredBundle?from?which?the?"IJobDetail"?/>
????????///???and?other?info?relating?to?the?trigger?firing?can?be?obtained.
????????///?"scheduler">
????????///?the?newly?instantiated?Job
????????///???SchedulerException?if?there?is?a?problem?instantiating?the?Job.?
????????public?virtual?IJob?NewJob(TriggerFiredBundle?bundle,?IScheduler?scheduler)
????????{
????????????IJobDetail?jobDetail?=?bundle.JobDetail;
????????????Type?jobType?=?jobDetail.JobType;
????????????try
????????????{
????????????????if?(log.IsDebugEnabled())
????????????????{
????????????????????log.Debug($"Producing?instance?of?Job?'{jobDetail.Key}',?class={jobType.FullName}");
????????????????}
????????????????return?ObjectUtils.InstantiateType(jobType);
????????????}
????????????catch?(Exception?e)
????????????{
????????????????SchedulerException?se?=?new?SchedulerException($"Problem?instantiating?class?'{jobDetail.JobType.FullName}'",?e);
????????????????throw?se;
????????????}
????????}
????????///?
????????///?Allows?the?job?factory?to?destroy/cleanup?the?job?if?needed.?
????????///?No-op?when?using?SimpleJobFactory.
????????///?
????????public?virtual?void?ReturnJob(IJob?job)
????????{
????????????var?disposable?=?job?as?IDisposable;
????????????disposable?.Dispose();
????????}
????}
}
//------------------節(jié)選自Quartz.Util.ObjectUtils類-------------------------
?public?static?T?InstantiateType(Type?type)
{
?????if?(type?==?null)
?????{
??????????throw?new?ArgumentNullException(nameof(type),?"Cannot?instantiate?null");
?????}
?????ConstructorInfo?ci?=?type.GetConstructor(Type.EmptyTypes);
?????if?(ci?==?null)
?????{
??????????throw?new?ArgumentException("Cannot?instantiate?type?which?has?no?empty?constructor",?type.Name);
?????}
?????return?(T)?ci.Invoke(new?object[0]);
}
但是很多情況下我們定義的Job很可能依賴第三方服務(wù),就比如一線大佬文中NotificationJob依賴了ILogger 服務(wù)。
這樣默認(rèn)的SimpleJobFactory不能滿足實(shí)例化要求, 考慮將Job任務(wù)作為依賴注入組件,加入依賴注入容器。
關(guān)鍵思路:
IScheduler 開放了JobFactory 屬性,便于你應(yīng)用自定義的Job工廠;
在自定義Job工廠中,使用ASP.NET Core依賴注入容器IServiceProvider解析出特定的Job。
JobFactories may be of use to those wishing to have their application produce IJob instances via some special mechanism, such as to give the opportunity for dependency injection
這才有一線碼農(nóng)大佬原文 [創(chuàng)建Job工廠類]動作的由來, ?知其然更知其所以然,如有勘誤,歡迎留言賜教。
Reference:?
