aspnet 定时任务--FluentScheduler

   在微信小程序中,需要做一个定时提醒的功能。在这个功能中,用户可以设置一个指定的时间来提醒自己。
在用户设置好后,服务器将用户的OPENID和FORMID以及用户设置的时间等信息保存进数据库。
因此,服务器需要定时的扫描所有用户的提醒信息,如果有用户的提醒到时了,就检查FORMID等条件是否满足,满足的话调用微信的接口,下发提醒消息。

我们选择FluentScheduler来达到定时任务的功能。

第一步:在Global.asax中添加,初始化定时器任务:

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();  // 清除全部数据格式  
            GlobalConfiguration.Configuration.Formatters.Insert(0, new MyJsonConverter());  // 只有定义序列化器及Json格式  

            JobManager.Initialize(new JobScheduler());
            
        }

        protected void Application_End(object sender, EventArgs e)
        {
            JobManager.Stop();//停止定时器
        }

这里需要在vs 中 工具-》nuget程序包管理器-》管理解决方案的NUGET程序包 中,联机查找FluentScheduler,并添加VS对应的版本,添加到解决方案中。


第二步:实现JobScheduler类:


     public class JobScheduler : Registry
    {
        public JobScheduler()
        {
            // 立即执行每10秒一次的计划任务。
            Schedule<DoJob>().ToRunNow().AndEvery(10).Seconds();

            // 延迟一个指定时间后,执行一次计划任务。
            Schedule<DoJob>().ToRunOnceIn(5).Seconds();

            // 在一个指定时间执行计划任务(在每天的9:30分执行)
            Schedule(() => dosomething())
                .ToRunEvery(1).Days().At(9, 30);


            // 立即执行,每月的星期一9:30的计划任务
            Schedule<DoJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(9, 30);

            // 在同一个计划中执行多个任务
            //Schedule<DoJob>().AndThen<DoJob>().ToRunNow().AndEvery(5).Minutes();
        }

        private void dosomething()
        {
            string path = @"api\debug\";
            TLogs.Save(path + @"scheduler.txt", "dosomething ... 开始定时任务了,现在时间是:" + DateTime.Now);
        }

        internal class DoJob : IJob
        {
            void IJob.Execute()
            {
                string path = @"api\debug\";
                TLogs.Save(path + @"scheduler.txt", "开始定时任务了,现在时间是:" + DateTime.Now);
            }
        }

        
    }


我在这里是定时的将结果输出到文件中,这是测试而已。

参考资料:

开源任务管理平台TaskManager介绍
desc:目前系统集成了四个常用任务,代理IP爬虫,快递进度,消息通知,动态修改Job任务。
https://www.cnblogs.com/yanweidie/p/3564062.html


.NET定时任务执行管理器开源组件–FluentScheduler
desc:FluentScheduler 是一个更精密和复杂的调度组件,它采用多线程操作,不会影响主线程,在线程安全方面它也有很好的处理,任务运行时还可以显式控制。
https://blog.csdn.net/gis_101/article/details/52215621

猜你喜欢

转载自blog.csdn.net/zyfzhangyafei/article/details/83615500