Quartz.Net系列(六):Quartz五大构件Trigger之TriggerBuilder解析
所有方法图:

1.Create、Build
Create:创建一个TriggerBuilder
Build:生成Trigger
var trigger = TriggerBuilder.Create().Build();
底层实现
/// <summary>
/// Create a new TriggerBuilder with which to define a
/// specification for a Trigger.
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>the new TriggerBuilder</returns>
public static TriggerBuilder Create()
{
return new TriggerBuilder();
} /// <summary>
/// Produce the <see cref="ITrigger" />.
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>a Trigger that meets the specifications of the builder.</returns>
public ITrigger Build()
{
if (scheduleBuilder == null)
{
scheduleBuilder = SimpleScheduleBuilder.Create();
}
IMutableTrigger trig = scheduleBuilder.Build(); trig.CalendarName = calendarName;
trig.Description = description;
trig.StartTimeUtc = startTime;
trig.EndTimeUtc = endTime;
if (key == null)
{
key = new TriggerKey(Guid.NewGuid().ToString(), null);
}
trig.Key = key;
if (jobKey != null)
{
trig.JobKey = jobKey;
}
trig.Priority = priority; if (!jobDataMap.IsEmpty)
{
trig.JobDataMap = jobDataMap;
} return trig;
}
2.StartAt、EndAt、StartNow
StartAt:设置触发器应该开始的时间
EndAt:设置触发器结束时间
StartNow:将触发器的启动时间设置为当前时刻
var trigger = TriggerBuilder.Create()
.StartNow()
//.StartAt(DateTimeOffset.Now)
.EndAt(DateTimeOffset.Now.AddSeconds())
.WithSimpleSchedule(x=>x.RepeatForever().WithIntervalInSeconds())
.Build();
注:DateTime和DateTimeOffset的区别
DateTimeOffset是跨时区的,也就是按照UTC时间来执行
底层实现:
/// <summary>
/// Set the time the Trigger should start at - the trigger may or may
/// not fire at this time - depending upon the schedule configured for
/// the Trigger. However the Trigger will NOT fire before this time,
/// regardless of the Trigger's schedule.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="startTimeUtc">the start time for the Trigger.</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="ITrigger.StartTimeUtc" />
/// <seealso cref="DateBuilder" />
public TriggerBuilder StartAt(DateTimeOffset startTimeUtc)
{
startTime = startTimeUtc;
return this;
} /// <summary>
/// Set the time the Trigger should start at to the current moment -
/// the trigger may or may not fire at this time - depending upon the
/// schedule configured for the Trigger.
/// </summary>
/// <remarks>
/// </remarks>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="ITrigger.StartTimeUtc" />
public TriggerBuilder StartNow()
{
startTime = SystemTime.UtcNow();
return this;
} /// <summary>
/// Set the time at which the Trigger will no longer fire - even if it's
/// schedule has remaining repeats.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="endTimeUtc">the end time for the Trigger. If null, the end time is indefinite.</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="ITrigger.EndTimeUtc" />
/// <seealso cref="DateBuilder" />
public TriggerBuilder EndAt(DateTimeOffset? endTimeUtc)
{
endTime = endTimeUtc;
return this;
}

3.ForJob
ForJob:指定对应关系的JobKey,四个重载方法,底层都是指定一个JobKey
var job = JobBuilder.CreateForAsync<FirstJob>()
//.StoreDurably()
.WithIdentity("myJob", "jobGroup")
.SetJobData(new JobDataMap(dict))
.UsingJobData("Password","")
.Build(); //trigger WithIntervalInSeconds(1)间隔1m RepeatForever重复
//var trigger = TriggerBuilder.Create().WithSimpleSchedule(x =>
// x.WithIntervalInSeconds(1)
// //.RepeatForever()
// ).Build();. var trigger = TriggerBuilder.Create()
.StartNow()
.ForJob("myJob","jobGroup")
//.StartAt(DateTimeOffset.Now)
.EndAt(DateTimeOffset.Now.AddSeconds())
.WithSimpleSchedule(x=>x.RepeatForever().WithIntervalInSeconds())
.Build(); //scheduler.ListenerManager.AddJobListener(new CustomJobListener(),GroupMatcher<JobKey>.AnyGroup()); //scheduler.ListenerManager.AddTriggerListener(); await scheduler.ScheduleJob(job, trigger); //await Monitor(); var jobGroups =await scheduler.GetJobGroupNames();
Console.WriteLine(jobGroups.FirstOrDefault());

底层实现
/// <summary>
/// Set the identity of the Job which should be fired by the produced
/// Trigger.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="jobKey">the identity of the Job to fire.</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="ITrigger.JobKey" />
public TriggerBuilder ForJob(JobKey jobKey)
{
this.jobKey = jobKey;
return this;
} /// <summary>
/// Set the identity of the Job which should be fired by the produced
/// Trigger - a <see cref="JobKey" /> will be produced with the given
/// name and default group.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="jobName">the name of the job (in default group) to fire.</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="ITrigger.JobKey" />
public TriggerBuilder ForJob(string jobName)
{
jobKey = new JobKey(jobName, null);
return this;
} /// <summary>
/// Set the identity of the Job which should be fired by the produced
/// Trigger - a <see cref="JobKey" /> will be produced with the given
/// name and group.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="jobName">the name of the job to fire.</param>
/// <param name="jobGroup">the group of the job to fire.</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="ITrigger.JobKey" />
public TriggerBuilder ForJob(string jobName, string jobGroup)
{
jobKey = new JobKey(jobName, jobGroup);
return this;
} /// <summary>
/// Set the identity of the Job which should be fired by the produced
/// Trigger, by extracting the JobKey from the given job.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="jobDetail">the Job to fire.</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="ITrigger.JobKey" />
public TriggerBuilder ForJob(IJobDetail jobDetail)
{
JobKey k = jobDetail.Key;
if (k.Name == null)
{
throw new ArgumentException("The given job has not yet had a name assigned to it.");
}
jobKey = k;
return this;
}
4.UsingJobData
UsingJobData:添加Job数据,底层是key-vaule的JobDataMap,和JobBuilder里面的一样
await Task.Run(() =>
{
//var userName=context.MergedJobDataMap.GetString("UserName");
//var password = context.MergedJobDataMap.GetString("Password");
//Console.WriteLine(userName);
//Console.WriteLine(password);
var name = context.Trigger.JobDataMap.GetString("name"); Console.WriteLine(name);
//Console.WriteLine("Hello World !");
});

5.WithPriority
WithPriority:设置触发器的优先级。当多个触发器具有相同的启动时间,调度程序将启动优先级最高的,默认5,数字大的优先级高
var trigger1 = TriggerBuilder.Create()
//.StartNow()
//.ForJob("myJob", "jobGroup")
.StartAt(DateBuilder.EvenSecondDateAfterNow())
.UsingJobData("name", "trigger1")
.WithPriority()
.EndAt(DateTimeOffset.Now.AddMinutes())
.WithSimpleSchedule(x => x.RepeatForever().WithIntervalInSeconds())
.Build(); var trigger2 = TriggerBuilder.Create()
//.StartNow()
//.ForJob("myJob", "jobGroup")
.StartAt(DateBuilder.EvenSecondDateAfterNow())
.UsingJobData("name", "trigger2")
.WithPriority()
.EndAt(DateTimeOffset.Now.AddMinutes())
.WithSimpleSchedule(x => x.RepeatForever().WithIntervalInSeconds())
.Build(); HashSet<ITrigger> triggers = new HashSet<ITrigger>(); triggers.Add(trigger1); triggers.Add(trigger2); await scheduler.ScheduleJob(job, triggers,true);
底层实现
public TriggerBuilder WithPriority(int priority)
{
this.priority = priority;
return this;
}
private int priority = TriggerConstants.DefaultPriority;
public static class TriggerConstants
{
/// <summary>
/// The default value for priority.
/// </summary>
public const int DefaultPriority = ;
}

6.WithSchedule
所有ScheduleBuilder的生成接口,默认有:SimpleScheduleBuilder、CalendarIntervalScheduleBuilder、CronScheduleBuilder、DailyTimeIntervalScheduleBuilder
表示可以扩展自己的实现类
/// <summary>
/// Set the <see cref="IScheduleBuilder" /> that will be used to define the
/// Trigger's schedule.
/// </summary>
/// <remarks>
/// <para>The particular <see cref="IScheduleBuilder" /> used will dictate
/// the concrete type of Trigger that is produced by the TriggerBuilder.</para>
/// </remarks>
/// <param name="scheduleBuilder">the SchedulerBuilder to use.</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="IScheduleBuilder" />
/// <seealso cref="SimpleScheduleBuilder" />
/// <seealso cref="CronScheduleBuilder" />
/// <seealso cref="CalendarIntervalScheduleBuilder" />
public TriggerBuilder WithSchedule(IScheduleBuilder scheduleBuilder)
{
this.scheduleBuilder = scheduleBuilder;
return this;
}
7.WithIdentity
设置TriggerKey,底层就是实现TriggerKey
var trigger2 = TriggerBuilder.Create()
//.StartNow()
//.ForJob("myJob", "jobGroup")
.StartAt(DateBuilder.EvenSecondDateAfterNow())
.UsingJobData("name", "trigger2")
.WithPriority()
.EndAt(DateTimeOffset.Now.AddMinutes())
.WithIdentity(new TriggerKey("myTrigger"))
.WithSimpleSchedule(x => x.RepeatForever().WithIntervalInSeconds())
.Build();
/// <summary>
/// Use a <see cref="TriggerKey" /> with the given name and default group to
/// identify the Trigger.
/// </summary>
/// <remarks>
/// <para>If none of the 'withIdentity' methods are set on the TriggerBuilder,
/// then a random, unique TriggerKey will be generated.</para>
/// </remarks>
/// <param name="name">the name element for the Trigger's TriggerKey</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="TriggerKey" />
/// <seealso cref="ITrigger.Key" />
public TriggerBuilder WithIdentity(string name)
{
key = new TriggerKey(name, null);
return this;
} /// <summary>
/// Use a TriggerKey with the given name and group to
/// identify the Trigger.
/// </summary>
/// <remarks>
/// <para>If none of the 'withIdentity' methods are set on the TriggerBuilder,
/// then a random, unique TriggerKey will be generated.</para>
/// </remarks>
/// <param name="name">the name element for the Trigger's TriggerKey</param>
/// <param name="group">the group element for the Trigger's TriggerKey</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="TriggerKey" />
/// <seealso cref="ITrigger.Key" />
public TriggerBuilder WithIdentity(string name, string group)
{
key = new TriggerKey(name, group);
return this;
} /// <summary>
/// Use the given TriggerKey to identify the Trigger.
/// </summary>
/// <remarks>
/// <para>If none of the 'withIdentity' methods are set on the TriggerBuilder,
/// then a random, unique TriggerKey will be generated.</para>
/// </remarks>
/// <param name="key">the TriggerKey for the Trigger to be built</param>
/// <returns>the updated TriggerBuilder</returns>
/// <seealso cref="TriggerKey" />
/// <seealso cref="ITrigger.Key" />
public TriggerBuilder WithIdentity(TriggerKey key)
{
this.key = key;
return this;
}
8.WithDesciption
添加说明
var trigger2 = TriggerBuilder.Create()
//.StartNow()
//.ForJob("myJob", "jobGroup")
.StartAt(DateBuilder.EvenSecondDateAfterNow())
.UsingJobData("name", "trigger2")
.WithPriority()
.EndAt(DateTimeOffset.Now.AddMinutes())
.WithIdentity(new TriggerKey("myTrigger"))
.WithDescription("description")
.WithSimpleSchedule(x => x.RepeatForever().WithIntervalInSeconds())
.Build();
9.WithDailyTimeIntervalSchedule、WithSimpleSchedule、WithCronSchedule、WithCalendarIntervalSchedule
一些ScheduleBuilder的实现类
Quartz.Net系列(六):Quartz五大构件Trigger之TriggerBuilder解析的更多相关文章
- Quartz.Net系列(七):Trigger之SimpleScheduleBuilder详解
所有方法图 1.SimpleScheduleBuilder RepeatForever:指定触发器将无限期重复. WithRepeatCount:指定重复次数 var trigger = Trigge ...
- Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解
1.介绍 中文意义就是每日时间间隔计划生成 2.API讲解 (1)WithInterval.WithIntervalInHours.WithIntervalInMinutes.WithInterval ...
- Quartz.Net系列(九):Trigger之CronScheduleBuilder和Cron表达式详解
1.使用 var scheduler =await StdSchedulerFactory.GetDefaultScheduler(); await scheduler.Start(); var jo ...
- java基础解析系列(六)---深入注解原理及使用
java基础解析系列(六)---注解原理及使用 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系列(二)---Integer ja ...
- java基础解析系列(六)---注解原理及使用
java基础解析系列(六)---注解原理及使用 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系列(二)---Integer缓存及 ...
- 第九节: 利用RemoteScheduler实现Sheduler的远程控制 第八节: Quartz.Net五大构件之SimpleThreadPool及其四种配置方案 第六节: 六类Calander处理六种不同的时间场景 第五节: Quartz.Net五大构件之Trigger的四大触发类 第三节: Quartz.Net五大构件之Scheduler(创建、封装、基本方法等)和Job(创建、关联
第九节: 利用RemoteScheduler实现Sheduler的远程控制 一. RemoteScheduler远程控制 1. 背景: 在A服务器上部署了一个Scheduler,我们想在B服务器上 ...
- Quartz.Net系列(十六):通过Plugins模式使用Xml方式配置Job和Trigger和自定义LogPrivider
1.简单介绍 Quarz.Net中采用插件式来实现配置文件配置,通过XMLSchedulingDataProcessor类进行Xml数据处理 默认配置文件命名:quart_jobs.xml publi ...
- 第三节: Quartz.Net五大构件之Scheduler(创建、封装、基本方法等)和Job(创建、关联等)
一. 五大构件 引言: Quartz.Net的五大构件 1. 调度器:Scheduler 2. 作业任务:Job 3. 触发器: Trigger 4. 线程池: SimpleThreadPoo ...
- 定时调度系列之Quartz.Net详解
一. 背景 我们在日常开发中,可能你会遇到这样的需求:"每个月的3号给用户发信息,提醒用户XXX "."每天的0点需要统计前一天的考勤记录"."每个月 ...
随机推荐
- SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'xxx' already exists
字面意思 xxx表已存在. 在使用laravel 写同步结构的时候 最好习惯性写个if语句判定是否存在 // 判断数据表是否存在 Schema::hasTable('table'); // 判断数据 ...
- Mysql的一次查询的过程
1.用户发起请求,这里往往时多线程并发访问 2.去数据库线程池拿数据库链接,如果没有线程池,每次访问都要和数据库建立一次连接,非常耗时,效率低下 3.数据库层面上来说,可能会有多个系统同时访问它,所以 ...
- [自动化-脚本]001.自动领淘金币:Anyweb模拟操作
通过模拟手工操作的方法领取淘金币.该方法万能且通用,有能力的还可以自行修改脚本. 工具 软件下载 anywebscript.com 方法/步骤 1.安装软件如图所示 2.设置脚本: (1)进入网站:[ ...
- html5学习之路_004
HTML表单 表单用于获取不同类型的用户输入 常用表单标签 下面为一个简单的表单: <!DOCTYPE html> <html> <head lang="en& ...
- CNN卷积神经网络的卷积层、池化层的输出维度计算公式
卷积层Conv的输入:高为h.宽为w,卷积核的长宽均为kernel,填充为pad,步长为Stride(长宽可不同,分别计算即可),则卷积层的输出维度为: 其中上开下闭开中括号表示向下取整. MaxPo ...
- 如何看待 HashiCorp 官宣,不允许中国境内使用其旗下产品?
欢迎转载,欢迎看官推荐. 前言 HashiCorp 官方宣布,不允许中国境内使用.部署和安装该企业旗下的企业版产品和软件.该公司比较知名的产品有:Terraform.Consul.Vagrant 等. ...
- Rocket - interrupts - Parameters
https://mp.weixin.qq.com/s/eD1_hG0n8W2Wodk25N5KnA 简单介绍interrupts相关的Parameters. 1. IntRange 定义一个中断号区间 ...
- 【Flume】安装与测试
1.下载安装包http://archive.apache.org/dist/flume/ 2.解压命令tar -zxvf 压缩包 -C 路径 3.配置环境变量 export FLUME_HOME=/o ...
- Java实现 LeetCode 581 最短无序连续子数组(从两遍搜索找两个指针)
581. 最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: ...
- java实现自行车行程
** 自行车行程** 计算行程 低碳生活,有氧运动.骑自行车出行是个好主意.小明为自己的自行车装了个计数器,可以计算出轮子转动的圈数.在一次骑车旅行中,出发时计算器的示数为begin,到达目的地时的示 ...