任务调度在我们日常开发过程中非常常见,比如:每天晚上0点自动执行某某操作;每周三晚上2点执行某某操作;......当然,我们处理这类问题的方法也有很多,比如:sql的自动任务;windows上创建任务计划;写windows服务等等。如果系统比较复杂,相互调用比较频繁,任务非常多,几百上千条甚至上万条,那么本身对任务的管理就是比较昂贵的代价;如何提高任务的高可用?任务的测试是否便捷等等问题就会出现。上述的方案是否还能从容应对?
这时我们就迫切地需要一个作业调度系统来处理这些场景。
Quartz.NET是一个强大、开源、轻量的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等。官方网址:https://www.quartz-scheduler.net;GitHub地址:
https://github.com/quartznet/quartznet,各种用法可以参考示例程序。
但如果想方便的知道某个作业执行情况,需要暂停,启动等操作行为,这时候就需要个Job管理的界面。CrystalQuartz可以实现远程管理。
多数系统都会涉及到“后台服务”的开发,一般是为了调度一些自动执行的任务或从队列中消费一些消息,开发 windows service 有一点不爽的是:调试麻烦,当然你还需要知道 windows service 相关的一些开发知识(也不难),TopShelf框架,可以你让 console application 封装为 windows service,这样你就非常方便的开发和调试 windows service。TopShelf框架的官网为Url:http://topshelf-project.com
用TopShelf和quartz.net编写任务,CrystalQuartz管理任务。本文就是搭建一个简易的任务调度方案,启动任务调度、添加Job、移除Job、远程管理等。
1、建立一个名为TaskScheduling的Asp.net MVC项目
2、新建一个类RemoteServerExample,主要代码:

1 //ILog log = LogManager.GetLogger(typeof(RemoteServerExample)); 2 3 NameValueCollection properties = new NameValueCollection(); 4 properties["quartz.scheduler.instanceName"] = "RemoteAutoTaskServer"; 5 6 properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; 7 properties["quartz.threadPool.threadCount"] = "5"; 8 properties["quartz.threadPool.threadPriority"] = "Normal"; 9 10 // 设置服务器 11 properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz"; 12 properties["quartz.scheduler.exporter.port"] = "555"; 13 properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler"; 14 properties["quartz.scheduler.exporter.channelType"] = "tcp"; 15 16 #region 2.3.0版本可用 17 18 //properties["quartz.scheduler.exporter.channelName"] = "httpQuartz"; 19 // 拒绝远程 20 //properties["quartz.scheduler.exporter.rejectRemoteRequests"] = "true"; 21 22 #endregion 2.3.0版本可用 23 24 #region 持久化所用 25 26 //存储类型 27 //properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"; 28 ////表明前缀 29 //properties["quartz.jobStore.tablePrefix"] = "QRTZ_"; 30 ////驱动类型 31 //properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz"; 32 ////数据源名称 33 //properties["quartz.jobStore.dataSource"] = "myDS"; 34 ////连接字符串 35 //properties["quartz.dataSource.myDS.connectionString"] = @"Data Source=(local);Initial Catalog=quartz;User ID=sa;Password=Ayy123"; 36 ////sqlserver版本 37 //properties["quartz.dataSource.myDS.provider"] = "SqlServer-20"; 38 39 #endregion 持久化所用 40 41 ISchedulerFactory sf = new StdSchedulerFactory(properties); 42 IScheduler sched = sf.GetScheduler(); 43 sched.Start(); 44 45 SchedulerMetaData metaData = sched.GetMetaData(); 46 47 IJobDetail job = JobBuilder.Create<SimpleJob>() 48 .WithIdentity("remotelyAddedJob", "default") 49 .Build(); 50 51 JobDataMap map = job.JobDataMap; 52 map.Put("msg", "信息"); 53 54 ITrigger trigger = TriggerBuilder.Create() 55 .WithIdentity("remotelyAddedTrigger", "default") 56 .ForJob(job.Key) 57 .WithCronSchedule("/5 * * ? * *") 58 .Build(); 59 60 string name = sched.SchedulerName; 61 sched.ScheduleJob(job, trigger);
3、新建一个类RemoteClientExample,主要代码:

1 ILog log = LogManager.GetLogger(typeof(RemoteClientExample)); 2 3 NameValueCollection properties = new NameValueCollection(); 4 properties["quartz.scheduler.instanceName"] = "RemoteClient"; 5 6 // 设置线程池 7 properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; 8 properties["quartz.threadPool.threadCount"] = "5"; 9 properties["quartz.threadPool.threadPriority"] = "Normal"; 10 11 // 设置远程连接 12 //properties["quartz.scheduler.proxy"] = "true"; 13 //properties["quartz.scheduler.proxy.address"] = "tcp://127.0.0.1:556/QuartzScheduler"; 14 15 ISchedulerFactory sf = new StdSchedulerFactory(properties); 16 IScheduler scheduler = sf.GetScheduler("RemoteAutoTaskServer"); 17 18 IJobDetail job = JobBuilder.Create<SimpleJob>() 19 .WithIdentity("remotelyAddedJob1", "default") 20 .Build(); 21 22 JobDataMap map = job.JobDataMap; 23 map.Put("msg", "信息"); 24 25 ITrigger trigger = TriggerBuilder.Create() 26 .WithIdentity("remotelyAddedTrigger1", "default") 27 .ForJob(job.Key) 28 .WithCronSchedule("/5 * * ? * *") 29 .Build(); 30 31 string name = scheduler.SchedulerName; 32 scheduler.ScheduleJob(job, trigger); 33 34 log.Info("向服务器添加计划任务");
4、SimpleJob代码:

1 [PersistJobDataAfterExecution] 2 [DisallowConcurrentExecution] 3 public class SimpleJob : IJob 4 { 5 public const string Message = "msg"; 6 7 public virtual void Execute(IJobExecutionContext context) 8 { 9 try 10 { 11 JobKey jobKey = context.JobDetail.Key; 12 string message = context.JobDetail.JobDataMap.GetString(Message); 13 14 string strLog = String.Format("{0} 执行时间 {1}", jobKey, DateTime.Now.ToString()); 15 } 16 catch (Exception ex) 17 { 18 } 19 } 20 }
5、程序运行界面:
点击启动调度器,启动服务器监听且默认有一个名为remotelyAddedJob的job,并且允许远程管理;点击添加任务,会加入一个名为remotelyAddedJob1的job;删除任务会删除所有job。
如果我们已经启动了调度器,有一个任务,比如,我们用TopShelf和quartz.net写了一个windows服务,在本地运行,如果要加入到远程管理中,怎么办呢?
可以按照上述RemoteClientExample代码,配置properties,调用的时候通过反射的方式获取,然后加入到调度器中,进行远程管理。
quartz.net还有持久化、集群等等特性,提高任务的高可用。
分享一个corn表达式生成器:https://files.cnblogs.com/files/net-yuan/CronExpBuilder.zip
大神张善友 的quartz.net系列:https://www.cnblogs.com/shanyou/category/102991.html