quartz.net简单入门
前几天使用了quartz.net,今天写一个简单的demo,温习一下。先准备一下dll Common.Logging.dll 和quartz.dll
1.先定义一个job接口,继承自quartz.dll的IJob
namespace ideabinder.web4.Common { interface ICusJob:IJob { } }
2.写一个类实现该接口
namespace ideabinder.web4.Common { public class CVData : ICusJob { public void Execute(Quartz.IJobExecutionContext context) { File.AppendAllLines(@"C:\Users\dqfan\Desktop\ideabinder.com\ideabinder.web4\App_Data\1.txt", new string[] { "hello word","ZY ZY " }); } } }
3.接下来写一个日程类,控制一下时间间隔
namespace ideabinder.web4.Common { public class SystemScheduler { private SystemScheduler() { } public static SystemScheduler CreateInstance() { return new SystemScheduler(); } private IScheduler _scheduler; public void StartScheduler() {
ISchedulerFactory schedulerFactory=new StdSchedulerFactory();//内存调度
_scheduler = schedulerFactory.GetScheduler();
#region IJobDetail eventInteractionDistributeJob = new JobDetailImpl("eventInteractionDistributeJob", typeof(EventInteractionDistributor));
//方式一 //ITrigger eventInteractionDistributeTrigger = TriggerBuilder.Create() // .WithDailyTimeIntervalSchedule( // a => a.WithIntervalInHours(24).OnEveryDay().StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 10)))//每日午夜0点10分调用 // .Build(); //_scheduler.ScheduleJob(eventInteractionDistributeJob, eventInteractionDistributeTrigger); //方式二 ITrigger ttTrigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(a => a.WithIntervalInHours(3)).Build(); _scheduler.ScheduleJob(eventInteractionDistributeJob, ttTrigger); #endregion IJobDetail cvJob = new JobDetailImpl("cvJob", typeof(CVData)); var cvDataJobTimeInterval = Convert.ToInt32(ConfigurationRef.GetConfig("MyScheduler")); var cvDataJobTrigger = new DailyTimeIntervalTriggerImpl("cvDataJobTriggerImplLALA", DateTimeOffset.UtcNow, null, new TimeOfDay(0, 0, 0), new TimeOfDay(23, 59, 00), IntervalUnit.Second, cvDataJobTimeInterval); _scheduler.ScheduleJob(cvJob, cvDataJobTrigger); //计划开始 _scheduler.Start(); } public void StopScheduler() { //结束计划 _scheduler.Shutdown(); } } }
当然这些都是简单的入门及的,quartz.net可以使用配置文件配置一下,这些以后再总结一下