1. 程式人生 > >Topshelf + Quartz2.5 創建基於windows服務

Topshelf + Quartz2.5 創建基於windows服務

data 業務 des art using ali ati dsc etime

1.創建一個定時調度Quartz類

技術分享圖片
 1 using Quartz;
 2 using Quartz.Impl;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 using Topshelf;
 9 
10 
11 namespace TopShelfSolution
12 {
13    
14     public sealed class QuartzServiceRunner :ServiceControl,ServiceSuspend
15 { 16 private readonly IScheduler scheduler; 17 18 public QuartzServiceRunner() 19 { 20 scheduler = StdSchedulerFactory.GetDefaultScheduler(); 21 } 22 23 public bool Start(HostControl hostControl) 24 { 25 scheduler.Start(); 26 return
true; 27 } 28 29 public bool Stop(HostControl hostControl) 30 { 31 scheduler.Shutdown(false); 32 return true; 33 } 34 35 public bool Continue(HostControl hostControl) 36 { 37 scheduler.ResumeAll(); 38 return true
; 39 } 40 41 public bool Pause(HostControl hostControl) 42 { 43 scheduler.PauseAll(); 44 return true; 45 } 46 47 } 48 }
View Code

2.創建用於處理業務邏輯的類

技術分享圖片
 1 using Quartz;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 
 9 
10 namespace TopShelfSolution.QuartzJobs
11 {
12     public sealed class QuartzJobForTest :IJob
13     {
14         public void Execute(IJobExecutionContext context) 
15         {
16            //處理業務邏輯
17             Console.WriteLine(DateTime.Now.ToString("yyyyMMddhhmmssfff"));
18           
19         }
20     }
21 }
View Code

3.創建quartz.config配置文件

技術分享圖片
# You can configure your scheduler in either <quartz> configuration section  
# or in quartz properties file  
# Configuration section has precedence  
  
quartz.scheduler.instanceName = ServerScheduler  
  
# 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 
  
# 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
View Code

4.創建定時job的配置信息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>QuartzJobForTestJob</name>
    <group>TestJobGroup</group>
    <description>Test_Sample job for Quartz</description>
    <job-type>TopShelfSolution.QuartzJobs.QuartzJobForTest, TopShelfSolution</job-type>
    <durable>true</durable>
    <recover>false</recover>
  </job>

  <trigger>
    <cron>
      <name>QuartzJobForTestJobTrigger</name>
      <group>TestJobTriggerGroup</group>
      <job-name>QuartzJobForTestJob</job-name>
      <job-group>TestJobGroup</job-group>
       <!--從start-time起,從0秒開始,每1/5秒執行一次IJob.Execute--> 
      <start-time>2018-01-22T00:00:00+08:00</start-time>
      <cron-expression>0/2 * * * * ?</cron-expression>
    </cron>
  </trigger>
  
  
</schedule>

</job-scheduling-data>
View Code

5.主函數入口

技術分享圖片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 using System.Threading.Tasks;
 7 using System.Timers;
 8 using Topshelf;
 9 
10 namespace TopShelfSolution
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             HostFactory.Run(u =>
17             {
18                 u.Service<QuartzServiceRunner>();
19                 u.RunAsLocalSystem();
20 
21                 u.SetDescription("Sample Topshelf Host服務的描述");
22                 u.SetDisplayName("Stuff顯示名稱");
23                 u.SetServiceName("Stuff服務名稱");
24 
25                 //new Ttask().Start();
26             });
27         }
28     }
29 }
View Code

註:每次修改完quartz_jobs.xml文件後, 需重新生成項目,將quartz_jobs.xml 復制到當前項目bin\Release目錄下。

Topshelf + Quartz2.5 創建基於windows服務