当前位置: 移动技术网 > IT编程>开发语言>c# > Quartz.NET常用方法 02

Quartz.NET常用方法 02

2019年12月27日  | 移动技术网IT编程  | 我要评论

 上一篇里介绍了job和trigger的常用方法,这一节将介绍calendar,它的作用是排除特定的日期时间。

 
calendar的常用类
dailycalendar 排除每天某个时间段任务的执行
例子:
        var sche = stdschedulerfactory.getdefaultscheduler();
        sche.start();

        //使任务在10点到11点间不再执行
        dailycalendar cale = new dailycalendar(
           datebuilder.dateof(10, 0, 0).datetime,
           datebuilder.dateof(11, 0, 0).datetime
        );
        sche.addcalendar("mycalendar", cale, true, true); 
 
weeklycalendar 排除每周某个星期的任务的执行
例子:
        var sche = stdschedulerfactory.getdefaultscheduler();
        sche.start();
        
        weeklycalendar cale = new weeklycalendar();
        cale.setdayexcluded(dayofweek.thursday, true);  //让星期四不触发schedule
        //cale.setdayexcluded(dayofweek.thursday, false); //让星期四触发schedule
        sche.addcalendar("mycalendar", cale, true, true);
 
holidaycalendar 排除某一天的任务的执行(如果涉及到同一天跨年的情况,需要多次添加不同年份)
例子:
        var sche = stdschedulerfactory.getdefaultscheduler();
        sche.start();

        holidaycalendar cale = new holidaycalendar();
        cale.addexcludeddate(datetime.now.addyears(-1)); //排除去年的今天不处理
        cale.addexcludeddate(datetime.now); //排除今天不处理
        sche.addcalendar("mycalendar", cale, true, true);
 
monthlycalendar 排除每月某一天的任务的执行
例子:
        var sche = stdschedulerfactory.getdefaultscheduler();
        sche.start();

        monthlycalendar cale = new monthlycalendar();
        cale.setdayexcluded(25, true);  //排除每个月25号执行
        sche.addcalendar("mycalendar", cale, true, true);
annualcalendar 排除每年某一天的任务的执行
例子:
        var sche = stdschedulerfactory.getdefaultscheduler();
        sche.start();
        
        annualcalendar cale = new annualcalendar();
        //12月25号不执行
        cale.setdayexcluded(new datetimeoffset(2018, 12, 25, 12, 0, 0, timespan.fromhours(8)), true);  
        sche.addcalendar("mycalendar", cale, true, true);
 
croncalendar 通过cron表达式排除任务的执行
例子:
        var sche = stdschedulerfactory.getdefaultscheduler();
        sche.start();

        //只在营业时间执行8am-5pm
        croncalendar cale = new croncalendar("* * 0-7,18-23 ? * *");

        sche.addcalendar("mycalendar", cale, true, true);
 
 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网