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应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...
随机推荐
- ubuntu实时查看网速
可以使用ifstat这个命令 安装 apt install ifstat 1 使用,直接打命令就行 ifstat
- Arduino 板的说明
Arduino 板的说明 在本章中,我们将了解 Arduino 板上的不同组件.将学习 Arduino UNO 板,因为它是 Arduino 板系列中最受欢迎的.此外,它是开始使用电子和编码的最佳板. ...
- 分布式应用开发的核心技术系列之——基于TCP/IP的原始消息设计
本文由葡萄城技术团队原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 本文的内容主要围绕以下几个部分: TCP/IP的简单介绍. 消息的介绍 ...
- C#桶排序算法
前言 桶排序是一种线性时间复杂度的排序算法,它将待排序的数据分到有限数量的桶中,每个桶再进行单独排序,最后将所有桶中的数据按顺序依次取出,即可得到排序结果. 实现原理 首先根据待排序数据,确定需要的桶 ...
- 【matplotlib 实战】--雷达图
雷达图(Radar Chart),也被称为蛛网图或星型图,是一种用于可视化多个变量之间关系的图表形式.雷达图是一种显示多变量数据的图形方法.通常从同一中心点开始等角度间隔地射出三个以上的轴,每个轴代表 ...
- SpringBoot数据响应、分层解耦、三层架构
响应数据 @ResponseBody 类型:方法注解.类注解 位置:Controller方法.类上 作用:将方法返回值直接响应,如果返回值类型是 实体对象/集合 ,将会转换为json格式响应 说明:@ ...
- vscode编写keil工程项目
vscode 前言 1 安装vscode 2 安装插件 2.1 设置中文 2.2 安装Keil Assistant 2.3 常用安装 3 快捷键 前言 我使用vscode只是用来为了弥补keil编写的 ...
- 在centos7上使用 docker安装mongodb挂载宿主机以及创建其数据库的用户名和密码(最新版本)
前言 因为博主在使用docker安装mongodb并挂载时,发现在网上搜了好多都是以前版本的mongodb,并且按照他们操作总是在进入mongodb出问题,博主搞了好久终于弄好了,故写下博客,供有需要 ...
- Codeforces Round #701 (Div. 2) A~C 题解
写在前边 链接:Codeforces Round #701 (Div. 2) 数学场,题目描述简单粗暴,思路很妙,代码短的不行,都是好神奇的一些题目. A. Add and Divide 链接:A题链 ...
- (数据科学学习手札155)基于martin为在线地图构建字体切片服务
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,在之前的一篇文章(基于mart ...