Quartz:首先我贴出来了两段代码(下方),可以看出,首先会根据配置文件(quartz.config),包装出一个Quartz.Core.QuartzScheduler
instance,这是一个调度器,调度各个任务项(Jobs)的执行。这个调度器可以被Start、被Shutdown、被PauseAll、被ResumeAll,这对应
了windows服务的开启、停止、暂停、恢复。当启动服务,我就调用调度器的Start(),停止服务我就调用调度器的Shutdown()方法。
namespace QTDemo{ public class ServiceRunner : ServiceControl, ServiceSuspend { private readonly IScheduler scheduler; public ServiceRunner() { scheduler = StdSchedulerFactory.GetDefaultScheduler(); } public bool Start(HostControl hostControl) { scheduler.Start(); return true; }
// 摘要: // An implementation of Quartz.ISchedulerFactory that does all of it's work // of creating a Quartz.Core.QuartzScheduler instance based on the contents // of a properties file. public class StdSchedulerFactory : ISchedulerFactory { public const string AutoGenerateInstanceId = "AUTO"; public const string ConfigurationSectionName = "quartz"; public const string DefaultInstanceId = "NON_CLUSTERED"; public const string PropertiesFile = "quartz.config";
那TopShelf又扮演了什么样的角色呢
这是一个宿主服务的框架。和ServiceBase、ServiceInstaller那一套的目的一样,都是用来创建Windows服务的。
项目示例
1、新建项目控制台应用程序‘QTDemo’
2、从NuGet安装‘Quartz’,‘Topshelf’,‘Topshelf.Log4Net’
1 23 4 5 6 7 8 9
3、定义一个Job(一个任务项),继承Quartz.IJob
1 using Quartz; 2 namespace QTDemo.QuartzJobs 3 { 4 public sealed class TestJob : IJob 5 { 6 public void Execute(IJobExecutionContext context) 7 { 8 CommonHelper.AppLogger.InfoFormat("TestJob测试"); 9 10 }11 }12 }13
4、配置(新建)quartz.config、quartz_jobs.xml
quartz.config可直接使用,不用修改
1 # You can configure your scheduler in eitherconfiguration section 2 # or in quartz properties file 3 # Configuration section has precedence 4 5 quartz.scheduler.instanceName = QuartzTest 6 7 # configure thread pool info 8 quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz 9 quartz.threadPool.threadCount = 1010 quartz.threadPool.threadPriority = Normal11 12 # job initialization plugin handles our xml reading, without it defaults are used13 quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz14 quartz.plugin.xml.fileNames = ~/quartz_jobs.xml15 16 # export this server to remoting context17 #quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz18 #quartz.scheduler.exporter.port = 55519 #quartz.scheduler.exporter.bindName = QuartzScheduler20 #quartz.scheduler.exporter.channelType = tcp21 #quartz.scheduler.exporter.channelName = httpQuartz
quartz_jobs.xml根据实际的Job项修改
1 2 3 45 6 7 9 10true 811 12 13 3414 21TestJob 15Test 16TestJob测试 17QTDemo.QuartzJobs.TestJob,QTDemo 18true 19false 2022 32 3323 31TestJobTrigger 24Test 25TestJob 26Test 27 282012-01-22T00:00:00+08:00 290/5 * * * * ? 30
5、创建服务
入口:
1 using System; 2 using System.IO; 3 using Topshelf; 4 5 namespace QTDemo 6 { 7 class Program 8 { 9 static void Main(string[] args)10 {11 log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));12 16 HostFactory.Run(x =>17 {18 x.UseLog4Net();19 20 x.Service();21 22 x.RunAsLocalSystem();23 24 x.SetDescription("Quartz+TopShelf实现Windows服务作业调度的一个示例Demo");25 x.SetDisplayName("QuartzTopShelfDemo服务");26 x.SetServiceName("QuartzTopShelfDemoService");27 28 x.EnablePauseAndContinue();29 30 });31 }32 }33 }
ServiceRunner.cs
1 using Quartz; 2 using Quartz.Impl; 3 using Topshelf; 4 5 namespace QTDemo 6 { 7 public class ServiceRunner : ServiceControl, ServiceSuspend 8 { 9 private readonly IScheduler scheduler;10 11 public ServiceRunner()12 {13 scheduler = StdSchedulerFactory.GetDefaultScheduler();14 }15 16 public bool Start(HostControl hostControl)17 {18 scheduler.Start();19 return true;20 }21 22 public bool Stop(HostControl hostControl)23 {24 scheduler.Shutdown(false);25 return true;26 }27 28 public bool Continue(HostControl hostControl)29 {30 scheduler.ResumeAll();31 return true;32 }33 34 public bool Pause(HostControl hostControl)35 {36 scheduler.PauseAll();37 return true;38 }39 40 }41 }
log4net配置文件,可直接使用
1 23 4 5 6 78 549 10 11 12 13 14 15 16 17 18 19 20 21 25 26 2722 23 2428 45 4629 3230 31 33 3634 35 37 39 4038 41 42 43 4447 48 52 5349 50 51
6、quartz.config、quartz_jobs.xml、log4net.config都设置成‘始终复制’
7、直接F5,调试,发现TestJob每5s执行一次
8、安装成Windows服务
用Release编一个版本
然后用命令行安装服务
服务面板里可以找到此服务
启动之,查看日志文件,服务运行正常
23:23:52,171 INFO -Configuration Result:[Success] Name QuartzTopShelfDemoService[Success] DisplayName QuartzTopShelfDemo服务[Success] Description Quartz+TopShelf实现Windows服务作业调度的一个示例Demo[Success] ServiceName QuartzTopShelfDemoService23:23:52,185 INFO -Topshelf v3.3.154.0, .NET Framework v4.0.30319.3401423:23:52,194 ERROR-The QuartzTopShelfDemoService service can only be installed as an administrator23:25:54,758 INFO -Configuration Result:[Success] Name QuartzTopShelfDemoService[Success] DisplayName QuartzTopShelfDemo服务[Success] Description Quartz+TopShelf实现Windows服务作业调度的一个示例Demo[Success] ServiceName QuartzTopShelfDemoService23:25:54,772 INFO -Topshelf v3.3.154.0, .NET Framework v4.0.30319.3401423:25:54,781 DEBUG-Attempting to install 'QuartzTopShelfDemoService'23:25:54,901 INFO -Installing QuartzTopShelfDemo服务 service23:25:55,123 DEBUG-Opening Registry23:25:55,123 DEBUG-Service path: "E:\DotNetProject\Y2016\QTDemo\QTDemo\bin\Release\QTDemo.exe"23:25:55,123 DEBUG-Image path: "E:\DotNetProject\Y2016\QTDemo\QTDemo\bin\Release\QTDemo.exe" -displayname "QuartzTopShelfDemo服务" -servicename "QuartzTopShelfDemoService"23:25:58,357 DEBUG-Closing Registry23:28:10,442 INFO -Configuration Result:[Success] Name QuartzTopShelfDemoService[Success] DisplayName QuartzTopShelfDemo服务[Success] Description Quartz+TopShelf实现Windows服务作业调度的一个示例Demo[Success] ServiceName QuartzTopShelfDemoService23:28:10,455 INFO -Topshelf v3.3.154.0, .NET Framework v4.0.30319.3401423:28:10,649 DEBUG-Started by the Windows services process23:28:10,649 DEBUG-Running as a service, creating service host.23:28:10,651 INFO -Starting as a Windows service23:28:10,654 DEBUG-[Topshelf] Starting up as a windows service application23:28:10,657 INFO -[Topshelf] Starting23:28:10,658 DEBUG-[Topshelf] Current Directory: E:\DotNetProject\Y2016\QTDemo\QTDemo\bin\Release23:28:10,658 DEBUG-[Topshelf] Arguments: 23:28:10,940 INFO -[Topshelf] Started23:28:10,988 INFO -TestJob测试23:28:15,000 INFO -TestJob测试23:28:20,000 INFO -TestJob测试
附: