任务调度在我们日常开发过程中非常常见,比如:每天晚上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,主要代码:
//ILog log = LogManager.GetLogger(typeof(RemoteServerExample));
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteAutoTaskServer";
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";
// 设置服务器
properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz";
properties["quartz.scheduler.exporter.port"] = "555";
properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler";
properties["quartz.scheduler.exporter.channelType"] = "tcp";
#region 2.3.0版本可用
//properties["quartz.scheduler.exporter.channelName"] = "httpQuartz";
// 拒绝远程
//properties["quartz.scheduler.exporter.rejectRemoteRequests"] = "true";
#endregion 2.3.0版本可用
#region 持久化所用
//存储类型
//properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
////表明前缀
//properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
////驱动类型
//properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz";
////数据源名称
//properties["quartz.jobStore.dataSource"] = "myDS";
////连接字符串
//properties["quartz.dataSource.myDS.connectionString"] = @"Data Source=(local);Initial Catalog=quartz;User ID=sa;Password=Ayy123";
////sqlserver版本
//properties["quartz.dataSource.myDS.provider"] = "SqlServer-20";
#endregion 持久化所用
ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = sf.GetScheduler();
sched.Start();
SchedulerMetaData metaData = sched.GetMetaData();
IJobDetail job = JobBuilder.Create<SimpleJob>()
.WithIdentity("remotelyAddedJob", "default")
.Build();
JobDataMap map = job.JobDataMap;
map.Put("msg", "信息");
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("remotelyAddedTrigger", "default")
.ForJob(job.Key)
.WithCronSchedule("/5 * * ? * *")
.Build();
string name = sched.SchedulerName;
sched.ScheduleJob(job, trigger);
3、新建一个类RemoteClientExample,主要代码:
ILog log = LogManager.GetLogger(typeof(RemoteClientExample));
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteClient";
// 设置线程池
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";
// 设置远程连接
//properties["quartz.scheduler.proxy"] = "true";
//properties["quartz.scheduler.proxy.address"] = "tcp://127.0.0.1:556/QuartzScheduler";
ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler scheduler = sf.GetScheduler("RemoteAutoTaskServer");
IJobDetail job = JobBuilder.Create<SimpleJob>()
.WithIdentity("remotelyAddedJob1", "default")
.Build();
JobDataMap map = job.JobDataMap;
map.Put("msg", "信息");
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("remotelyAddedTrigger1", "default")
.ForJob(job.Key)
.WithCronSchedule("/5 * * ? * *")
.Build();
string name = scheduler.SchedulerName;
scheduler.ScheduleJob(job, trigger);
log.Info("向服务器添加计划任务");
4、SimpleJob代码:
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class SimpleJob : IJob
{
public const string Message = "msg";
public virtual void Execute(IJobExecutionContext context)
{
try
{
JobKey jobKey = context.JobDetail.Key;
string message = context.JobDetail.JobDataMap.GetString(Message);
string strLog = String.Format("{0} 执行时间 {1}", jobKey, DateTime.Now.ToString());
}
catch (Exception ex)
{
}
}
}
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
GitHub:https://github.com/anangyang/TaskScheduling