Quartz.Net multiple calendars

痞子三分冷 提交于 2019-12-11 08:06:33

问题


Using Quartz.Net and have a need for the same trigger to have multiple Calendars, whilst this isn't possible I'm looking for suggestions of how to achieve similar functionality.

For example I want to run the job in X minutes and exlude 9-10am every day but be able to block out other times during the day as required.

The code below works fine but if I want to block out another time interval I can't see a way of doing it.

ISchedulerFactory schedFact = new StdSchedulerFactory();
sched = schedFact.GetScheduler();

CronCalendar cronCal = new CronCalendar("* * 9 ? * * *");
sched.AddCalendar("testCal", cronCal, true, true);

CronTrigger trigger = new CronTrigger("cronTrigger", null, "0 0/1 * 1/1 * ? *");               
trigger.StartTimeUtc = DateTime.UtcNow.AddMinutes(10);
trigger.CalendarName = "testCal";

JobDetail job = new JobDetail("testJob", null, typeof(DumbJob));

sched.ScheduleJob(job, trigger);
sched.start();

Simple test job:

public class DumbJob : IJob
        {
            public DumbJob()
            {
            }

            public void Execute(JobExecutionContext context)
            {
                MessageBox.Show("Dumb job is running");
            }
        }

回答1:


You can create a chain of calendars. Every calendar can have a base calendar which is also checked when determining whether given time is excluded or included. See CronCalendar's contructor:

public CronCalendar(ICalendar baseCalendar, string expression)



回答2:


I managed to find the solution to implement multi calendar and find the link : Quartz.Net multple calendars Marko Lahma give the solution to create calendar chain with BaseCaleandar.

I tested and find there is some bug in calendar chain.

I just change some code in Quartz.Examples.2010.Example8.

Add a WeeklyCalendar to the AnnualCalendar:

WeeklyCalendar weeklyCalendar = new WeeklyCalendar();

weeklyCalendar.SetDayExcluded(DayOfWeek.Sunday, true);
weeklyCalendar.SetDayExcluded(DayOfWeek.Saturday, true);

// Add the holiday calendar to the schedule
AnnualCalendar holidays = new AnnualCalendar(weeklyCalendar);

Add two holidays into AnuualCalendar for next two days:

 DateTime day1= new DateTime(DateTime.UtcNow.Year, 1, 22);
 holidays.SetDayExcluded(day1, true);
 DateTime day2= new DateTime(DateTime.UtcNow.Year, 1, 23);
 holidays.SetDayExcluded(day2, true);

Attach the AnnualCalendar to a SimpleTrigger with IntervalInHourse for 72h/96h/120h and fire in 1/21.

  1. the right result is 1/25 for 72h but return 1/24.
  2. the right result is 1/26 for 96h but return 1/24.
  3. the right result is 1/31 for 120, yes it does return 1/31.


来源:https://stackoverflow.com/questions/5863435/quartz-net-multiple-calendars

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!