使用普通Windows服务创建Quartz.Net服务项目
- 首先创建Quartz.Net.2.0解决方案,添加 Windows服务 项目,添加安装程序,修改服务运行账户类型为LocalSystem(默认为User)
- 添加C5.dll、Common.Logging.dll、Common.Logging.Log4Net.dll、log4net.dll、Quartz.dll引用
C5.dll 一个C#和其他CLI语言的泛型集合类。.Net2.0及以上才可以使用。简介地址:http://www.itu.dk/research/c5/ Common.Logging.dll 通用日志接口 Common.Logging.Log4Net.dll 提供log4net对通用日志接口(Common.Logging)的实现 log4net.dll 小白都知道不解释
- 添加quartz.config文件 Quartz.Net的配置文件,并设置编译时生成到输出目录为始终复制
- Windows服务文件上右键查看代码,编写Quartz服务启动代码,重写OnStart、OnStop、OnPause、OnContinue方法,分别对应IScheduler的Start、Shutdown、PauseAll、ResumeAll方法,详见代码
QuartzService.cs
using System.ServiceProcess; using Common.Logging; using Quartz.Impl; namespace Quartz.Net.Service { public partial class QuartzService : ServiceBase { private readonly ILog logger; private IScheduler scheduler; public QuartzService() { InitializeComponent(); logger = LogManager.GetLogger(GetType()); ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); scheduler = schedulerFactory.GetScheduler(); } protected override void OnStart(string[] args) { scheduler.Start(); logger.Info("Quartz服务成功启动"); } protected override void OnStop() { scheduler.Shutdown(); logger.Info("Quartz服务成功终止"); } protected override void OnPause() { scheduler.PauseAll(); } protected override void OnContinue() { scheduler.ResumeAll(); } } }
至此Quarzt.Net 的Windows服务已成功创建,接下创建实现了IJob接口Quartz任务
- 添加Quartz.Net.Demo类库项目
- 添加Common.Logging.dll、log4net.dll、Quartz.dll的引用(实际项目中Common.Logging.dll和log4net.dll任意引用一个即可,此处为演示需要)
- 添加DemoJob1.cs,实现IJob接口,日志记录使用Common.Logging接口
DemoJob1.cs
using System; namespace Quartz.Net.Demo { /// <summary> /// 实现IJob接口 /// </summary> public class DemoJob1 : IJob { //使用Common.Logging.dll日志接口实现日志记录 private static readonly Common.Logging.ILog logger = Common.Logging.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #region IJob 成员 public void Execute(IJobExecutionContext context) { try { logger.Info("DemoJob1 任务开始运行"); for (int i = 0; i < 10; i++) { logger.InfoFormat("DemoJob1 正在运行{0}", i); } logger.Info("DemoJob1任务运行结束"); } catch (Exception ex) { logger.Error("DemoJob2 运行异常", ex); } } #endregion } }
- 添加DemoJob2.cs,实现IJob接口,日志记录使用log4net
DemoJob2.cs
using System; namespace Quartz.Net.Demo { /// <summary> /// 实现IJob接口 /// </summary> public class DemoJob2 : IJob { //使用log4net.dll日志接口实现日志记录 private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #region IJob 成员 public void Execute(IJobExecutionContext context) { try { logger.Info("DemoJob2 任务开始运行"); for (int i = 0; i < 10; i++) { logger.InfoFormat("DemoJob2 正在运行{0}", i); } logger.Info("DemoJob2任务运行结束"); } catch (Exception ex) { logger.Error("DemoJob2 运行异常", ex); } } #endregion } }
- 添加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> <!--定义示例任务1 Job--> <job> <name>DemoJob1</name> <group>DeomJobGroup</group> <description>Quartz.Net示例任务1</description> <job-type>Quartz.Net.Demo.DemoJob1,Quartz.Net.Demo</job-type> <durable>true</durable> <recover>false</recover> </job> <!--定义示例任务2 Job--> <job> <name>DemoJob2</name> <group>DeomJobGroup</group> <description>Quartz.Net示例任务2</description> <job-type>Quartz.Net.Demo.DemoJob2,Quartz.Net.Demo</job-type> <durable>true</durable> <recover>false</recover> </job> <!--定义示例任务1 触发器 每30秒执行一次DemoJob1任务--> <trigger> <cron> <name>DemoJob1Trigger</name> <group>DeomJobTriggerGroup</group> <job-name>DemoJob1</job-name> <job-group>DeomJobGroup</job-group> <cron-expression>0/30 * * * * ?</cron-expression> </cron> </trigger> <!--定义示例任务2 触发器 每分钟执行一次DemoJob2任务--> <trigger> <cron> <name>DemoJob2Trigger1</name> <group>DeomJobTriggerGroup</group> <job-name>DemoJob2</job-name> <job-group>DeomJobGroup</job-group> <cron-expression>0 * * * * ?</cron-expression> </cron> </trigger> <!--定义示例任务2 触发器 每天凌晨01:00执行一次DemoJob2任务--> <trigger> <cron> <name>DemoJob2Trigger2</name> <group>DeomJobTriggerGroup</group> <job-name>DemoJob2</job-name> <job-group>DeomJobGroup</