Quartz.Net multiple calendars

2019-08-03 05:49发布

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");
            }
        }

标签: c# quartz.net
2条回答
混吃等死
2楼-- · 2019-08-03 06:26

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.
查看更多
该账号已被封号
3楼-- · 2019-08-03 06:27

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)
查看更多
登录 后发表回答