1、Nuget
Install-Package Quartz Install-Package Quartz.Plugins
2、站点根目录下加入文件quartz.config、quartz_jobs.xml,文件名称默认:
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.Plugins quartz.plugin.xml.fileNames = ~/quartz_jobs.xml
quartz_jobs.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>TestJob</name> <group>DefaultJobGroup</group> <description>任务描述</description> <job-type>TestQuartzService.TestJob,TestQuartzService</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <cron> <name>TestTrigger</name> <group>DefaultJobTriggerGroup</group> <job-name>TestJob</job-name> <job-group>DefaultJobGroup</job-group> <misfire-instruction>DoNothing</misfire-instruction> <cron-expression>*/5 * * * * ?</cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>
3、创建QuartzFactory.cs,名称自定义:
public static void Start() { StdSchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = factory.GetScheduler().Result; Schedulers.Add(scheduler); scheduler.Start(); }
4、Startup中启动:
QuartzFactory.Start();
5、最后定义业务job,实现IJob接口
public class TestJob : IJob { public Task Execute(IJobExecutionContext context) { return Task.Run(() => { //业务操作 }); } }