• Quartz.Net系列(十二):六大Calendar(Annual、Cron、Daily、Holiday、Monthly、Weekly)


    Quartz.Net中为了动态排除一些时间,而使用Calendar可以做到

    1.DailyCalendar

    可以动态的排除一天中的某些时间段

    示例:在一天当中的13:00到14:00不要执行

            public async static Task Test()
            {
                var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
    
                await scheduler.Start();
    
                DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
                                                                DateBuilder.DateOf(14,0,0).DateTime);
    
                var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();
    
                var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                                   d => d.OnEveryDay()
                                                         .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                         .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                         .WithIntervalInSeconds(1)
                                                  )
                                                   .ModifiedByCalendar("myCalendar")
                                                   .Build();
    
                await scheduler.AddCalendar("myCalendar", dailyCalendar, true,true);
    
                await scheduler.ScheduleJob(job, trigger);
            }
        public class MyJob : IJob
        {
            public async Task Execute(IJobExecutionContext context)
            {
                await Task.Factory.StartNew(() =>
                {
                    var nextTime = context.NextFireTimeUtc?.ToLocalTime();
    
                    var currentTime = DateTime.Now;
    
                    var jobKey = context.JobDetail.Key;
    
                    Console.WriteLine($"JobKey={jobKey},  CurrentTime={currentTime},  FireTime={context.ScheduledFireTimeUtc?.ToLocalTime()},  NextTime={nextTime}");
                });
            }
        }

     2.WeeklyCalendar

    可以动态的排除一周内的哪几天不执行

    示例:设置每周的周六和周日不能被执行

            public async static Task Test()
            {
                var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
    
                await scheduler.Start();
    
                //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
                //                                                DateBuilder.DateOf(14,0,0).DateTime);
    
                WeeklyCalendar calendar = new WeeklyCalendar();
    
                calendar.SetDayExcluded(DayOfWeek.Sunday,true);
                calendar.SetDayExcluded(DayOfWeek.Saturday, true);
    
                var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();
    
                var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                                   d => d.OnEveryDay()
                                                         .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                         .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                         .WithIntervalInSeconds(1)
                                                  )
                                                   .ModifiedByCalendar("myCalendar")
                                                   .Build();
    
                await scheduler.AddCalendar("myCalendar", calendar, true,true);
    
                await scheduler.ScheduleJob(job, trigger);
            }

    3.HolidayCalendar

    动态的排除哪一天不执行

    示例:设置今天不能被执行

      public async static Task Test()
            {
                var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
    
                await scheduler.Start();
    
                //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
                //                                                DateBuilder.DateOf(14,0,0).DateTime);
    
                //WeeklyCalendar calendar = new WeeklyCalendar();
    
                //calendar.SetDayExcluded(DayOfWeek.Sunday,true);
                //calendar.SetDayExcluded(DayOfWeek.Saturday, true);
    
                HolidayCalendar calendar = new HolidayCalendar();
    
                calendar.AddExcludedDate(DateTime.Today);
    
                var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();
    
                var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                                   d => d.OnEveryDay()
                                                         .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                         .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                         .WithIntervalInSeconds(1)
                                                  )
                                                   //.ModifiedByCalendar("myCalendar")
                                                   .Build();
    
                await scheduler.AddCalendar("myCalendar", calendar, true,true);
    
                await scheduler.ScheduleJob(job, trigger);
            }

    4.MonthlyCalendar

    排除每个月中的某一天不能被执行

    示例:设置每月的第12天不能被执行

       public async static Task Test()
            {
                var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
    
                await scheduler.Start();
    
                //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
                //                                                DateBuilder.DateOf(14,0,0).DateTime);
    
                //WeeklyCalendar calendar = new WeeklyCalendar();
    
                //calendar.SetDayExcluded(DayOfWeek.Sunday,true);
                //calendar.SetDayExcluded(DayOfWeek.Saturday, true);
    
                //HolidayCalendar calendar = new HolidayCalendar();
    
                //calendar.AddExcludedDate(DateTime.Today);
    
    
                //calendar.RemoveExcludedDate(DateTime.Now); 
    
                MonthlyCalendar calendar = new MonthlyCalendar();
    
                calendar.SetDayExcluded(12, true);
    
    
                var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();
    
                var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                                   d => d.OnEveryDay()
                                                         .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                         .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                         .WithIntervalInSeconds(1)
                                                  )
                                                   .ModifiedByCalendar("myCalendar")
                                                   .Build();
    
                await scheduler.AddCalendar("myCalendar", calendar, true,true);
    
                await scheduler.ScheduleJob(job, trigger);
            }

    5.AnnualCalendar

    排除每一年中的某一天不被执行

    示例:设置每年的7月12日不能被执行

            public async static Task Test()
            {
                var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
    
                await scheduler.Start();
    
                //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
                //                                                DateBuilder.DateOf(14,0,0).DateTime);
    
                //WeeklyCalendar calendar = new WeeklyCalendar();
    
                //calendar.SetDayExcluded(DayOfWeek.Sunday,true);
                //calendar.SetDayExcluded(DayOfWeek.Saturday, true);
    
                //HolidayCalendar calendar = new HolidayCalendar();
    
                //calendar.AddExcludedDate(DateTime.Today);
    
    
                //calendar.RemoveExcludedDate(DateTime.Now); 
    
                //MonthlyCalendar calendar = new MonthlyCalendar();
    
                //calendar.SetDayExcluded(12, true);
    
    
                AnnualCalendar calendar = new AnnualCalendar();
    
                calendar.SetDayExcluded(new DateTime(2020,7,12),true);
    
                var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();
    
                var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                                   d => d.OnEveryDay()
                                                         .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                         .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                         .WithIntervalInSeconds(1)
                                                  )
                                                   .ModifiedByCalendar("myCalendar")
                                                   .Build();
    
                await scheduler.AddCalendar("myCalendar", calendar, true,true);
    
                await scheduler.ScheduleJob(job, trigger);
            }
      public virtual void SetDayExcluded(DateTime day, bool exclude)
            {
                DateTime d = new DateTime(FixedYear, day.Month, day.Day, 0, 0, 0);
    
                if (exclude)
                {
                    if (!IsDateTimeExcluded(day, false))
                    {
                        excludeDays.Add(d);
                    }
                }
                else
                {
                    // include
                    if (IsDateTimeExcluded(day, false))
                    {
                        excludeDays.Remove(d);
                    }
                }
            }

    6.CronCalendar

    以 Cron表达式的方式排除哪些时间不能被执行

    示例:设置每月的第12天不能被执行

    public async static Task Test()
            {
                var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
    
                await scheduler.Start();
    
                //DailyCalendar dailyCalendar = new DailyCalendar(DateBuilder.DateOf(13,0,0).DateTime,
                //                                                DateBuilder.DateOf(14,0,0).DateTime);
    
                //WeeklyCalendar calendar = new WeeklyCalendar();
    
                //calendar.SetDayExcluded(DayOfWeek.Sunday,true);
                //calendar.SetDayExcluded(DayOfWeek.Saturday, true);
    
                //HolidayCalendar calendar = new HolidayCalendar();
    
                //calendar.AddExcludedDate(DateTime.Today);
    
    
                //calendar.RemoveExcludedDate(DateTime.Now); 
    
                //MonthlyCalendar calendar = new MonthlyCalendar();
    
                //calendar.SetDayExcluded(12, true);
    
    
                //AnnualCalendar calendar = new AnnualCalendar();
    
                //calendar.SetDayExcluded(new DateTime(2020,7,12),true);
    
                CronCalendar calendar = new CronCalendar("* * * 12 * ?");
    
    
    
                var job = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();
    
                var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                                                   d => d.OnEveryDay()
                                                         .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                                                         .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 00))
                                                         .WithIntervalInSeconds(1)
                                                  )
                                                   .ModifiedByCalendar("myCalendar")
                                                   .Build();
    
                await scheduler.AddCalendar("myCalendar", calendar, true,true);
    
                await scheduler.ScheduleJob(job, trigger);
            }
  • 相关阅读:
    Promise是如何实现异步编程的?
    js 检测元素是否被覆盖
    antd upload组件结合七牛云上传图片
    webpack原理分析之编写一个打包器
    docker命令构建Java程序镜像,并运行它
    新建mysql docker指定版本
    spring官方文档网址
    rabbitmq用x-delayed-message的exchange特性支持消息延迟消费
    解决Can't open /usr/lib/grub/update-grub_lib
    java8-强大的Stream API
  • 原文地址:https://www.cnblogs.com/vic-tory/p/13288321.html
Copyright © 2020-2023  润新知