(一)创建服务 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 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 = 555
quartz.scheduler.exporter.bindName = QuartzScheduler
quartz.scheduler.exporter.channelType = tcp
quartz.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.exe
Net Start QuarzService
sc 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