码迷,mamicode.com
首页 > Windows程序 > 详细

Topshelf + Quartz2.5 创建基于windows服务

时间:2018-07-16 18:32:04      阅读:325      评论:0      收藏:0      [点我收藏+]

标签: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 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服务

标签:use   hmm   space   down   reading   lease   sso   ado   let   

原文地址:https://www.cnblogs.com/-xyl/p/9319072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!