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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

有关Quartz.NET,与一线码农大佬对个线?

發布時間:2023/12/4 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 有关Quartz.NET,与一线码农大佬对个线? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

跟[一線碼農大佬]翻譯的某技術文對個線

最近看到一線碼農大佬翻譯的《如何在 ASP.NET Core 中使用 Quartz.NET 執行任務調度》,
行文思路:

  • 安裝Quartz.NET

  • Quartz.NET 中的Job,triggers 和 Schedulers

  • 創建 Scheduler

  • 開啟和停止 scheduler

  • 創建 job 工廠

  • 創建 JobMetadata 存儲你的 job 元數據

  • 不可否認,一線大佬的翻譯文還是相當精準的, 但個人認為這篇文章的底稿有點硬輸出,并沒有以一個流暢、直觀的編碼思路來講述[如何在ASP.NET Core中使用Quartz.NET 執行定時任務]。

    尤其是下面這段:

    想起我之前也寫了《ASP.NET Core+Quartz.Net實現web定時任務》, 文章以一個簡單的定時任務講述了Quartz.NET在ASP.NET Core中的應用思路,遇河架橋,遇山開路。

    這里我要解釋一下上圖中:為什么要自定義一個Job工廠?

    先看下官方JobFactory的作用:

    大意是說:

    如果某觸發器被觸發,該觸發器關聯的Job將被調度器上配置的JobFactory初始化;

    Quartz.NET默認的SimpleJobFactory工廠類,是利用反射+無參構造函數構造出Job實例。

    翻源碼:

    //----------------選自Quartz.Simpl.SimpleJobFactory類------------- using?System; using?Quartz.Logging; using?Quartz.Spi; using?Quartz.Util; namespace?Quartz.Simpl {///?<summary>?///?The?default?JobFactory?used?by?Quartz?-?simply?calls?///?<see?cref="ObjectUtils.InstantiateType{T}"?/>?on?the?job?class.///?</summary>///?<seealso?cref="IJobFactory"?/>///?<seealso?cref="PropertySettingJobFactory"?/>///?<author>James?House</author>///?<author>Marko?Lahma?(.NET)</author>public?class?SimpleJobFactory?:?IJobFactory{private?static?readonly?ILog?log?=?LogProvider.GetLogger(typeof?(SimpleJobFactory));///?<summary>///?Called?by?the?scheduler?at?the?time?of?the?trigger?firing,?in?order?to///?produce?a?<see?cref="IJob"?/>?instance?on?which?to?call?Execute.///?</summary>///?<remarks>///?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///?<see?cref="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).///?</remarks>///?<param?name="bundle">The?TriggerFiredBundle?from?which?the?<see?cref="IJobDetail"?/>///???and?other?info?relating?to?the?trigger?firing?can?be?obtained.</param>///?<param?name="scheduler"></param>///?<returns>the?newly?instantiated?Job</returns>///?<throws>??SchedulerException?if?there?is?a?problem?instantiating?the?Job.?</throws>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<IJob>(jobType);}catch?(Exception?e){SchedulerException?se?=?new?SchedulerException($"Problem?instantiating?class?'{jobDetail.JobType.FullName}'",?e);throw?se;}}///?<summary>///?Allows?the?job?factory?to?destroy/cleanup?the?job?if?needed.?///?No-op?when?using?SimpleJobFactory.///?</summary>public?virtual?void?ReturnJob(IJob?job){var?disposable?=?job?as?IDisposable;disposable?.Dispose();}} }//------------------節選自Quartz.Util.ObjectUtils類-------------------------public?static?T?InstantiateType<T>(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很可能依賴第三方服務,就比如一線大佬文中NotificationJob依賴了ILogger<NotificationJob> 服務。

    這樣默認的SimpleJobFactory不能滿足實例化要求, 考慮將Job任務作為依賴注入組件,加入依賴注入容器。

    關鍵思路:

    IScheduler 開放了JobFactory 屬性,便于你應用自定義的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

    這才有一線碼農大佬原文 [創建Job工廠類]動作的由來, ?知其然更知其所以然,如有勘誤,歡迎留言賜教。

    Reference:?

    ASP.NET Core+Quartz.Net實現web定時任務?

    總結

    以上是生活随笔為你收集整理的有关Quartz.NET,与一线码农大佬对个线?的全部內容,希望文章能夠幫你解決所遇到的問題。

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