使用 Topshelf 組件一步一步建立 Windows 服務 (2) 使用Quartz.net 調度

上一篇說了如何使用 Topshelf 組件快速建立Windows服務,接下來介紹如何使用 Quartz.nethtml

關於Quartz.net的好處,網上搜索都是一大把一大把的,我就再也不多介紹。git

先介紹須要用到的插件:express

Quartz版本我用的 2.6.2的, 沒有用3.0以上的,由於你用了就會知道,會打印出一大堆坑爹的日誌文件,安全

我是沒有找到如何屏蔽的辦法,若是大家誰有,歡迎分享出來,我也學習一下,哈哈。tcp

整個項目結構以下:ide

AppConfigHelper 文件須要改動一下,增長以下屬性
 1         /// <summary>
 2         /// 程序標識
 3         /// </summary>
 4         [ConfigurationProperty("AppKey", IsRequired = true)]
 5         public string AppKey
 6         {
 7             get { return base["AppKey"].ToString(); }
 8             internal set { base["AppKey"] = value; }
 9         }
10 
11         /// <summary>
12         /// 程序集信息
13         /// </summary>
14         [ConfigurationProperty("TypeInfo", IsRequired = true)]
15         public string TypeInfo
16         {
17             get { return base["TypeInfo"].ToString(); }
18             internal set { base["TypeInfo"] = value; }
19         }

AppConfig文件也作稍微改動函數

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <!--該節點必定要放在最上邊-->
 4   <configSections>
 5     <section name="AppConfigHelper" type="Quartz.WinService.AppConfigHelper,Quartz.WinService"/>
 6   </configSections>
 7 
 8   <!--TopSelf服務配置文件 -->
 9   <AppConfigHelper
10     ServiceName="ProcessPrintLogService"
11     Desc="日誌打印服務"
12     AppKey="ProcessPrintLogService"
13     TypeInfo="ProcessService.ProcessPrintLogService,ProcessService"
14   />
15 
16   <startup>
17     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
18   </startup>
19 </configuration>
ProcessPrintLogService 就是Windows服務要執行的邏輯程序文件,能夠執行任何你想要的功能
ProcessService.ProcessPrintLogService,ProcessService 是 命名空間.類名,類名  的格式,用於後邊反射程序集用

假如你要執行其餘業務邏輯程序,只須要更換這裏的配置就行,
ProcessPrintLogService 業務邏輯內容以下:這就是咱們要執行的業務邏輯,定時打印一段日誌內容,能夠建立一個類庫,裏邊專門存放你要執行的業務邏輯
 1 namespace ProcessService
 2 {
 3     /// <summary>
 4     /// 日誌打印服務
 5     /// </summary>
 6     public class ProcessPrintLogService
 7     {
 8         private Logger log = LogManager.GetCurrentClassLogger();
 9         /// <summary>
10         /// 服務入口
11         /// </summary>
12         public void DoWork()
13         {
14             //log.Info("******************排行榜服務開始執行******************");
15             try
16             {
17                 PrintLogMethod();
18             }
19             catch (Exception ex)
20             {
21                 log.Error(string.Format("排行榜服務異常,緣由:{0}", ex));
22             }
23             finally
24             {
25                 //log.Info("******************排行榜服務結束執行******************");
26             }
27         }
28 
29 
30         private void PrintLogMethod()
31         {
32             log.Trace(string.Format("我是日誌:{0}號", Thread.CurrentThread.ManagedThreadId));
33         }
34     }
35 }

而後須要新增長兩個文件:quartz.config  和  quartz_jobs.xml學習

quartz.config文件內容以下:ui

# You can configure your scheduler in either <quartz> configuration section
# or in quartz properties file
# Configuration section has precedence

quartz.scheduler.instanceName = ServiceQuartzScheduler

# configure thread pool info
quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
quartz.threadPool.threadCount = 10
quartz.threadPool.threadPriority = Normal

# job initialization plugin handles our xml reading, without it defaults are used
quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
quartz.plugin.xml.fileNames = ~/quartz_jobs.xml

# 3.0以上用如下配置
# quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins
# quartz.plugin.xml.fileNames = ~/quartz_jobs.xml

# export this server to remoting context
# quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
# quartz.scheduler.exporter.port = 555
# quartz.scheduler.exporter.bindName = QuartzScheduler
# quartz.scheduler.exporter.channelType = tcp
# quartz.scheduler.exporter.channelName = httpQuartz
quartz.scheduler.instanceName = ServiceQuartzScheduler 是調度的實例名稱,能夠隨意自定義命名
其餘的都是固定的,不須要修改
quartz_jobs.xml 文件內容以下:
<?xml version="1.0" encoding="UTF-8"?>

<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">

  <processing-directives>
    <overwrite-existing-data>true</overwrite-existing-data>
  </processing-directives>
  <schedule>
    <!--調度配置-->
    <job>
      <name>ProcessPrintLogService</name>
      <group>ProcessPrintLogServiceGroup</group>
      <description>日誌打印服務</description>
      <job-type>Quartz.WinService.QuartzWork,Quartz.WinService</job-type>
      <durable>true</durable>
      <recover>false</recover>
    </job>
    <trigger>
      <cron>
        <name>ProcessPrintLogServiceTrigger</name>
        <group>ProcessPrintLogServiceTriggerGroup</group>
        <job-name>ProcessPrintLogService</job-name>
        <job-group>ProcessPrintLogServiceGroup</job-group>
        <misfire-instruction>SmartPolicy</misfire-instruction>
        <cron-expression>0/3 * * * * ? </cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>

這個xml配置文件很重要! 須要重點說下this

首先 job節點 和 trigger節點 均可以定義多個,也就是一個服務能夠跑多個不一樣的業務邏輯程序

先說 job節點

  • name(必填) 任務名稱,多個job的name不能相同,這裏通常使用業務邏輯程序的名稱就好了
  • group(選填) 任務所屬分組,用於標識任務所屬分組,通常用業務邏輯程序的名稱+Group後綴   如:<group>sampleGroup</group>
  • description(選填) 任務描述,用於描述任務具體內容,如:<description>打印日誌服務</description>
  • job-type(必填) 任務類型,任務的具體類型及所屬程序集,格式:實現了IJob接口的包含完整命名空間的類名,程序集名稱,如:<job-type>Quartz.Server.SampleJob, Quartz.Server</job-type>
  • durable(選填) 具體做用不知,官方示例中默認爲true,如:<durable>true</durable>
  • recover(選填) 具體做用不知,官方示例中默認爲false,如:<recover>false</recover>

這裏的 job-type 節點調用的任務類型須要說下,這裏設置的就是上邊項目結構中的 QuartzWork 類,具體內容以下:

namespace Quartz.WinService
{
    public class QuartzWork : IJob
    {
        private Logger log = LogManager.GetCurrentClassLogger();
        //ConcurrentDictionary是線程安全的字典集
        private readonly ConcurrentDictionary<string, Lazy<Delegate>> _dynamicCache = new ConcurrentDictionary<string, Lazy<Delegate>>();

        //記錄當前工做接口是否已經工做
        private static readonly Dictionary<string, bool> WorkingNow = new Dictionary<string, bool>();

        /// <summary>
        /// 任務調度執行入口
        /// 實現IJob的Execute方法,在Execute方法裏編寫要處理的業務邏輯,系統就會按照Quartz的配置,定時處理
        /// 當Job的trigger觸發的時候, Execute(..) 方法就會在scheduler的工做線程中執行
        /// </summary>
        /// <param name="context"></param>
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                Task.Factory.StartNew(() =>
                {
                    var service = AppConfigHelper.Initity();
                    WorkNow(service);
                });
            }
            catch (Exception ex)
            {
                log.Fatal($"執行Quartz調度異常,信息:{ex.Message}");
            }
            //return Task.FromResult(true);  //返回一個bool類型的Task, Quartz 3.0版本以上須要用到
        }

        private void WorkNow(AppConfigHelper service)
        {
            string key = service.AppKey;  //key值
            lock (this)
            {
                if (!WorkingNow.ContainsKey(key))
                {
                    WorkingNow.Add(key, false);
                }
                //若是執行則跳出
                if (WorkingNow[key])
                {
                    log.Trace($"服務key:{key} 正在運行,這次服務忽略");
                    return;
                }
                //而且設置爲執行狀態
                WorkingNow[key] = true;
            }
            try
            {
                var type = Type.GetType(service.TypeInfo);  //這裏經過App.config文件設置
                if (type != null)
                {
                    //建立指定類型的實例,至關於經過反射new了一個對象實例
                    var provider = Activator.CreateInstance(type);
                    Dynamic(provider, "DoWork", key);
                }
                else
                {
                    log.Error($"任務:{key} 實例化失敗");
                }
            }
            catch (Exception ex)
            {
                log.Fatal($"任務:{key} 實例化異常:{ex.Message}");
            }
            finally
            {
                WorkingNow[key] = false;
            }
        }

        //Delegate.CreateDelegate 官方定義:用來動態建立指定類型的委託,該委託能夠對指定的類實例調用的指定的方法。
        //簡單來講:就是能夠調用指定類裏邊指定的方法,前提是,使用時須要實例化該類
        //GetOrAdd函數會根據指定key判斷是否存在對應內容,存在則返回
        //DynamicInvoke 動態調用委託方法
        //obj參數就是指定類的實例化對象,methodName指定類中的方法名
        private void Dynamic(object obj, string methodName, string key)
        {
            var dmc = _dynamicCache.GetOrAdd(key, t => new Lazy<Delegate>(() => Delegate.CreateDelegate(typeof(Action), obj, methodName)));
            dmc.Value.DynamicInvoke();   //動態調用委託方法
        }

    }
}

接下來講 trigger  節點

trigger 任務觸發器,用於定義使用何種方式出發任務(job),同一個job能夠定義多個trigger ,多個trigger 各自獨立的執行調度,

每一個trigger 中必須且只能定義一種觸發器類型(calendar-interval、simple、cron)

說白些就是,假如你要一個服務分別在 上午 8:00~18:00   和  凌晨 00:00 ~ 6:00  這兩個時間段執行任務,那麼你能夠設置兩個 trigger 觸發器,

分別設置爲這兩個時間段便可實現你要的結果,怎麼樣,很牛X吧

  • name(必填) 觸發器名稱,通常以 業務邏輯類+Trigger結尾, 若是須要設置多個 trigger節點,該名稱不能相同
  • group(選填) 觸發器組  通常以 業務邏輯類+TriggerGroup結尾,多個 trigger節點,該名稱能夠相同
  • job-name(必填) 要調度的任務名稱,該job-name必須和對應job節點中的name名稱徹底相同
  • job-group(選填) 調度任務(job)所屬分組,該值必須和job節點中的group名稱徹底相同
  • misfire-instruction 不知道幹啥用,這麼寫就行  <misfire-instruction>SmartPolicy</misfire-instruction>
  • cron-expression(必填) cron表達式,如:<cron-expression>0/10 * * * * ?</cron-expression>每10秒執行一次

須要注意的是修改了quartz_jobs.xml文件後,quartz服務默認不會從新加載該文件,若要讓修改後的文件生效須要重啓下服務才行。

另外,quartz.config文件 和 quartz_jobs.xml文件 都須要在項目中設置,右鍵-->屬性-->複製到輸出目錄-->始終複製

 

服務註冊文件 RegistService 增長了自動重啓功能,完整內容以下:

namespace Quartz.WinService
{
    public class RegistService
    {
        /// <summary>
        /// 註冊入口
        /// </summary>
        /// <param name="config">配置文件</param>
        /// <param name="isreg">是否註冊</param>
        public static void Regist(AppConfigHelper config, bool isreg = false)
        {
            //這裏也可使用HostFactory.Run()代替HostFactory.New()
            var host = HostFactory.New(x =>
            {
                x.Service<QuartzHost>(s =>
                {
                    //經過 new QuartzHost() 構建一個服務實例 
                    s.ConstructUsing(name => new QuartzHost());
                    //當服務啓動後執行什麼
                    s.WhenStarted(tc => tc.Start());
                    //當服務中止後執行什麼
                    s.WhenStopped(tc => tc.Stop());
                    //當服務暫停後執行什麼
                    s.WhenPaused(w => w.Stop());
                    //當服務繼續後執行什麼
                    s.WhenContinued(w => w.Start());
                });

                if (!isreg) return; //false表示不註冊

                //服務用本地系統帳號來運行
                x.RunAsLocalSystem();

                //啓用自動重啓服務
                x.EnableServiceRecovery(v =>
                {
                    v.RestartService(2);  //2分鐘後重啓
                });

                //服務的描述信息
                x.SetDescription(config.Description);
                //服務的顯示名稱
                x.SetDisplayName(config.ServiceName);
                //服務的名稱(最好不要包含空格或者有空格屬性的字符)Windows 服務名稱不能重複。
                x.SetServiceName(config.ServiceName);
            }).Run();   //啓動服務  若是使用HostFactory.Run()則不須要該方法
        }
    }
}

服務註冊中調用的 QuartzHost 類內容以下:

namespace Quartz.WinService
{
    public class QuartzHost
    {
        private Logger log = LogManager.GetCurrentClassLogger();
        private readonly IScheduler scheduler;
        public QuartzHost()
        {
            //初始化調度服務
            //scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;  //3.0以上寫法
            scheduler = StdSchedulerFactory.GetDefaultScheduler();
        }

        /// <summary>
        /// 調度開始
        /// </summary>
        public void Start()
        {
            try
            {
                scheduler.Start();
                log.Info("Quartz調度服務開始工做");
            }
            catch (Exception ex)
            {
                log.Fatal(string.Format("Quartz調度服務開始異常!錯誤信息:{0}", ex));
                throw;
            }
        }

        /// <summary>
        /// 調度中止
        /// </summary>
        public void Stop()
        {
            try
            {
                if (scheduler != null)
                {
                    scheduler.Shutdown(true);
                }
                log.Info("Quartz調度服務結束工做");
            }
            catch (Exception ex)
            {
                log.Fatal(string.Format("Quartz調度服務中止異常!錯誤信息:{0}", ex));
                throw;
            }
        }
    }
}

項目文件地址:https://gitee.com/gitee_zhang/Quartz.WinService.git


參考文檔:

https://blog.csdn.net/clb929/article/details/90341485

https://blog.csdn.net/weixin_33948416/article/details/92989386

https://www.cnblogs.com/lzrabbit/archive/2012/04/14/2446942.html

相關文章
相關標籤/搜索