(一)创建服务 QuarzService
using System.ServiceProcess;using System.Text;
using Quartz;using Quartz.Impl;
using WinNet.Log;namespace QuarzService{ public partial class QuartzService : ServiceBase { private IScheduler scheduler;
public QuartzService() { InitializeComponent(); ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); scheduler = schedulerFactory.GetScheduler();
}
protected override void OnStart(string[] args) { scheduler.Start(); WriteLog.logTxt("Quartz服务成功启动", "scheduler"); }
protected override void OnStop() { scheduler.Shutdown(); WriteLog.logTxt("Quartz服务成功终止", "scheduler"); }
protected override void OnPause() { scheduler.PauseAll(); }
protected override void OnContinue() { scheduler.ResumeAll(); } }}
(二)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 infoquartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartzquartz.threadPool.threadCount = 10quartz.threadPool.threadPriority = Normal
# job initialization plugin handles our xml reading, without it defaults are usedquartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartzquartz.plugin.xml.fileNames = ~/quartz_jobs.xml
# export this server to remoting contextquartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartzquartz.scheduler.exporter.port = 555quartz.scheduler.exporter.bindName = QuartzSchedulerquartz.scheduler.exporter.channelType = tcpquartz.scheduler.exporter.channelName = httpQuartz
写日志方法:
using System.Text;using System.Windows.Forms;using System.IO;
namespace WinNet.Log{ public class WriteLog { public WriteLog() { }
public static void logTxt(string Msg, string name) { string logPath = Path.GetDirectoryName(Application.ExecutablePath); System.IO.StreamWriter sw = System.IO.File.AppendText(logPath + "/" + name + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + Msg); sw.Close(); sw.Dispose(); } }}
(三)创建一个任务:WinNet.JobTask
using Quartz;
using WinNet.Log;
namespace WinNet.JobTask{ public class JobGeneral:IJob { //private static readonly Common.Logging.ILog logger = Common.Logging.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #region IJob Members
public void Execute(IJobExecutionContext context) { try { WriteLog.logTxt("DemoJob1 任务开始运行", "scheduler"); for (int i = 0; i < 10; i++) { WriteLog.logTxt("DemoJob1 正在运行 " + i, "scheduler"); }
WriteLog.logTxt("DemoJob1任务运行结束", "scheduler"); } catch (Exception ex) { WriteLog.logTxt("Error:"+ex.ToString (), "scheduler"); } }
#endregion }}
(四)安装与卸载服务
安装服务,写成bat 文件
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe QuarzService.exeNet Start QuarzServicesc config QuarzService start= auto
卸载服务,写成bat 文件
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u QuarzService.exe
源码:http://files.cnblogs.com/files/aran/ServiceSchedulerDesign.rar
转载于:https://www.cnblogs.com/aran/p/4380400.html
