ASP.NET MVC中使用FluentScheduler任務定時器

46 篇文章 1 订阅

github作者示例地址 https://github.com/fluentscheduler/FluentScheduler

發佈web程序在IIS中,最好推薦將IIS改下配置,設置如下:

应用程序池-高级设置-启动模式:AlwaysRunning
应用程序-高级设置-常规-预加载已启用:True

//示例代碼:

using FluentScheduler;
namespace WebAppTimer.Models
{
    public class MyRegistry : Registry
    {
        public MyRegistry()
        {            
            //測試任務一,
            int jiange = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["jiange_seconds"]);
            Schedule<MyJob>().ToRunEvery(jiange).Seconds();

            //測試任務二,分鐘任務
            int coinMinutes = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["coinMinutes"]);
            Schedule<CoinValueJob>().ToRunEvery(coinMinutes).Minutes();
        }
    }
}


public class MyJob : IJob
    {
        private readonly object _lockMin = new object();
        public void Execute()
        {
            //加鎖是為了保證在多線程環境下的準確
            lock (_lockMin)
            {
                LogHelpter.AddLog("任務執行" + DateTime.Now.Ticks);
            }       
        }
    }

  public class CoinValueJob : IJob
    {
        private readonly object _lock = new object();
        public void Execute()
        {
            //加鎖是為了保證在多線程環境下的準確
            lock (_lock)
            {                
                LogHelpter.AddLog("幣價值平衡,任務執行");
            }
        }
    }   

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            //初始化定時器任務
            FluentScheduler.JobManager.Initialize(new MyRegistry());
        }

        protected void Application_End()
        {
            LogHelpter.AddLog("进程即将被IIS回收");
            LogHelpter.AddLog("重新访问一个页面,以唤醒服务");
            //地址最好是全路徑,帶有http://開頭的,參考:http://localhost:8087/
            string strUrl = System.Configuration.ConfigurationManager.AppSettings["SelfAddress"];//本程序部署地址  
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                System.IO.Stream stream = wc.OpenRead(strUrl);
                System.IO.StreamReader reader= new System.IO.StreamReader(stream);
                string html = reader.ReadToEnd();
                //LogHelpter.AddLog("訪問的頁面" + html);
                if (!string.IsNullOrWhiteSpace(html))
                {
                    LogHelpter.AddLog("喚醒程序成功");
                }
                reader.Close();
                reader.Dispose();
                stream.Close();
                stream.Dispose();
                wc.Dispose();
            }
            catch (Exception ex)
            {
                LogHelpter.AddLog("喚醒異常" + ex.Message);
            }           
        }

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值