开发工具与关键技术:后端
作者:汪利燕
撰写时间:2019年5月28日
什么是FluentScheduler:FluentScheduler是一个简单的任务调度框架,定时任务管理器。
下面介绍一些如何使用FluentScheduler定时任务
一、引用插件
①FluentScheduler——②在MVC里面引用——③添加引用——④浏览,找到FluentScheduler这个插件——⑤找到插件,选择到FluentScheduler——⑥添加——⑦添加成功就会显示——⑧确定引用,确定之后就引用成功了。
二、写代码
需要建立一个文件夹装类,要写三个类:①MyRegistry②TestJob③ClearJob还需要用到这个Global.asax
第一步:Global.asax注册定时任务
using System.Web.Routing;
using FluentScheduler;
using ZHSMitem.Timer;
namespace ZHSMitem
{public class MvcApplication : System.Web.HttpApplication
{protected void Application_Start()
{ //注册定时任务
JobManager.Initialize(new MyRegistry());
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
第二步:②TestJob:先给它做我们想要做的工作。
using FluentScheduler;//先引用它的插件
using System.IO;
using System.Text;
namespace ZHSMitem.Timer
{
/// <summary>
/// 定时任务演示类
/// 作用:在定时任务执行后,往D:/log.txt文件输出当前的时间,如果E:/log.txt不存在就创建
/// </summary>
public class TestJob : IJob
{
public void Execute()
{
string path = "D:/log.txt";//选择存在哪里D盘或者E盘
TextWriter textWriter = new StreamWriter(path, true, new UTF8Encoding(false)); textWriter.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
textWriter.Close();
}
}
}
第三步:①MyRegistry给他注册
using FluentScheduler;//先引用插件
namespace ZHSMitem.Timer
{ public class MyRegistry : Registry//给它注册 {
/// <summary>
/// https://github.com/fluentscheduler/FluentScheduler
/// </summary>
public MyRegistry()
{
Schedule(() =>
{
string path = "D:/log.txt";
TextWriter textWriter = new StreamWriter(path, true, new UTF8Encoding(false));textWriter.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
textWriter.Close();
}).ToRunOnceIn(10).Seconds();
ToRunOnceAt(new DateTime(2019, 3, 25, 16, 42, 58));
// 立即执行计划任务,并根据指定时间间隔执行一次计划任务//(指定一个时间间隔运行,根据自己需求,可以是秒、分、时、天、月、年等。)
Schedule<ClearJob>().ToRunNow().AndEvery(10).Seconds();
// 延迟一个指定时间间隔执行一次计划任务。//(当然,这个间隔依然可以是秒、分、时、天、月、年等。)
Schedule<TestJob>().ToRunOnceIn(10).Seconds();
//在一个指定时间执行计划任务 每个小时的第10分钟执行
Schedule<TestJob>().ToRunEvery(1).Hours().At(46);
// 在一个指定时间执行计划任务(最常用。这里是在每天的下午 1:10 分执行)
Schedule<TestJob>().ToRunEvery(1).Days().At(13,10);
//每n年的第几天
Schedule<TestJob>().ToRunEvery(1).Years().On(5).At(12, 00);
//每n年的最后一天
Schedule<TestJob>().ToRunEvery(1).Years().OnTheLastDay();
//每n月的第几天
Schedule<TestJob>().ToRunEvery(1).Months().On(1).At(12,0);
//每n月的第一个星期的星期5 的15:0执行
Schedule<TestJob>().ToRunEvery(1).Months().OnTheFirst(DayOfWeek.Friday).At(15, 0);
//每n月的第一个星期的星期5 的15:0执行 CleanJob和TestJob
Schedule<ClearJob>().AndThen<TestJob>().ToRunEvery(1).Months().OnTheFirst(DayOfWeek.Friday).At(15, 0);
//在每天3点清理数据
Schedule<ClearJob>().ToRunNow().AndEvery(10).Days().At(3,00);
}
}
}(执行黑体的工作,它就会在你的D盘或者E盘显示它执行的工作)
第四步:③ClearJob给它删除
using FluentScheduler;//引用插件
using System.IO;
namespace ZHSMitem.Timer
{
public class ClearJob : IJob
{
public void Execute()
{
//获取项目运行目录+Document\Temp来获取临时目录的绝对路径string str = AppDomain.CurrentDomain.BaseDirectory +"Document\\Temp\\";try
{
//判读是否存在Temp目录
if (Directory.Exists(str))
{
//存在Temp目录,就删除所有的子目录和子文件//删除子文件夹
string[] directories = Directory.GetDirectories(str);
foreach (string directory in directories){
Directory.Delete(directory, true);
} //删子文件
string[] files = Directory.GetFiles(str);
foreach (string file in files){
System.IO.File.Delete(file);
}
}else{
// 不存在,就创建Temp目录
Directory.CreateDirectory(str);
5 }
} catch (Exception e) {
Console.WriteLine(e);
}
}
}
}
(定时任务完成,这篇技术源于一位非常厉害的Teacher,我只是记住了Teacher传授给我的东西,如有不懂可以留言,我会及时讲解)