C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器

必须JobManager初始化

方式1:

public void Start()
        {
            JobManager.AddJob(() => FetchingData(), schedule =>
            {
                schedule.ToRunEvery(1).Days().At(10, 8);
            });
        }

 

方式2:继承Registry

public class WebSchedule : Registry
    {
        ILogger<WebSchedule> log;       
        /// <summary>
        ///
        /// </summary>
        public WebSchedule(ILoggerFactory loggerFactory)
        {
            log = loggerFactory.CreateLogger<WebSchedule>();

            //立即执行,之后每2秒执行
            Schedule(() => log.LogTrace("每分钟执行:" + DateTime.Now)).ToRunNow().AndEvery(1).Minutes();

            Schedule(() => log.LogTrace("每天执行:" + DateTime.Now)).ToRunNow().AndEvery(1).Days();

            //5秒后执行一次
            Schedule(() => log.LogTrace("执行一次:" + DateTime.Now)).ToRunOnceIn(5).Seconds();

            Schedule(() => log.LogTrace("每天21:15执行:" + DateTime.Now))
                .ToRunEvery(1).Days().At(13, 55);

            //立即执行,之后每月的第一个星期一3点再执行
            Schedule(() =>
            {
                log.LogTrace("job started at " + DateTime.Now);
                Thread.Sleep(10000);
                log.LogTrace("job ended at" + DateTime.Now);
            }).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);

            //多任务
            //Schedule<MyJob>().AndThen<MyOtherJob>().ToRunNow();
        }
    }

调用:JobManager.Initialize(new WebSchedule(loggerFactory));

posted @ 2016-12-20 10:22  94cool  阅读(2233)  评论(0编辑  收藏  举报