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应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...
随机推荐
- 9. 用Rust手把手编写一个wmproxy(代理,内网穿透等), HTTP2改造篇之HPACK示例, 了解http2头信息如何处理
9. 用Rust手把手编写一个wmproxy(代理,内网穿透等), HTTP2改造篇之HPACK示例, 了解http2头信息如何处理 项目 ++wmproxy++ gite: https://gite ...
- CF433B
题目简化和分析: 为了更加快速的求出答案,好像没前缀和快速. 为了大家更好的理解线段树,我们使用了线段树. 如果您并不了解线段树,可以转战模板. 因为我们知道线段树可以快速求区间和,于是我们建两棵树. ...
- 算法修养--广度优先搜索BFS
广度优先算法(BFS) 广度优先算法(Breadth-First Search)是在图和树领域的搜索方法,其核心思想是从一个起始点开始,访问其所有的临近节点,然后再按照相同的方式访问这些临近节点的节点 ...
- [ABC201D] Game in Momotetsu World 题解
Game in Momotetsu World 题目大意 在一个 \(n\times m\) 的网格中,存在红色和蓝色两种格子,红色格子用 - 表示,蓝色格子用 + 表示. 现在 Takahashi ...
- AGC 补题笔记
[AGC001] A.BBQ Easy 由于最大数肯定要和一个比自己小的数搭配保留该数,不如选择保留次大数,如此递归即解.因此将序列排序后输出序号为奇数的数即可. B.Mysterious Light ...
- FDA周五发布的药物安全警示信息相对会较少地被媒体传播
The Friday Effect: Firm Lobbying, the Timing of Drug Safety Alerts, and Drug Side Effects 周五发布的药物安全警 ...
- HarmonyOS 高级特性
引言 本章将探讨 HarmonyOS 的高级特性,包括分布式能力.安全机制和性能优化.这些特性可以帮助你构建更强大.更安全.更高效的应用. 目录 HarmonyOS 的分布式能力 HarmonyOS ...
- 微服务系列-基于Spring Cloud Eureka进行服务的注册与消费
公众号「架构成长指南」,专注于生产实践.云原生.分布式系统.大数据技术分享. 在之前的几个教程中,我们学了: 使用 RestTemplate 的 Spring Boot 微服务通信示例 使用 WebC ...
- 开源一套快速部署程序的工具(CI/CD)
随着微服务越写越多,程序发布就成了一个麻烦事,所以写了一个部署工具 Vela,只要填写一个git地址.编译命令等简单信息,就能自动完成程序的部署. Vela 特性: 代码可在任意一台电脑自动完成编译, ...
- xv6:labs2 syscall
lab2 1.lab2的内容总结:关于系统调用整个跟踪过程: 使用系统调用时,用户态会通过软中断(trap,陷阱)进入内核中,由trap识别中断来自系统调用,然后调用syscall函数, 跟踪过程: ...