1.Nuget 搜索并安装Quartz.net 2.3.3
2.添加配置到App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <quartz> <add key="quartz.scheduler.instanceName" value="ServerScheduler" /> <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" /> <add key="quartz.threadPool.threadCount" value="10" /> <add key="quartz.threadPool.threadPriority" value="2" /> <add key="quartz.plugin.xml.type" value = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" /> <add key="quartz.plugin.xml.fileNames" value = "quartz_jobs.xml" /> <add key="quartz.jobStore.misfireThreshold" value="60000" /> <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" /> </quartz> </configuration>
3.添加配置文件quartz_jobs.xml,注意,此文件要在属性中设置“始终复制”到输出目录。
<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>FtpJob</name> <group>GeneralGroup</group> <description>Sample job for Quartz Server</description> <job-type>Temperature.Trails.FtpJob, Temperature.Trails</job-type> <durable>true</durable> <recover>false</recover> <job-data-map> <entry> <key>Receiver</key> <value>gogogo@cctv.com</value> </entry> <entry> <key>FtpUser</key> <value>FORMAT</value> </entry> </job-data-map> </job> <trigger> <simple> <name>FtpTrigger</name> <group>FtpTriggerGroup</group> <description>Ftp job trigger</description> <job-name>FtpJob</job-name> <job-group>GeneralGroup</job-group> <misfire-instruction>SmartPolicy</misfire-instruction> <repeat-count>-1</repeat-count> <repeat-interval>10000</repeat-interval> </simple> </trigger> <job> <name>HttpJob</name> <group>GeneralGroup</group> <description>Sample job for Quartz Server</description> <job-type>Temperature.Trails.HttpJob, Temperature.Trails</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <simple> <name>HttpTrigger</name> <group>HttpTriggerGroup</group> <description>Http job trigger</description> <job-name>HttpJob</job-name> <job-group>GeneralGroup</job-group> <misfire-instruction>SmartPolicy</misfire-instruction> <repeat-count>-1</repeat-count> <repeat-interval>10000</repeat-interval> </simple> </trigger> </schedule> </job-scheduling-data>
4.控制台测试DEMO
using System; using Quartz; using Quartz.Impl; namespace Temperature.Trails { class Program { static void Main(string[] args) { StdSchedulerFactory sf = new StdSchedulerFactory(); var sc = sf.GetScheduler(); sc.Start(); Console.ReadLine(); } } public class FtpJob : IJob { public void Execute(IJobExecutionContext context) { var Receiver = context.JobDetail.JobDataMap["Receiver"]; var user = context.JobDetail.JobDataMap["FtpUser"]; Console.WriteLine("ftp upload something with user {0}.",user); } } public class HttpJob : IJob { public void Execute(IJobExecutionContext context) { Console.WriteLine("send a mail and attach something."); } } }