Fork me on GitHub
Quartz.NET 任务调度框架

在之前的博客中,介绍过Quartz任务调度框架(Java版本)。最近在.NET平台使用了Quartz任务调度框架,并且结合TopShelf框架,把Quartz发布成Windows Service。

今天把示例贴出来,作为自己学习的记录,如果对读者有所帮助,幸甚至哉!

1.资源

1)Quartz 类库

2)TopShelf/TopShelf.Log4Net

3)Common.Logging/Common.Logging.Core/Common.Logging.Log4Net

2.Jobs

1)FirstJob

 

[csharp] view plaincopy
 
  1. using log4net;  
  2. using Quartz;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespace QuartzDemo.QuartzJobs  
  10. {  
  11.     public sealed class FirstJob : IJob  
  12.     {  
  13.         private readonly ILog _logger = LogManager.GetLogger(typeof(FirstJob));  
  14.   
  15.         public void Execute(IJobExecutionContext context)  
  16.         {  
  17.             _logger.InfoFormat("FirstJob executed successfully.");  
  18.         }  
  19.     }  
  20. }  

2)SecondJob

 

 

[csharp] view plaincopy
 
  1. using log4net;  
  2. using Quartz;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespace QuartzDemo.QuartzJobs  
  10. {  
  11.     public sealed class SecondJob : IJob  
  12.     {  
  13.         private readonly ILog _logger = LogManager.GetLogger(typeof(SecondJob));  
  14.   
  15.         public void Execute(IJobExecutionContext context)  
  16.         {  
  17.             _logger.InfoFormat("SecondJob executed successfully.");  
  18.         }  
  19.     }  
  20. }  

3.配置Jobs

 

 

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!-- This file contains job definitions in schema version 2.0 format -->  
  4.   
  5. <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"   
  6.                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">  
  7.   
  8.   <processing-directives>  
  9.     <overwrite-existing-data>true</overwrite-existing-data>  
  10.   </processing-directives>  
  11.   
  12.   <schedule>  
  13.   
  14.     <!--FirstJob 任务配置-->  
  15.     <job>  
  16.       <name>FirstJob</name>  
  17.       <group>First</group>  
  18.       <description>First Job Test</description>  
  19.       <job-type>QuartzDemo.QuartzJobs.FirstJob,QuartzDemo</job-type>  
  20.       <durable>true</durable>  
  21.       <recover>false</recover>  
  22.     </job>  
  23.     <trigger>  
  24.       <cron>  
  25.         <name>FirstJobTrigger</name>  
  26.         <group>First</group>  
  27.         <job-name>FirstJob</job-name>  
  28.         <job-group>First</job-group>  
  29.         <start-time>2015-01-22T00:00:00+08:00</start-time>  
  30.         <cron-expression>0/5 * * * * ?</cron-expression>  
  31.       </cron>  
  32.     </trigger>  
  33.   
  34.     <!--SecondJob 任务配置-->  
  35.     <job>  
  36.       <name>SecondJob</name>  
  37.       <group>Second</group>  
  38.       <description>Second Job Test</description>  
  39.       <job-type>QuartzDemo.QuartzJobs.SecondJob,QuartzDemo</job-type>  
  40.       <durable>true</durable>  
  41.       <recover>false</recover>  
  42.     </job>  
  43.     <trigger>  
  44.       <cron>  
  45.         <name>SecondJobTrigger</name>  
  46.         <group>Second</group>  
  47.         <job-name>SecondJob</job-name>  
  48.         <job-group>Second</job-group>  
  49.         <start-time>2015-01-22T00:00:00+08:00</start-time>  
  50.         <cron-expression>0/3 * * * * ?</cron-expression>  
  51.       </cron>  
  52.     </trigger>  
  53.       
  54.   </schedule>  
  55. </job-scheduling-data>  

4.quartz.config

 

 

[html] view plaincopy
 
  1. # You can configure your scheduler in either <quartz> 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 = 10  
  10. quartz.threadPool.threadPriority = Normal  
  11.   
  12. # job initialization plugin handles our xml reading, without it defaults are used  
  13. quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz  
  14. quartz.plugin.xml.fileNames = ~/quartz_jobs.xml  
  15.   
  16. # export this server to remoting context  
  17. #quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz  
  18. #quartz.scheduler.exporter.port = 555  
  19. #quartz.scheduler.exporter.bindName = QuartzScheduler  
  20. #quartz.scheduler.exporter.channelType = tcp  
  21. #quartz.scheduler.exporter.channelName = httpQuartz  

5.TopShelf

 

服务有启动,停止,继续,暂停等动作,我们使用TopShelf框架很方便地实现。

 

[csharp] view plaincopy
 
  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. namespace QuartzDemo  
  11. {  
  12.     public sealed class ServiceRunner : ServiceControl, ServiceSuspend  
  13.     {  
  14.         private readonly IScheduler scheduler;  
  15.   
  16.         public ServiceRunner()  
  17.         {  
  18.             scheduler = StdSchedulerFactory.GetDefaultScheduler();  
  19.         }  
  20.   
  21.         public bool Start(HostControl hostControl)  
  22.         {  
  23.             scheduler.Start();  
  24.             return true;  
  25.         }  
  26.   
  27.         public bool Stop(HostControl hostControl)  
  28.         {  
  29.             scheduler.Shutdown(false);  
  30.             return true;  
  31.         }  
  32.   
  33.         public bool Continue(HostControl hostControl)  
  34.         {  
  35.             scheduler.ResumeAll();  
  36.             return true;  
  37.         }  
  38.   
  39.         public bool Pause(HostControl hostControl)  
  40.         {  
  41.             scheduler.PauseAll();  
  42.             return true;  
  43.         }  
  44.     }  
  45. }  

6.Log4Net配置

 

 

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <configSections>  
  4.     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>  
  5.   </configSections>  
  6.   
  7.   <log4net>  
  8.     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">  
  9.       <!--日志路径-->  
  10.       <param name= "File" value= "D:\App_Log\servicelog\"/>  
  11.       <!--是否是向文件中追加日志-->  
  12.       <param name= "AppendToFile" value= "true"/>  
  13.       <!--log保留天数-->  
  14.       <param name= "MaxSizeRollBackups" value= "10"/>  
  15.       <!--日志文件名是否是固定不变的-->  
  16.       <param name= "StaticLogFileName" value= "false"/>  
  17.       <!--日志文件名格式为:2008-08-31.log-->  
  18.       <param name= "DatePattern" value= "yyyy-MM-dd".read.log""/>  
  19.       <!--日志根据日期滚动-->  
  20.       <param name= "RollingStyle" value= "Date"/>  
  21.       <layout type="log4net.Layout.PatternLayout">  
  22.         <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />  
  23.       </layout>  
  24.     </appender>  
  25.   
  26.     <!-- 控制台前台显示日志 -->  
  27.     <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">  
  28.       <mapping>  
  29.         <level value="ERROR" />  
  30.         <foreColor value="Red, HighIntensity" />  
  31.       </mapping>  
  32.       <mapping>  
  33.         <level value="Info" />  
  34.         <foreColor value="Green" />  
  35.       </mapping>  
  36.       <layout type="log4net.Layout.PatternLayout">  
  37.         <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />  
  38.       </layout>  
  39.   
  40.       <filter type="log4net.Filter.LevelRangeFilter">  
  41.         <param name="LevelMin" value="Info" />  
  42.         <param name="LevelMax" value="Fatal" />  
  43.       </filter>  
  44.     </appender>  
  45.   
  46.     <root>  
  47.       <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->  
  48.       <level value="all" />  
  49.       <appender-ref ref="ColoredConsoleAppender"/>  
  50.       <appender-ref ref="RollingLogFileAppender"/>  
  51.     </root>  
  52.   </log4net>  
  53. </configuration>  

7.TopShelf启动

 

 

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Topshelf;  
  8.   
  9. namespace QuartzDemo  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             var fileInfo = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");  
  16.             log4net.Config.XmlConfigurator.ConfigureAndWatch(fileInfo);  
  17.             HostFactory.Run(x =>  
  18.             {  
  19.                 x.RunAsLocalSystem();  
  20.                 x.Service<ServiceRunner>();  
  21.                 x.SetDescription("QuartzDemo服务描述");  
  22.                 x.SetDisplayName("QuartzDemo服务显示名称");  
  23.                 x.SetServiceName("QuartzDemo服务名称");  
  24.                 x.EnablePauseAndContinue();  
  25.             });  
  26.         }  
  27.     }  
  28. }  

8.效果

 



安装十分简单,在DOS(系统管理员身份启动)环境下,切换到QuartzDemo.exe所在目录

 

[plain] view plaincopy
 
  1. QuartzDemo.exe install  


服务安装后并启动后如下:

 



http://blog.csdn.net/afandaafandaafanda/article/details/47323799

posted on 2015-11-19 23:33  HackerVirus  阅读(499)  评论(0编辑  收藏  举报