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

來源:互聯網
上載者:User

標籤:use   hmm   space   down   reading   lease   sso   ado   let   

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 TopShelfSolution12 {13    14     public sealed class QuartzServiceRunner :ServiceControl,ServiceSuspend15     {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.QuartzJobs11 {12     public sealed class QuartzJobForTest :IJob13     {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 TopShelfSolution11 {12     class Program13     {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服務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.