<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          有關(guān)Quartz.NET,對(duì)線一線碼農(nóng)大佬!

          共 4280字,需瀏覽 9分鐘

           ·

          2021-02-09 22:08

          跟[一線碼農(nóng)大佬]翻譯的某技術(shù)文對(duì)個(gè)線

          最近看到一線碼農(nóng)大佬翻譯的《如何在 ASP.NET Core 中使用 Quartz.NET 執(zhí)行任務(wù)調(diào)度》,
          行文思路:

          1. 安裝Quartz.NET
          2. Quartz.NET 中的Job,triggers 和 Schedulers
          3. 創(chuàng)建 Scheduler
          4. 開啟和停止 scheduler
          5. 創(chuàng)建 job 工廠
          6. 創(chuàng)建 JobMetadata 存儲(chǔ)你的 job 元數(shù)據(jù)

          不可否認(rèn),一線大佬的翻譯文還是相當(dāng)精準(zhǔn)的, 但個(gè)人認(rèn)為這篇文章的底稿有點(diǎn)硬輸出,并沒有以一個(gè)流暢、直觀的編碼思路來講述[如何在ASP.NET Core中使用Quartz.NET 執(zhí)行定時(shí)任務(wù)]。

          尤其是下面這段:

          想起我之前也寫了《ASP.NET Core+Quartz.Net實(shí)現(xiàn)web定時(shí)任務(wù)》, 文章以一個(gè)簡(jiǎn)單的定時(shí)任務(wù)講述了Quartz.NET在ASP.NET Core中的應(yīng)用思路,遇河架橋,遇山開路。

          這里我要解釋一下上圖中:為什么要自定義一個(gè)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工廠類]動(dòng)作的由來, ?知其然更知其所以然,如有勘誤,歡迎留言賜教。

          Reference:?

          ASP.NET Core+Quartz.Net實(shí)現(xiàn)web定時(shí)任務(wù)

          回復(fù)?【關(guān)閉】學(xué)關(guān)
          回復(fù)?【實(shí)戰(zhàn)】獲取20套實(shí)戰(zhàn)源碼
          回復(fù)?【被刪】學(xué)個(gè)
          回復(fù)?【訪客】學(xué)
          回復(fù)?【小程序】學(xué)獲取15套【入門+實(shí)戰(zhàn)+賺錢】小程序源碼
          回復(fù)?【python】學(xué)微獲取全套0基礎(chǔ)Python知識(shí)手冊(cè)
          回復(fù)?【2019】獲取2019 .NET 開發(fā)者峰會(huì)資料PPT
          回復(fù)?【加群】加入dotnet微信交流群


          你應(yīng)該知道.NET Core的8點(diǎn)總結(jié)!


          百度網(wǎng)盤發(fā)布8周年報(bào)告:男性偏愛在半夜使用,原因是...


          瀏覽 119
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩有码第一页 | 国产内射小视频 | 久久一黄色 | 怒操香蕉 | 天天干视频在线91 |