Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解
1.介绍
中文意义就是每日时间间隔计划生成
2.API讲解
(1)WithInterval、WithIntervalInHours、WithIntervalInMinutes、WithIntervalInSeconds
WithInterval:指定要生成触发器的时间单位和间隔。
WithIntervalInHours:指定要生成触发器的间隔按小时来
WithIntervalInMinutes:指定要生成触发器的间隔按分钟来
WithIntervalInSeconds:指定要生成触发器的间隔按秒来
和前面的SimpleSceduleBuilder、CalendarIntervalScheduleBuilder一样的
(2)OnDaysOfTheWeek、OnMondayThroughFriday、OnSaturdayAndSunday、OnEveryDay
OnDaysOfTheWeek:设置触发器一周中的哪几天
OnMondayThroughFriday:从星期一到星期五
OnSaturdayAndSunday:周六和周日
OnEveryDay:每天
每天10:00到23:10.00的每一分钟执行一次
var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
w => w.OnEveryDay() //设置每天
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(, )) //设置每天开始于几点
.EndingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(, , )) //设置每日结束于几点
.WithIntervalInMinutes()//间隔分钟
).Build();
一周当中的星期二和星期三每秒执行一次
List<DayOfWeek> dayOfWeeks = new List<DayOfWeek>(); dayOfWeeks.Add(DayOfWeek.Wednesday); dayOfWeeks.Add(DayOfWeek.Tuesday); trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
w=>w.OnDaysOfTheWeek(dayOfWeeks)//.OnDaysOfTheWeek(new DayOfWeek[2] { DayOfWeek.Wednesday,DayOfWeek.Tuesday})
.WithIntervalInSeconds()
).Build();
源码实现
/// <summary>
/// Set the trigger to fire on the given days of the week.
/// </summary>
/// <param name="onDaysOfWeek">a Set containing the integers representing the days of the week, defined by <see cref="DayOfWeek.Sunday"/> - <see cref="DayOfWeek.Saturday"/>.
/// </param>
/// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
public DailyTimeIntervalScheduleBuilder OnDaysOfTheWeek(IReadOnlyCollection<DayOfWeek> onDaysOfWeek)
{
if (onDaysOfWeek == null || onDaysOfWeek.Count == )
{
throw new ArgumentException("Days of week must be an non-empty set.");
} foreach (DayOfWeek day in onDaysOfWeek)
{
if (!AllDaysOfTheWeek.Contains(day))
{
throw new ArgumentException("Invalid value for day of week: " + day);
}
} daysOfWeek = new HashSet<DayOfWeek>(onDaysOfWeek);
return this;
} /// <summary>
/// Set the trigger to fire on the given days of the week.
/// </summary>
/// <param name="onDaysOfWeek">a variable length list of week days representing the days of the week</param>
/// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
public DailyTimeIntervalScheduleBuilder OnDaysOfTheWeek(params DayOfWeek[] onDaysOfWeek)
{
return OnDaysOfTheWeek((IReadOnlyCollection<DayOfWeek>) onDaysOfWeek);
} /// <summary>
/// Set the trigger to fire on the days from Monday through Friday.
/// </summary>
/// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
public DailyTimeIntervalScheduleBuilder OnMondayThroughFriday()
{
daysOfWeek = new HashSet<DayOfWeek>(MondayThroughFriday);
return this;
} /// <summary>
/// Set the trigger to fire on the days Saturday and Sunday.
/// </summary>
/// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
public DailyTimeIntervalScheduleBuilder OnSaturdayAndSunday()
{
daysOfWeek = new HashSet<DayOfWeek>(SaturdayAndSunday);
return this;
} /// <summary>
/// Set the trigger to fire on all days of the week.
/// </summary>
/// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
public DailyTimeIntervalScheduleBuilder OnEveryDay()
{
daysOfWeek = new HashSet<DayOfWeek>(AllDaysOfTheWeek);
return this;
}
(3)StartingDailyAt、EndingDailyAt
StartingDailyAt:开始时间于
EndingDailyAt:结束时间于
源码实现
/// <summary>
/// The TimeOfDay for this trigger to start firing each day.
/// </summary>
/// <param name="timeOfDayUtc"></param>
/// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
public DailyTimeIntervalScheduleBuilder StartingDailyAt(TimeOfDay timeOfDayUtc)
{
startTimeOfDayUtc = timeOfDayUtc ?? throw new ArgumentException("Start time of day cannot be null!");
return this;
} /// <summary>
/// The TimeOfDay for this trigger to end firing each day.
/// </summary>
/// <param name="timeOfDayUtc"></param>
/// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
public DailyTimeIntervalScheduleBuilder EndingDailyAt(TimeOfDay timeOfDayUtc)
{
endTimeOfDayUtc = timeOfDayUtc;
return this;
}
(4)EndingDailyAfterCount
EndingDailyAfterCount:使用count、interval和StarTimeOfDay计算并设置EndTimeOfDay。
源码实现
/// <summary>
/// Calculate and set the EndTimeOfDay using count, interval and StarTimeOfDay. This means
/// that these must be set before this method is call.
/// </summary>
/// <param name="count"></param>
/// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
public DailyTimeIntervalScheduleBuilder EndingDailyAfterCount(int count)
{
if (count <= )
{
throw new ArgumentException("Ending daily after count must be a positive number!");
} if (startTimeOfDayUtc == null)
{
throw new ArgumentException("You must set the StartDailyAt() before calling this EndingDailyAfterCount()!");
} DateTimeOffset today = SystemTime.UtcNow();
DateTimeOffset startTimeOfDayDate = startTimeOfDayUtc.GetTimeOfDayForDate(today).Value;
DateTimeOffset maxEndTimeOfDayDate = TimeOfDay.HourMinuteAndSecondOfDay(, , ).GetTimeOfDayForDate(today).Value; //apply proper offsets according to timezone
TimeZoneInfo targetTimeZone = timeZone ?? TimeZoneInfo.Local;
startTimeOfDayDate = new DateTimeOffset(startTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(startTimeOfDayDate.DateTime, targetTimeZone));
maxEndTimeOfDayDate = new DateTimeOffset(maxEndTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(maxEndTimeOfDayDate.DateTime, targetTimeZone)); TimeSpan remainingMillisInDay = maxEndTimeOfDayDate - startTimeOfDayDate;
TimeSpan intervalInMillis;
if (intervalUnit == IntervalUnit.Second)
{
intervalInMillis = TimeSpan.FromSeconds(interval);
}
else if (intervalUnit == IntervalUnit.Minute)
{
intervalInMillis = TimeSpan.FromMinutes(interval);
}
else if (intervalUnit == IntervalUnit.Hour)
{
intervalInMillis = TimeSpan.FromHours(interval);
}
else
{
throw new ArgumentException("The IntervalUnit: " + intervalUnit + " is invalid for this trigger.");
} if (remainingMillisInDay < intervalInMillis)
{
throw new ArgumentException("The startTimeOfDay is too late with given Interval and IntervalUnit values.");
} long maxNumOfCount = remainingMillisInDay.Ticks / intervalInMillis.Ticks;
if (count > maxNumOfCount)
{
throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount);
} TimeSpan incrementInMillis = TimeSpan.FromTicks((count - ) * intervalInMillis.Ticks);
DateTimeOffset endTimeOfDayDate = startTimeOfDayDate.Add(incrementInMillis); if (endTimeOfDayDate > maxEndTimeOfDayDate)
{
throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount);
} DateTime cal = SystemTime.UtcNow().Date;
cal = cal.Add(endTimeOfDayDate.TimeOfDay);
endTimeOfDayUtc = TimeOfDay.HourMinuteAndSecondOfDay(cal.Hour, cal.Minute, cal.Second);
return this;
}
Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解的更多相关文章
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
- 深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)
上篇文章<深入浅出Mybatis系列(三)---配置详解之properties与environments(mybatis源码篇)> 介绍了properties与environments, ...
- Android Studio系列教程五--Gradle命令详解与导入第三方包
Android Studio系列教程五--Gradle命令详解与导入第三方包 2015 年 01 月 05 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://s ...
- Android 颜色渲染(九) PorterDuff及Xfermode详解
版权声明:本文为博主原创文章,未经博主允许不得转载. Android 颜色渲染(九) PorterDuff及Xfermode详解 之前已经讲过了除ComposeShader之外Shader的全部子类 ...
- 构建安全的Xml Web Service系列之wse之错误代码详解
原文:构建安全的Xml Web Service系列之wse之错误代码详解 WSE3.0现在还没有中文版的可以下载,使用英文版的过程中,难免会遇到各种各样的错误,而面对一堆毫无头绪的错误异常,常常会感到 ...
- [js高手之路] es6系列教程 - 对象功能扩展详解
第一:字面量对象的方法,支持缩写形式 //es6之前,这么写 var User = { name : 'ghostwu', showName : function(){ return this.nam ...
- “全栈2019”Java多线程第十九章:死锁详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java异常第十九章:RuntimeException详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- “全栈2019”Java第二十九章:数组详解(中篇)
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
随机推荐
- 乌云jsonp案例
新浪微博之点击我的链接就登录你的微博(JSONP劫持) 生活处处有惊喜啊!逛逛wooyun都能捡到bug. 测试的时候没关burp,逛乌云的时候抓到一条url: http://login.sina.c ...
- 小谢第2问:后端返回为数组list时候,怎么实现转为tree
要求后端返回给我的list时候,在数组中定义有id , parentid, 可以用双重循环的方法,得到tree需要的数据结构,这样得到的数据就可以直接复制给树组件的data啦const oldData ...
- leetcode976之三角形最大周长
题目描述: 给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的.面积不为零的三角形的最大周长. 如果不能形成任何面积不为零的三角形,返回 0. def largePara(A): A. ...
- 团队作业第五次——Alpha冲刺
这个作业属于哪个课程 软件工程 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 Alpha冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.代码规范与计划 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(二)
系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...
- Shell脚本 (三) 条件判断 与 流程控制
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 六.条件判断 1.基本语法 [ condition ](注意condition 前后要有空格) 注意:条 ...
- Java实现 LeetCode 794 有效的井字游戏 (暴力分析)
794. 有效的井字游戏 用字符串数组作为井字游戏的游戏板 board.当且仅当在井字游戏过程中,玩家有可能将字符放置成游戏板所显示的状态时,才返回 true. 该游戏板是一个 3 x 3 数组,由字 ...
- Java实现 LeetCode 151 翻转字符串里的单词
151. 翻转字符串里的单词 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky th ...
- Java实现 蓝桥杯 算法提高 数组求和
试题 算法提高 数组求和 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 输入n个数,围成一圈,求连续m(m<n)个数的和最大为多少? 输入格式 输入的第一行包含两个整数n, ...
- Java实现 蓝桥杯 算法提高 扶老奶奶过街
1 问题描述 一共有5个红领巾,编号分别为A.B.C.D.E,老奶奶被他们其中一个扶过了马路. 五个红领巾各自说话: A :我和E都没有扶老奶奶 B :老奶奶是被C和E其中一个扶过大街的 C :老奶奶 ...