Quartz.net创建windows服务

Quartz.NET 项目地址: http://www.quartz-scheduler.net/

1、创建windows服务项目

 

2、引用Quartz.dll,Topshelf.dll

3、添加quartz.config和quartz_jobs.xml文件,并设置为复制到输出目录为“始终复制”

quartz.config

# You can configure your scheduler in either <quartz> configuration section
# or in quartz properties file
# Configuration section has precedence

quartz.scheduler.instanceName = QuartzTest

# 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 = 777
quartz.scheduler.exporter.bindName = QuartzScheduler
quartz.scheduler.exporter.channelType = tcp
quartz.scheduler.exporter.channelName = httpQuartz
quartz.config

4、添加任务处理类TestJob.cs并继承IJob接口类,实现Execute方法,此方法里面做具体任务逻辑

TestJob.cs

修改quartz_jobs.xml文件,增加定时处理规则。

quartz_jobs.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!-- This file contains job definitions in schema version 2.0 format -->

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

    <!--TestJob测试 任务配置-->
    <job>
      <name>TestJob</name>
      <group>Test</group>
      <description>TestJob测试</description>
      <job-type>QuartzWinService.Job.TestJob,QuartzWinService</job-type>
      <durable>true</durable>
      <recover>false</recover>
    </job>
    <trigger>
      <cron>
        <name>TestJobTrigger</name>
        <group>Test</group>
        <job-name>TestJob</job-name>
        <job-group>Test</job-group>
        <start-time>2015-01-22T00:00:00+08:00</start-time>
        <cron-expression>0/3 * * * * ?</cron-expression>
      </cron>
    </trigger>
    <!--多个 任务配置 配置多个job和trigger-->
     
    
  </schedule>
</job-scheduling-data>
quartz_jobs.xml

5、在service1文件中添加quartz启动

重写OnStart,OnStop,OnPause,OnContinue方法

namespace QuartzWinService
{
    public partial class Service1 : ServiceBase
    {
        private IScheduler scheduler;
        public Service1()
        {
            InitializeComponent();
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
            scheduler = schedulerFactory.GetScheduler();
        }
        protected override void OnStart(string[] args)
        {
            scheduler.Start();
            LogHelp.SysLog("Quartz服务成功启动----", "MyFirstQuartzService");
        }
        protected override void OnStop()
        {
            // 在此处添加代码以执行停止服务所需的关闭操作。
            scheduler.Shutdown();
            LogHelp.SysLog("Quartz服务成功终止", "MyFirstQuartzService");
        }
        protected override void OnPause()
        {
            scheduler.PauseAll();
        }
        protected override void OnContinue()
        {
            scheduler.ResumeAll();
        }
    }
}
Service1.cs

6、添加安装程序

 

修改serviceProcessInstaller1的Account LocalSystem

最后

cmd执行:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe E:\qzsd\QuartzWinService.exe

net start MyQuartzService

pause

 

粗略

可以下代码看下,一目了然

 https://github.com/JsenKe/quartz.git

posted on 2016-05-31 14:05  jsenke  阅读(1923)  评论(0编辑  收藏  举报

导航