1.创建一个定时调度Quartz类
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf; namespace TopShelfSolution
{ public sealed class QuartzServiceRunner :ServiceControl,ServiceSuspend
{
private readonly IScheduler scheduler; public QuartzServiceRunner()
{
scheduler = StdSchedulerFactory.GetDefaultScheduler();
} public bool Start(HostControl hostControl)
{
scheduler.Start();
return true;
} public bool Stop(HostControl hostControl)
{
scheduler.Shutdown(false);
return true;
} public bool Continue(HostControl hostControl)
{
scheduler.ResumeAll();
return true;
} public bool Pause(HostControl hostControl)
{
scheduler.PauseAll();
return true;
} }
}
2.创建用于处理业务逻辑的类
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TopShelfSolution.QuartzJobs
{
public sealed class QuartzJobForTest :IJob
{
public void Execute(IJobExecutionContext context)
{
//处理业务逻辑
Console.WriteLine(DateTime.Now.ToString("yyyyMMddhhmmssfff")); }
}
}
3.创建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 =
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 =
quartz.scheduler.exporter.bindName = QuartzScheduler
quartz.scheduler.exporter.channelType = tcp
quartz.scheduler.exporter.channelName = httpQuartz
4.创建定时job的配置信息xml文件
<?xml version="1.0" encoding="UTF-8"?> <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> <job>
<name>QuartzJobForTestJob</name>
<group>TestJobGroup</group>
<description>Test_Sample job for Quartz</description>
<job-type>TopShelfSolution.QuartzJobs.QuartzJobForTest, TopShelfSolution</job-type>
<durable>true</durable>
<recover>false</recover>
</job> <trigger>
<cron>
<name>QuartzJobForTestJobTrigger</name>
<group>TestJobTriggerGroup</group>
<job-name>QuartzJobForTestJob</job-name>
<job-group>TestJobGroup</job-group>
<!--从start-time起,从0秒开始,每1/5秒执行一次IJob.Execute-->
<start-time>--22T00::+:</start-time>
<cron-expression>/ * * * * ?</cron-expression>
</cron>
</trigger> </schedule> </job-scheduling-data>
5.主函数入口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Topshelf; namespace TopShelfSolution
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(u =>
{
u.Service<QuartzServiceRunner>();
u.RunAsLocalSystem(); u.SetDescription("Sample Topshelf Host服务的描述");
u.SetDisplayName("Stuff显示名称");
u.SetServiceName("Stuff服务名称"); //new Ttask().Start();
});
}
}
}
注:每次修改完quartz_jobs.xml文件后, 需重新生成项目,将quartz_jobs.xml 复制到当前项目bin\Release目录下。