所有方法图:

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解析的更多相关文章

  1. Quartz.Net系列(六):Quartz五大构件Trigger之TriggerBuilder解析

    所有方法图: 1.Create.Build Create:创建一个TriggerBuilder Build:生成Trigger var trigger = TriggerBuilder.Create( ...

  2. 第九节: 利用RemoteScheduler实现Sheduler的远程控制 第八节: Quartz.Net五大构件之SimpleThreadPool及其四种配置方案 第六节: 六类Calander处理六种不同的时间场景 第五节: Quartz.Net五大构件之Trigger的四大触发类 第三节: Quartz.Net五大构件之Scheduler(创建、封装、基本方法等)和Job(创建、关联

    第九节: 利用RemoteScheduler实现Sheduler的远程控制   一. RemoteScheduler远程控制 1. 背景: 在A服务器上部署了一个Scheduler,我们想在B服务器上 ...

  3. 第三节: Quartz.Net五大构件之Scheduler(创建、封装、基本方法等)和Job(创建、关联等)

    一. 五大构件 引言: Quartz.Net的五大构件 1.  调度器:Scheduler 2.  作业任务:Job 3.  触发器: Trigger 4.  线程池: SimpleThreadPoo ...

  4. Quartz.NET开源作业调度框架系列(五):AdoJobStore保存job到数据库

    Quartz.NET 任务调度的核心元素是 scheduler, trigger 和 job,其中 trigger(用于定义调度时间的元素,即按照什么时间规则去执行任务) 和 job 是任务调度的元数 ...

  5. 定时调度系列之Quartz.Net详解

    一. 背景 我们在日常开发中,可能你会遇到这样的需求:"每个月的3号给用户发信息,提醒用户XXX "."每天的0点需要统计前一天的考勤记录"."每个月 ...

  6. 定时调度系列之Quartz.Net详解(转)

    出处:https://www.cnblogs.com/yaopengfei/p/9216229.html 一. 背景 我们在日常开发中,可能你会遇到这样的需求:"每个月的3号给用户发信息,提 ...

  7. iPhone之Quartz 2D系列--图形上下文(2)(Graphics Contexts)

    以下几遍关于Quartz 2D博文都是转载自:http://www.cocoachina.com/bbs/u.php?action=topic&uid=38018 iPhone之Quartz ...

  8. iPhone之Quartz 2D系列--编程指南(1)概览

    以下几遍关于Quartz 2D博文都是转载自:http://www.cocoachina.com/bbs/u.php?action=topic&uid=38018 iPhone之Quartz ...

  9. 定时组件quartz系列<三>quartz调度机制调研及源码分析

    quartz2.2.1集群调度机制调研及源码分析引言quartz集群架构调度器实例化调度过程触发器的获取触发trigger:Job执行过程:总结:附: 引言 quratz是目前最为成熟,使用最广泛的j ...

  10. 定时组件quartz系列<二>quartz的集群原理

    1.基本信息:      Quartz是一个开源的作业调度框架,它完全由java写成,并设计用于J2Se和J2EE应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...

随机推荐

  1. Xshell远程连接、MBR/BOOT和GRUB三者关系总结(系统启动过程)

    远程连接 远程连接Linux服务器的常见工具有Xshell.SecureCRT.Putty等,这些客户端连接工具在Linux服务器对应着相同SSH服务进程sshd,即远程连接都是使用SSH协议,当然它 ...

  2. Android历史版本

    目录 [隐藏]  1 测试版 2 版本列表 2.1 Android 1.0 2.2 Android 1.1 2.3 Android 1.5 Cupcake 2.4 Android 1.6 Donut ...

  3. 【WPF】单例软件实现自重启

    原文地址 https://www.cnblogs.com/younShieh/p/17749694.html 如果本文对你有所帮助,不妨点个关注和推荐呀,这是对笔者最大的支持~   在WPF应用程序中 ...

  4. 解决因对EFCore执行SQL方法不熟练而引起的问题

    前言 本文测试环境:VS2022+.Net7+MySQL 因为我想要实现使用EFCore去执行sql文件,所以就用到了方法ExecuteSqlAsync,然后就产生了下面的问题,首先因为方法接收的参数 ...

  5. LVS+keepalived配置高可用架构和负载均衡机制(1)

    一.基础知识 1. 四层负载均衡(基于IP+端口的负载均衡) 所谓四层负载均衡,也就是主要通过报文中的目标ip地址和端口,再加上负载均衡设备设置的服务器选择方式(分发策略,轮询),决定最终选择的内部服 ...

  6. CF1575I Illusions of the Desert

    prologue 还是太菜了,这个 154 行的树剖 20min 才敲完. analysis 首先,处理这个给到我们的这个式子. \[\max(| a _ u + a _ v |, | a _ u - ...

  7. android的listview控件,加了行内按钮事件导致行点击失效的问题

    近日,修改一个app,原来的listview中只有行点击事件 ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() ...

  8. Java Exception最佳实践(转)

    https://www.dubby.cn/detail.html?id=9033 1.异常介绍 2.Java中的异常介绍 3.自定义异常 4.几个建议 1)不要生吞异常 2)申明具体的异常 3)尽可能 ...

  9. CDQZ DS 题单总结(上)

    Preview: 个人认为是一套非常好的题单,能在各个方面练习 DS 水平,并且很多题型也是比赛当中的经典题 题单链接 Challenge 0: 简单的数组,懒得写了. Challenge 1: 考虑 ...

  10. Maven安装与配置【idea2022版本】

    一.maven下载 https://maven.apache.org/download.cgi 下载完毕后解压,注意解压路径不要有中文 二.环境变量 在环境变量Path里面新建(自己的maven的bi ...