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应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...
随机推荐
- 【scipy 基础】--聚类
物以类聚,聚类算法使用最优化的算法来计算数据点之间的距离,并将它们分组到最近的簇中. Scipy的聚类模块中,进一步分为两个聚类子模块: vq(vector quantization):提供了一种基于 ...
- 不可复制的PDF转成双层可复制PDF
有些PDF是通过扫描或者虚拟打印机生成的,这些PDF不可复制里边的内容 市面上的工具一般都是收费或者有水印,所以就萌生了自己搞一个的想法: 使用了以下三个开源库 PdfiumViewer PDF预览及 ...
- Idea单窗口导入多个项目模块
现在我们比较流行微服务,但是服务一旦多了,项目打开也是很麻烦的,运行内存16个G的电脑,基本上打开4,5个项目模块就顶不住了.那么,我们怎么把多个项目导入到一个idea窗口中呢? 实现效果 导入步骤 ...
- 第1章 .NET起步
1.1 什么是.NET? .NET 8.0 SDK下载地址:https://dotnet.microsoft.com/zh-cn/download/dotnet/8.0 .NET 是一个免费的跨平台开 ...
- 微信支付:wxpay.unifiedOrder(data)返回appid 与 openId 不配
原因:小程序和APP.公众号等支付方式夸端口调用支付,后台配置多个appId时 A程序中的openid 在B程序中支付.即使用A程序的openid和B程序的appIdy去调用wxpay.unified ...
- 将ECharts图表插入到Word文档中
@ 目录 在后端调用JS代码 准备ECharts库 生成Word文档 项目地址 库封装 本文示例 EChartsGen_DocTemplateTool_Sample 如何通过ECharts在后台生成图 ...
- UIPath之Excel操作
你的选择是做或不做,但不做就永远不会有机会. 一. UIPath操作Excel的两组方法 1. App Integration > Excel 特点: 对Excel里的操作必须包含在Exec ...
- vue + node 前后端分离项目解决跨域问题
vue + node 前后端分离项目解决跨域问题 由于前端 和 后端 项目运行于不同端口,无法直接传递数据 后端 app.js 添加如下代码 var cors = require('cors') ap ...
- CentOS(7.6)环境下迁移Mysql(5.7)的data目录到指定位置
第一步:关闭Mysql #关闭Mysql服务systemctl stop mysqld#查看Mysql服务状态 ps -ef|grep mysql 第二步:创建新目录,并拷贝数据文件 #创建data文 ...
- Rong晔大佬教程学习(2):取指
1.rvseed_defines.v(定义了一些参数,没有实际意义) 该文件定义了一些基本参数,在后续的代码中都会调用该文件 // simulation clock period `define SI ...