之前定时任务一直用的Windows服务,前段时间发现FluentScheduler这个框架,他跟Quarz.Net,Hangfire一样都是任务调度框架,但是相对使用而言我觉得FluentScheduler更加方便简单一些.

1.新建一个mvc项目

2.nuget直接安装FluentScheduler

3.新建一个类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace FluentSchedulerWeb
{
using FluentScheduler;
using System.Diagnostics;
using System.IO;
using System.Threading;
public class MyJob : Registry
{
public MyJob()
{
// 每2秒一次循环
Schedule<MyJob1>().ToRunNow().AndEvery().Seconds(); // 5秒后,只一次
Schedule<MyOtherJob>().ToRunOnceIn().Seconds(); //每天晚上21:15分执行
Schedule(() => Console.WriteLine("Timed Task - Will run every day at 9:15pm: " + DateTime.Now)).ToRunEvery().Days().At(, ); // 每个月的执行
Schedule(() =>
{
Console.WriteLine("Complex Action Task Starts: " + DateTime.Now);
Thread.Sleep();
Console.WriteLine("Complex Action Task Ends: " + DateTime.Now);
}).ToRunNow().AndEvery().Months().OnTheFirst(DayOfWeek.Monday).At(, ); //先执行第一个Job、再执行第二个Job;完成后等5秒继续循环
Schedule<MyFirstJob>().AndThen<MySecoundJob>().ToRunNow().AndEvery().Seconds();
}
} public class MyJob1 : IJob
{ void IJob.Execute()
{
Trace.WriteLine("循环每2秒执行一次;现在时间是:" + DateTime.Now);
var str = "循环每2秒执行一次;现在时间是:" + DateTime.Now.ToString();
System.IO.StreamWriter writer = null;
try
{ //写入日志
string path = string.Empty;
path = @"F:\ErrorLogs\";
//不存在则创建错误日志文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += string.Format(@"\{0}.txt", DateTime.Now.ToString("yyyy-MM-dd")); writer = !System.IO.File.Exists(path) ? System.IO.File.CreateText(path) : System.IO.File.AppendText(path); //判断文件是否存在,如果不存在则创建,存在则添加
writer.WriteLine(str);
writer.WriteLine("********************************************************************************************");
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
} public class MyOtherJob : IJob
{ void IJob.Execute()
{
Trace.WriteLine("5秒后只执行一次 ,现在时间是:" + DateTime.Now);
}
} public class MyFirstJob : IJob
{ void IJob.Execute()
{
Trace.WriteLine("执行第一个 Job ,现在时间是:" + DateTime.Now);
}
}
public class MySecoundJob : IJob
{ void IJob.Execute()
{
Trace.WriteLine("执行第二个 Job ,现在时间是:" + DateTime.Now);
}
} }

4.Global.asax中启用FluentScheduler

 protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
//启用
JobManager.Initialize(new MyJob());
}

这样FluentScheduler就可以使用了

当然在使用过程中发布到iis必然面临着iis回收的问题

下面两种方法亲测可用

方法一.IIS预加载

(1)修改应用程序池启动模式

(2)开启对应网站预加载(IIS7.5没有这个)

(3).增加配置编辑器,编写默认预加载的请求页面

hostName是地址,initializationPage是要加载的页面

这样预加载就搞定了

方法二:通过代码实现iis回收之后自动访问页面来唤醒服务

using FluentScheduler;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing; namespace FluentSchedulerWeb2
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
//启用
JobManager.Initialize(new MyJob());
} protected void Application_End()
{
WriteLog("进程即将被IIS回收"+DateTime.Now);
WriteLog("重新访问一个页面,以唤醒服务" + DateTime.Now);
string strURL = System.Configuration.ConfigurationManager.AppSettings["homeURL"].ToString();
try
{
System.Net.WebClient wc = new System.Net.WebClient();
System.IO.Stream stream = wc.OpenRead(strURL);
System.IO.StreamReader reader = new StreamReader(stream);
string html = reader.ReadToEnd();
if (!string.IsNullOrWhiteSpace(html))
{
WriteLog("唤醒程序成功" + DateTime.Now);
}
reader.Close();
reader.Dispose();
stream.Close();
stream.Dispose();
wc.Dispose();
}
catch (Exception ex)
{
WriteLog("唤醒异常"+ex.Message+DateTime.Now);
}
} public void WriteLog(string str)
{
System.IO.StreamWriter writer = null;
try
{
//写入日志
string path = string.Empty;
path = @"F:\ErrorLogs\";
//不存在则创建错误日志文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += string.Format(@"\{0}.txt", DateTime.Now.ToString("yyyy-MM-dd")); writer = !System.IO.File.Exists(path) ? System.IO.File.CreateText(path) : System.IO.File.AppendText(path); //判断文件是否存在,如果不存在则创建,存在则添加
writer.WriteLine(str);
writer.WriteLine("********************************************************************************************");
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
}
}

这样就搞定了

那么,我们如何测试呢,可以更改IIS的回收时间

在应用程序池中右键当前应用程序池,正在回收

设置回收间隔  我设置1分钟

这样可以再去看日志

使用FluentScheduler实现定时任务管理的更多相关文章

  1. Quartz 定时任务管理

    前言 将项目中的所有定时任务都统一管理吧,使用 quartz 定时任务 设计思路 使用 quartz 的相关jar 包,懒得去升级了,我使用的是 quart 1.6 写一个定时任务管理类 用一张数据库 ...

  2. 基于PHP的crontab定时任务管理

    BY JENNER · 2014年11月10日· 阅读次数:6 linux的crontab一直是server运维.业务开展的利器.但当定时任务增多时,管理和迁移都变得非常麻烦,并且easy出问题.以下 ...

  3. 定时任务管理中心(dubbo+spring)-我们到底能走多远系列47

    我们到底能走多远系列47 扯淡: 又是一年新年时,不知道上一年你付出了多少,收获了多少呢?也许你正想着老板会发多少奖金,也许你正想着明年去哪家公司投靠. 这个时间点好好整理一下,思考总结一下,的确是个 ...

  4. 基于Spring4的定时任务管理

    在项目中,有时会遇到定时任务的处理,下面介绍一下我的做法. 此做法基于Spring4,Spring框架搭建成功,另需引入quartz.jar,pom.xml文件中加入 <dependency&g ...

  5. 定时任务管理之python篇celery使用

    一.为什么要用celery celery是一个简单.灵活.可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必须工具.他是一个专注于实时处理的任务队列,同时也支持任务调度. celery是异 ...

  6. Linux编译安装、压缩打包、定时任务管理

    编译安装 压缩打包 定时任务管理 一.编译安装 使用源代码,编译打包软件 1.特点 1.可以定制软件 2.按需构建软件 2.编译安装 1.下载源代码包 wget https://nginx.org/d ...

  7. 使用FluentScheduler和IIS预加载在asp.net中实现定时任务管理

    FluentScheduler介绍 github地址:https://github.com/fluentscheduler/FluentScheduler FluentScheduler是一个简单的任 ...

  8. Windows Service 服务搭配FluentScheduler实现定时任务调度

    Windows Service 服务 创建Windows Service 项目 创建一个Windows Service项目,并将项目名称改为 TaskWindowService 在解决方案资源管理器内 ...

  9. Quartz简单实现定时任务管理(SSM+Quartz)

    首先你得有一个用Maven搭好的SSM框架,数据库用的Mysql,这里只有关于Quartz的部分.其实有大神总结的很好了,但做完后总有些地方不一样,所以写这篇作为笔记.这里先把大神的写的分享给大家:h ...

随机推荐

  1. C++中纯虚函数

    1.纯虚函数 virtual ReturnType Function()= 0; 纯虚函数可以让类先具有一个操作名称,而没有操作内容,让派生类在继承时再去具体地给出定义.凡是含有纯虚函数的类叫做抽象类 ...

  2. Raft 一致性算法论文译文

    本篇博客为著名的 RAFT 一致性算法论文的中文翻译,论文名为<In search of an Understandable Consensus Algorithm (Extended Vers ...

  3. 开发中经典sql总结

    1.说明:显示文章.提交人和最后回复时间 select a.title,a.username,b.adddate ,(select max(adddate) from table where tabl ...

  4. Luogu 3959 [NOIP2017] 宝藏

    NOIP2017最后一道题 挺难想的状压dp. 受到深度的条件限制,所以一般的状态设计带有后效性,这时候考虑把深度作为一维,这样子可以保证所有状态不重复计算一遍. 神仙预处理:先处理出一个点连到一个集 ...

  5. MapReduce调优总结与拓展

    本文为<hadoop技术内幕:深入解析MapReduce架构设计与实现原理>一书第9章<Hadoop性能调优>的总结. 图1 Hadoop层次结构图 从管理员角度进行调优 1. ...

  6. UI设计:掌握这6点,轻松0到1

    非科班出身能成为UI设计师吗? 答案是肯定的.世上无难事,只怕有心人.只要找对方法.坚持不懈,即便是零基础也能学好UI设计. 那么零基础学习UI设计,需要学习哪些知识?我们要从哪些地方学起?怎么系统学 ...

  7. jquery 元素筛选 13.6.20

    <ul> <li>list item 1</li> <li>list item 2</li> <li class="thir ...

  8. 2018.09.24 bzoj1016: [JSOI2008]最小生成树计数(并查集+搜索)

    传送门 正解是并查集+矩阵树定理. 但由于数据范围小搜索也可以过. 我们需要知道最小生成树的两个性质: 不同的最小生成树中,每种权值的边出现的个数是确定的 不同的生成树中,某一种权值的边连接完成后,形 ...

  9. 30. Child Labor Problem and Its Solution 童工问题及解决方法

    30. Child Labor Problem and Its Solution 童工问题及解决方法 ① Over a hundred years ago,Charles Dickens shocke ...

  10. 《C++ Primer (V4)》读书笔记

    第2章 变量和基本类型 1.(P56)如果使用class关键字来定义类,那么定义在第一个访问标号前的任何成员都隐式指定为private:如果使用struct关键字,那么这些成员都是public. 第7 ...