Quartz (Net) 简介(Quartz.Net定时任务简单实用(实例)) 摘录地址 :https://www.cnblogs.com/jiufeilang/archive/2017/09/21/7566021.html 待完成()
Quartz.Net定时任务简单实用(实例)
Quartz 是一个任务调度系统。
一、Quartz.Net是什么?
Quartz.Net是一个定时任务框架
二、有Timer了,为什么需要用Quartz.Net?
Quartz.Net比Timer使用起来更灵活。例如:每个月最后一个星期天23:59分做什么事情,而Timer实现起来就会麻烦很多。
三、Quartz.Net怎么用?
《一、无配置实现》
1. 下载引用3个DLL:Quartz.dll、Common.Logging.dll、Common.Logging.Core.dll
下载地址:http://pan.baidu.com/s/1eRC5Iwi
2.创建任务操作类:
1 public class TimingJob : IJob
2 {
3 public void Execute(IJobExecutionContext context)
4 {
5 //将要定时执行的逻辑代码写于此处
6 Console.WriteLine(DateTime.Now.ToString()+"\t工作太久了,休息10分钟吧。");
7 }
8 }
3.创建处理类、触发器类
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 ISchedulerFactory sf = new StdSchedulerFactory();
6 IScheduler scheduler = sf.GetScheduler(); //创建调度实例
7 //创建任务实例
8 IJobDetail job = JobBuilder.Create<TimingJob>().WithIdentity(new JobKey("job1")).Build();
9 //创建触发器实例
10 ITrigger trigger = TriggerBuilder.Create().StartAt(DateTime.Now.AddSeconds(0)).WithCronSchedule("59 59 23 ? * 1L ").Build();
11 scheduler.ScheduleJob(job, trigger); //绑定触发器和任务
12 scheduler.Start(); //启动监控
13 }
14 }
《二、通过配置文件实现》
1. 下载引用3个DLL:Quartz.dll、Common.Logging.dll、Common.Logging.Core.dll
下载地址:http://pan.baidu.com/s/1eRC5Iwi
2. 创建工作类:
1 public class PrintTimeJob : IJob
2 {
3 public void Execute(IJobExecutionContext context)
4 {
5 Console.WriteLine(DateTime.Now.ToString()+"工作了很久,休息一会吧!");
6 }
7 }
3. 工作类、触发器类的配置文件(quartz_jobs.xml)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> 3 <processing-directives> 4 <overwrite-existing-data>true</overwrite-existing-data> 5 </processing-directives> 6 <schedule> <!--工作类配置--> 7 <job> 8 <name>PrintTimeJob</name> 9 <group>PrintGroup</group> 10 <description>RecyclingResourcesJob</description> 11 <job-type>SettingMothod.PrintTimeJob, SettingMothod</job-type> 12 <durable>true</durable> 13 <recover>false</recover> 14 </job> 15 <trigger> <!--触发器类配置--> 16 <cron> 17 <name>PrintDateTimeTrigger</name> 18 <group>PrintDateTimeTriggerGroup</group> 19 <job-name>PrintTimeJob</job-name> 20 <job-group>PrintGroup</job-group> 21 <cron-expression>59 59 23 ? * 1L </cron-expression> // 22 </cron> 23 </trigger> 24 </schedule> 25 </job-scheduling-data>
4. 设置配置文件(App.config)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="quartz" type="System.Configuration.NameValueSectionHandler"/> 5 </configSections> 6 <quartz> 7 <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/> 8 <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/> 9 <add key="quartz.threadPool.threadCount" value="10"/> 10 <add key="quartz.threadPool.threadPriority" value="2"/> 11 <add key="quartz.jobStore.misfireThreshold" value="60000"/> 12 <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/> 13 <!--******************************Plugin配置*********************************************--> 14 <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" /> 15 <add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml"/> 16 </quartz> 17 </configuration>
5. 实例化、启动任务调度。
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 ISchedulerFactory sf = new StdSchedulerFactory();
6 IScheduler sched = sf.GetScheduler(); //创建调度实例
7 sched.Start(); //开启调度
8 Console.WriteLine("启动成功 ");
9 Console.ReadKey();
10 }
11 }

浙公网安备 33010602011771号