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

1.Create,OfType
在JobBuilder中有五种方法执行Action:
var job1 = JobBuilder.Create().OfType<FirstJob>().Build();
var job2 = JobBuilder.Create<FirstJob>().Build();
var job3 = JobBuilder.CreateForAsync<FirstJob>().Build();
var job4 = JobBuilder.Create(typeof(FirstJob));
var job5 = JobBuilder.Create().OfType(typeof(FirstJob));
底层都是调用OfType方法来获取Type
public JobBuilder OfType<T>()
{
return this.OfType(typeof (T));
} /// <summary>
/// Set the class which will be instantiated and executed when a
/// Trigger fires that is associated with this JobDetail.
/// </summary>
/// <returns>the updated JobBuilder</returns>
/// <seealso cref="P:Quartz.IJobDetail.JobType" />
public JobBuilder OfType(Type type)
{
this.jobType = type;
return this;
}
2.RequestRecovery
请求恢复,也就是说当应用发生故障的时候,是否重新执行默认是false
var job = JobBuilder.Create<FirstJob>()
.RequestRecovery()//请求恢复,也就是说当应用发生故障的时候,是否重新执行
.Build();
public JobBuilder RequestRecovery()
{
return this.RequestRecovery(true);
} /// <summary>
/// Instructs the <see cref="T:Quartz.IScheduler" /> whether or not the job
/// should be re-executed if a 'recovery' or 'fail-over' situation is
/// encountered.
/// </summary>
/// <remarks>
/// If not explicitly set, the default value is <see langword="false" />.
/// </remarks>
/// <param name="shouldRecover"></param>
/// <returns>the updated JobBuilder</returns>
public JobBuilder RequestRecovery(bool shouldRecover)
{
this.shouldRecover = shouldRecover;
return this;
}
3.WithDescription
设置描述
var job = JobBuilder.Create<FirstJob>()
.RequestRecovery()//请求恢复,也就是说当应用发生故障的时候,是否重新执行
.WithDescription("描述") //设置描述
.Build();
public JobBuilder WithDescription(string description)
{
this.description = description;
return this;
}
4.WithIdentity
给JobDetail起一个Id,方便后面检索
WithIdentity的三种写法
var job = JobBuilder.Create<FirstJob>()
.RequestRecovery()//请求恢复,也就是说当应用发生故障的时候,是否重新执行
.WithDescription("描述") //设置描述
.WithIdentity("myJob","myJobGroup")
.WithIdentity("myJob")
.WithIdentity(JobKey.Create("myJob"))
.Build();
public JobBuilder WithIdentity(string name)
{
this.key = new JobKey(name, (string) null);
return this;
} /// <summary>
/// Use a <see cref="T:Quartz.JobKey" /> with the given name and group to
/// identify the JobDetail.
/// </summary>
/// <remarks>
/// <para>If none of the 'withIdentity' methods are set on the JobBuilder,
/// then a random, unique JobKey will be generated.</para>
/// </remarks>
/// <param name="name">the name element for the Job's JobKey</param>
/// <param name="group"> the group element for the Job's JobKey</param>
/// <returns>the updated JobBuilder</returns>
/// <seealso cref="T:Quartz.JobKey" />
/// <seealso cref="P:Quartz.IJobDetail.Key" />
public JobBuilder WithIdentity(string name, string group)
{
this.key = new JobKey(name, group);
return this;
} /// <summary>
/// Use a <see cref="T:Quartz.JobKey" /> to identify the JobDetail.
/// </summary>
/// <remarks>
/// <para>If none of the 'withIdentity' methods are set on the JobBuilder,
/// then a random, unique JobKey will be generated.</para>
/// </remarks>
/// <param name="key">the Job's JobKey</param>
/// <returns>the updated JobBuilder</returns>
/// <seealso cref="T:Quartz.JobKey" />
/// <seealso cref="P:Quartz.IJobDetail.Key" />
public JobBuilder WithIdentity(JobKey key)
{
this.key = key;
return this;
}
5.StoreDurably
保留存储,也就是说当执行完后,保留Job
var job = JobBuilder.CreateForAsync<FirstJob>()
.StoreDurably()
.Build();

/// <summary>
/// Whether or not the job should remain stored after it is
/// orphaned (no <see cref="T:Quartz.ITrigger" />s point to it).
/// </summary>
/// <remarks>
/// If not explicitly set, the default value is <see langword="false" />
/// - this method sets the value to <code>true</code>.
/// </remarks>
/// <returns>the updated JobBuilder</returns>
/// <seealso cref="P:Quartz.IJobDetail.Durable" />
public JobBuilder StoreDurably()
{
return this.StoreDurably(true);
} /// <summary>
/// Whether or not the job should remain stored after it is
/// orphaned (no <see cref="T:Quartz.ITrigger" />s point to it).
/// </summary>
/// <remarks>
/// If not explicitly set, the default value is <see langword="false" />.
/// </remarks>
/// <param name="durability">the value to set for the durability property.</param>
/// <returns>the updated JobBuilder</returns>
/// <seealso cref="P:Quartz.IJobDetail.Durable" />
public JobBuilder StoreDurably(bool durability)
{
this.durability = durability;
return this;
}
6.UsingJobData,SetJobData
添加Job数据
每个JobDetail内都有一个JobDataMap,包含了关联到这个Job的数据,在Job类中,可以通过context取出该数据,进行业务流程处理。
jec = new JobExecutionContextImpl(scheduler, firedTriggerBundle, job);
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("UserName","Jack");
var job = JobBuilder.CreateForAsync<FirstJob>()
//.StoreDurably()
.SetJobData(new JobDataMap(dict))
.UsingJobData("Password","123456")
.Build();
public class FirstJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Task.Run(() =>
{
var userName=context.MergedJobDataMap.GetString("UserName");
var password = context.MergedJobDataMap.GetString("Password");
Console.WriteLine(userName);
Console.WriteLine(password);
//Console.WriteLine("Hello World !");
});
}
}

public JobBuilder UsingJobData(string key, double value)
{
jobDataMap.Put(key, value);
return this;
} /// <summary>
/// Add the given key-value pair to the JobDetail's <see cref="JobDataMap" />.
/// </summary>
///<returns>the updated JobBuilder</returns>
/// <seealso cref="IJobDetail.JobDataMap" />
public JobBuilder UsingJobData(string key, bool value)
{
jobDataMap.Put(key, value);
return this;
} /// <summary>
/// Add all the data from the given <see cref="JobDataMap" /> to the
/// <see cref="IJobDetail" />'s <see cref="JobDataMap" />.
/// </summary>
///<returns>the updated JobBuilder</returns>
/// <seealso cref="IJobDetail.JobDataMap" />
public JobBuilder UsingJobData(JobDataMap newJobDataMap)
{
jobDataMap.PutAll(newJobDataMap);
return this;
} /// <summary>
/// Replace the <see cref="IJobDetail" />'s <see cref="JobDataMap" /> with the
/// given <see cref="JobDataMap" />.
/// </summary>
/// <param name="newJobDataMap"></param>
/// <returns></returns>
public JobBuilder SetJobData(JobDataMap newJobDataMap)
{
jobDataMap = newJobDataMap;
return this;
}
Quartz.Net系列(五):Quartz五大构件Job之JobBuilder解析的更多相关文章
- Quartz.Net系列(六):Quartz五大构件Trigger之TriggerBuilder解析
所有方法图: 1.Create.Build Create:创建一个TriggerBuilder Build:生成Trigger var trigger = TriggerBuilder.Create( ...
- 第九节: 利用RemoteScheduler实现Sheduler的远程控制 第八节: Quartz.Net五大构件之SimpleThreadPool及其四种配置方案 第六节: 六类Calander处理六种不同的时间场景 第五节: Quartz.Net五大构件之Trigger的四大触发类 第三节: Quartz.Net五大构件之Scheduler(创建、封装、基本方法等)和Job(创建、关联
第九节: 利用RemoteScheduler实现Sheduler的远程控制 一. RemoteScheduler远程控制 1. 背景: 在A服务器上部署了一个Scheduler,我们想在B服务器上 ...
- 第三节: Quartz.Net五大构件之Scheduler(创建、封装、基本方法等)和Job(创建、关联等)
一. 五大构件 引言: Quartz.Net的五大构件 1. 调度器:Scheduler 2. 作业任务:Job 3. 触发器: Trigger 4. 线程池: SimpleThreadPoo ...
- Quartz.NET开源作业调度框架系列(五):AdoJobStore保存job到数据库
Quartz.NET 任务调度的核心元素是 scheduler, trigger 和 job,其中 trigger(用于定义调度时间的元素,即按照什么时间规则去执行任务) 和 job 是任务调度的元数 ...
- 定时调度系列之Quartz.Net详解
一. 背景 我们在日常开发中,可能你会遇到这样的需求:"每个月的3号给用户发信息,提醒用户XXX "."每天的0点需要统计前一天的考勤记录"."每个月 ...
- 定时调度系列之Quartz.Net详解(转)
出处:https://www.cnblogs.com/yaopengfei/p/9216229.html 一. 背景 我们在日常开发中,可能你会遇到这样的需求:"每个月的3号给用户发信息,提 ...
- iPhone之Quartz 2D系列--图形上下文(2)(Graphics Contexts)
以下几遍关于Quartz 2D博文都是转载自:http://www.cocoachina.com/bbs/u.php?action=topic&uid=38018 iPhone之Quartz ...
- iPhone之Quartz 2D系列--编程指南(1)概览
以下几遍关于Quartz 2D博文都是转载自:http://www.cocoachina.com/bbs/u.php?action=topic&uid=38018 iPhone之Quartz ...
- 定时组件quartz系列<三>quartz调度机制调研及源码分析
quartz2.2.1集群调度机制调研及源码分析引言quartz集群架构调度器实例化调度过程触发器的获取触发trigger:Job执行过程:总结:附: 引言 quratz是目前最为成熟,使用最广泛的j ...
- 定时组件quartz系列<二>quartz的集群原理
1.基本信息: Quartz是一个开源的作业调度框架,它完全由java写成,并设计用于J2Se和J2EE应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...
随机推荐
- Capture Data.dmg
苹果apple mac 系统检测 日常分析 软件 https://gigafiles.apple.com/#/download 现有文件可供下载. 备注 Capture Data 9.9.0 Th ...
- PostgreSQL快速导入千万条数据
目录 一.测试环境 二.修改源数据为COPY可用的格式 三.DDL 四.COPY 五.结论 为了与MySQL做个对比,做一个PG的数据导入测试,使用COPY方式,测试环境保持一致,具体如下所述. 一. ...
- [CISCN 2019华东南]Web11
看到下面connection 里面的内容有一点像抓包出来的 就抓包试试 似乎感觉也没有什么用 看到这个东西,那么就想到改IP 添加X-Forwarded-For:127.0.0.1 发现这个IP随着我 ...
- php反序列化--[SWPUCTF 2021 新生赛]no_wakeup
打开网站发现这个,点击 ??? 就看到了代码: 发现是PHP反序列化, 但和一般的PHP反序列化不同的是,多了一个_wakeup函数,然后就去网上搜了一下, 发现是一个cve漏洞CVE-2016-7 ...
- CSS之3D翻转效果
<!DOCTYPE html> <html> <head> <title></title> <style type="tex ...
- wireshark数据包时间戳修改
1.数据包格式 两种数据包格式,pcap和pcapng两种.pcapng为升级版,时间戳细粒度更高. 2.时间戳位置(arrival time) 时间戳在数据包中表现为物理层的arrival time ...
- Dubbo 路由及负载均衡性能优化
作者:vivo 互联网中间件团队- Wang Xiaochuang 本文主要介绍在vivo内部针对Dubbo路由模块及负载均衡的一些优化手段,主要是异步化+缓存,可减少在RPC调用过程中路由及负载均衡 ...
- 设置Eclipse自动补全变量名
具体配置步骤如下: Windows -> Preferences -> Java -> Editor -> Content Assist 选中 Enable auto-acti ...
- JS判断点是否在线段上
本文利用向量的点积和叉积来判断点是否在线段上. 基础知识补充 从零开始的高中数学--向量.向量的点积.带你一次搞懂点积(内积).叉积(外积).Unity游戏开发--向量运算(点乘和叉乘 说明 点积可以 ...
- HDL刷题:Count clock
原题链接 要写一个12小时的时钟. 由题目得知,reset信号的优先级最高,其次是enable,这里很好实现. 我的思路: 写了一个4位的bcd计数器,并实例化了4个,对ss与mm的[7:4]与[3: ...