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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM