Quartz.Net—Calendar

动态的排除一些触发器的时间。

DailyCalendar-天日历

定义:

This implementation of the Calendar excludes (or includes - see below) a specified time range each day.

排除 天  内的一个时间段。或者叫做 在天上做减法操作。

 

private async void button8_Click_1(object sender, EventArgs e)
{
    ISchedulerFactory sf = new StdSchedulerFactory();
    var schdeler = await sf.GetScheduler();
    await schedu.Start();
    IJobDetail job = JobBuilder.Create<MyJob5>().WithDescription("JOB5").StoreDurably(true).Build();

    //某一天的时间段上做减法操作。
    DailyCalendar dailyCa = new DailyCalendar(DateBuilder.DateOf(21, 01, 02).DateTime, DateBuilder.DateOf(21, 01, 20).DateTime);
    await schdeler.AddCalendar("ceshica", dailyCa,true,true);

    //ModifiedByCalendar   减去时间段
    ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").ModifiedByCalendar("ceshica").Build();

    
    await schedu.ScheduleJob(job, trigger);
}
View Code

 

WeeklyCalendar-周日历

定义:

This implementation of the Calendar excludes a set of days of the week. You may use it to exclude weekends for example. But you may define any day of the week. By default it excludes Saturday and Sunday.

该日历的实现不包括一组星期的天数。你可以用它来排除周末。但是你可以定义一周中的任何一天。默认情况下,它不包括周六和周日。

private async void button9_Click(object sender, EventArgs e)
{
    ISchedulerFactory sf = new StdSchedulerFactory();
    var schdeler = await sf.GetScheduler();
    await schedu.Start();
    IJobDetail job = JobBuilder.Create<MyJob5>().WithDescription("JOB5").StoreDurably(true).Build();

    //一周内  某些天数做减法。   默认周六周日是排除的。
    WeeklyCalendar weekCa = new WeeklyCalendar();

    weekCa.SetDayExcluded(DayOfWeek.Friday, true);
    

    await schdeler.AddCalendar("ceshica", weekCa, true, true);

    //ModifiedByCalendar   减去周内的 几天
    ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").ModifiedByCalendar("ceshica").Build();


    await schedu.ScheduleJob(job, trigger);
}
View Code

 

HolidayCalendar-假期日历

定义:

This implementation of the Calendar stores a list of holidays (full days that are excluded from scheduling).

该日历的实现存储了一个假日列表(被排除在调度之外的完整天数)。   

适合用在某一天  假期等,或者当年的某一天不能执行。

private async void button10_Click(object sender, EventArgs e)
{
    ISchedulerFactory sf = new StdSchedulerFactory();
    var schdeler = await sf.GetScheduler();
    await schedu.Start();
    IJobDetail job = JobBuilder.Create<MyJob5>().WithDescription("JOB5").StoreDurably(true).Build();

    //制定某一天不能执行。   这个更加倾向于 holiday  假期。一般工作通过工作日历表中找出所有的假期,然后放入holidaycalendar。
    HolidayCalendar holidayCa = new HolidayCalendar();
    holidayCa.AddExcludedDate(DateTime.Now);//排除今天。
    //holidayCa.RemoveExcludedDate(DateTime.Now);//后期可以动态处理
    await schdeler.AddCalendar("ceshica", holidayCa, true, true);

    //ModifiedByCalendar   减去周内的 几天
    ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").ModifiedByCalendar("ceshica").Build();


    await schedu.ScheduleJob(job, trigger);
}
View Code

 

MonthlyCalendar-月日历

定义:

This implementation of the Calendar excludes a set of days of the month. You may use it to exclude every 1. of each month for example. But you may define any day of a month.

这个日历的实现不包括一个月的天数。你可以用它来排除每个月一号。但是你也可以定义一个月的任何一天。

private async void button11_Click(object sender, EventArgs e)
{
    ISchedulerFactory sf = new StdSchedulerFactory();
    var schdeler = await sf.GetScheduler();
    await schedu.Start();
    IJobDetail job = JobBuilder.Create<MyJob5>().WithDescription("JOB5").StoreDurably(true).Build();


    MonthlyCalendar monthlyCa = new MonthlyCalendar();
    //月份中的某一天不能被执行。
    monthlyCa.SetDayExcluded(17, true);
    
    await schdeler.AddCalendar("ceshica", monthlyCa, true, true);

    //ModifiedByCalendar   减去周内的 几天
    ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").ModifiedByCalendar("ceshica").Build();


    await schedu.ScheduleJob(job, trigger);

}
View Code

 

AnnualCalendar-年日历

定义:

This implementation of the Calendar excludes a set of days of the year. You may use it to exclude bank holidays which are on the same date every year.

该日历的实现不包括一组年份。你可以用它来排除每年同一日期的银行假日。  适合每年的每一天不能执行  比如说国庆节  不干活。

private async void button12_Click(object sender, EventArgs e)
{
    ISchedulerFactory sf = new StdSchedulerFactory();
    var schdeler = await sf.GetScheduler();
    await schedu.Start();
    IJobDetail job = JobBuilder.Create<MyJob5>().WithDescription("JOB5").StoreDurably(true).Build()

    AnnualCalendar annualCa = new AnnualCalendar();
    //每年这几天不执行
    annualCa.SetDayExcluded(new DateTime(2017, 10, 1), true);
    annualCa.SetDayExcluded(new DateTime(2017, 10, 2), true);
    annualCa.SetDayExcluded(new DateTime(2017, 10, 3), true);
    annualCa.SetDayExcluded(new DateTime(2017, 10,4), true);
    annualCa.SetDayExcluded(new DateTime(2017, 10, 5), true);
    annualCa.SetDayExcluded(new DateTime(2017, 10, 6), true);
    annualCa.SetDayExcluded(new DateTime(2017, 10, 7), true);

    annualCa.SetDayExcluded(new DateTime(2017, 3, 18), true);

    await schdeler.AddCalendar("ceshica", annualCa, true, true);

    //ModifiedByCalendar   
    ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").ModifiedByCalendar("ceshica").Build();


    await schedu.ScheduleJob(job, trigger);
}
View Code

 

CronCalendar-Cron日历

定义:

This implementation of the Calendar excludes the set of times expressed by a given CronExpression.

该日历的实现不包括给定的CronExpression所表示的时间集合。  也就是排除cron时间。

private async void button13_Click(object sender, EventArgs e)
{
    ISchedulerFactory sf = new StdSchedulerFactory();
    var schdeler = await sf.GetScheduler();
    await schedu.Start();
    IJobDetail job = JobBuilder.Create<MyJob5>().WithDescription("JOB5").StoreDurably(true).Build();

    CronCalendar cronCa = new CronCalendar("1,5,10,15,20,25,30,35,40,45,50,55  * * * * ?");


    await schdeler.AddCalendar("ceshica", cronCa, true, true);

    //ModifiedByCalendar   
    ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").ModifiedByCalendar("ceshica").Build();


    await schedu.ScheduleJob(job, trigger);
}
View Code

 

posted @ 2018-03-18 03:26  西伯利亚的狼  阅读(903)  评论(0编辑  收藏  举报