博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Quartz+TopShelf实现Windows服务作业调度
阅读量:5327 次
发布时间:2019-06-14

本文共 8515 字,大约阅读时间需要 28 分钟。

  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 
2
3
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 either 
configuration 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
View Code

quartz_jobs.xml根据实际的Job项修改

1 
2
3 4
5 6
7
true
8
9 10
11 12
13
14
TestJob
15
Test
16
TestJob测试
17
QTDemo.QuartzJobs.TestJob,QTDemo
18
true
19
false
20
21
22
23
TestJobTrigger
24
Test
25
TestJob
26
Test
27
28
2012-01-22T00:00:00+08:00
29
0/5 * * * * ?
30
31
32 33
34

 

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 
2
3
4
5
6 7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 26
27
28
29
30
31
32
33
34
35
36
37
38
39 40
41
42
43
44
45 46
47
48
49
50
51
52 53
54
View Code 

 

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测试

 

  附:

 

转载于:https://www.cnblogs.com/frozenzhang/p/5443778.html

你可能感兴趣的文章
JSP指令、动作和对象
查看>>
自然语言处理--中文文本向量化counterVectorizer()
查看>>
luogu_4551【题解】最长异或路径 trie树
查看>>
每周必写
查看>>
文件操作其他方法
查看>>
jQuery插件之ajaxFileUpload
查看>>
MMO-SNS类游戏服务器间数据交互策略分享
查看>>
记录:protobuf在网游中的用法
查看>>
【C++】指针的引用及面向对象
查看>>
《设计模式之禅》--摘要
查看>>
poj2976(二分搜索,最大化平均值)
查看>>
24种设计模式
查看>>
[bzoj3532][Sdoi2014]Lis
查看>>
HDU 2546 饭卡 动态规划01背包
查看>>
devexpress TreeList递归及点击获取数据
查看>>
记一次渗透测试某路由器
查看>>
使用gatling做压力测试与负载测试
查看>>
[转载]Linux shell中的竖线(|)——管道符号
查看>>
python 生成器和迭代器
查看>>
集合与数组,集合与集合之间的转换
查看>>