Quartz.Net—JobBuilder
JobBuilder
JobBuilder是一个建造者模式,链式建造。通过静态方法构建一个JobBuilder实例,然后再调用类方法Build()创建一个IJobDetail的实现。
1、静态方法
public static JobBuilder Create()
{
return new JobBuilder();
} /// <summary>
/// Create a JobBuilder with which to define a <see cref="IJobDetail" />,
/// and set the class name of the job to be executed.
/// </summary>
/// <returns>a new JobBuilder</returns>
public static JobBuilder Create(Type jobType)
{
JobBuilder b = new JobBuilder();
b.OfType(jobType);
return b;
} /// <summary>
/// Create a JobBuilder with which to define a <see cref="IJobDetail" />,
/// and set the class name of the job to be executed.
/// </summary>
/// <returns>a new JobBuilder</returns>
public static JobBuilder Create<T>() where T : IJob
{
JobBuilder b = new JobBuilder();
b.OfType(typeof(T));
return b;
} /// <summary>
/// Create a JobBuilder with which to define a <see cref="IJobDetail" />,
/// and set the class name of the job to be executed.
/// </summary>
/// <returns>a new JobBuilder</returns>
public static JobBuilder CreateForAsync<T>() where T : IJob
{
JobBuilder b = new JobBuilder();
b.OfType(typeof(T));
return b;
}
上面主要就是通过静态方法创建一个对象实例,或并且制定他的jobType 类型。既然是使用的类型,那么执行job任务的时候,一定是通过反射获取对象的。
下面试类方法,设置jobType类型的。
这种设计一个jobType的好处就是,任务第三方提供的任务,只要继承了 IJob接口的,都可以直接拿过来使用。
public JobBuilder OfType<T>()
{
return 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="IJobDetail.JobType" />
public JobBuilder OfType(Type type)
{
jobType = type;
return this;
}
2、StroreDurably Job持久化
默认情况下 job没有trigger的时候会被删除,
IJobDetail job = JobBuilder.Create<MyJob2>().StoreDurably(true).Build();
设置为true则不会删除。
job和trigger都是存在ramjobstore这个里面。
3、job名字
/// <summary>
/// Use a <see cref="JobKey" /> with the given name and default 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>
/// <returns>the updated JobBuilder</returns>
/// <seealso cref="JobKey" />
/// <seealso cref="IJobDetail.Key" />
public JobBuilder WithIdentity(string name)
{
key = new JobKey(name, null);
return this;
} /// <summary>
/// Use a <see cref="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="JobKey" />
/// <seealso cref="IJobDetail.Key" />
public JobBuilder WithIdentity(string name, string group)
{
key = new JobKey(name, group);
return this;
} /// <summary>
/// Use a <see cref="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="JobKey" />
/// <seealso cref="IJobDetail.Key" />
public JobBuilder WithIdentity(JobKey key)
{
this.key = key;
return this;
}
就是制定job的名子,这里名字类型为JobKey。如果没有指定名字,则在Builder的时候制定一个GUID
if (key == null)
{
key = new JobKey(Guid.NewGuid().ToString(), null);
}
4、附加信息 UsingJobData SetJobData
构建job的时候可以指定一些附加信息,然后再job方法中可以拿到这些i信息。
IJobDetail job = JobBuilder.Create<MyJob2>().StoreDurably(true).WithIdentity("ceshi2").UsingJobData("zangfeng","123").Build();
public class MyJob2 : IJob
{ public Task Execute(IJobExecutionContext context)
{
Console.WriteLine(context.JobDetail.JobDataMap["zangfeng"]);
return Task.Factory.StartNew(() => Console.WriteLine($"工作任务测试2:{DateTime.Now.ToString("yyyy-MM-dd
HH:mm:ss")}"));
}
}
5、创建 Build
public IJobDetail Build()
{
JobDetailImpl job = new JobDetailImpl(); job.JobType = jobType;
job.Description = description;
if (key == null)
{
key = new JobKey(Guid.NewGuid().ToString(), null);
}
job.Key = key;
job.Durable = durability;
job.RequestsRecovery = shouldRecover; if (!jobDataMap.IsEmpty)
{
job.JobDataMap = jobDataMap;
} return job;
}
就是返回一个IJobDetail的实现JobDetailImpl,并初始化这个类型
Quartz.Net—JobBuilder的更多相关文章
- Quartz
Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中.它提供了巨大的灵 活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度. eg: ja ...
- Spring Quartz实现任务调度
任务调度 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情 核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作 任务调度涉及多线程并发.线程池维 ...
- Quartz.net持久化与集群部署开发详解
序言 我前边有几篇文章有介绍过quartz的基本使用语法与类库.但是他的执行计划都是被写在本地的xml文件中.无法做集群部署,我让它看起来脆弱不堪,那是我的罪过. 但是quart.net是经过许多大项 ...
- Quartz.net开源作业调度框架使用详解
前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron-like表达式,集群,数据库.功能性能强大更不 ...
- [Quartz笔记]玩转定时调度
简介 Quartz是什么? Quartz是一个特性丰富的.开源的作业调度框架.它可以集成到任何Java应用. 使用它,你可以非常轻松的实现定时任务的调度执行. Quartz的应用场景 场景1:提醒和告 ...
- 关于Quartz.NET作业调度框架的一点小小的封装,实现伪AOP写LOG功能
Quartz.NET是一个非常强大的作业调度框架,适用于各种定时执行的业务处理等,类似于WINDOWS自带的任务计划程序,其中运用Cron表达式来实现各种定时触发条件是我认为最为惊喜的地方. Quar ...
- Quartz.net 开源job调度框架(二)----定点执行
在上一篇 Quartz.net 开源job调度框架(一) 中讲到了基本的使用以及配置job轮训数据执行 这种做法适用于对数据操作实时性要求不高的场景,在实际场景中还有一种比较常用的场景就是我们需要在 ...
- Quartz —— 从 HelloWorld 开始
1.Quartz 是用来完成任务调度的. 2.Quartz 的三个核心概念:调度器.任务.触发器. (1)Job:通过实现该接口来定义需要执行的任务. public interface Job { / ...
- quartz定时+log4net日志+exchangeservice发邮件
main using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
随机推荐
- 「雅礼集训 2018 Day2」农民
传送门 Description 「搞 OI 不如种田.」 小 D 在家种了一棵二叉树,第 ii 个结点的权值为 \(a_i\). 小 D 为自己种的树买了肥料,每天给树施肥. 可是几天后,小 D 却 ...
- Android Studio 和 SDK 下载、安装和环境变量配置
转Android Studio 和 SDK 下载.安装和环境变量配置https://blog.csdn.net/hahahhahahahha123456/article/details/8065135 ...
- [HackTheBox]WEB题目
0x01 [50 Points] I know Mag1k 问题描述: Can you get to the profile page of the admin? 访问分配的地址,是一个带注册的登入页 ...
- docker-machine 远程安装docker
base=https://github.com/docker/machine/releases/download/v0.14.0 && curl -L $base/docker-mac ...
- 大数据技术之kettle(1)——安装
一. kettle概述 1.kettle是一款开源的ETL工具,纯java编写,可以在Windows.Linux.Unix上运行,绿色无需安装,数据抽取高效稳定. 2.kettle的两种设计 简述: ...
- 苹果IPhone真机开发调试
需要 在苹果开发网站 加入真机的UDID, 并在Profile中勾选该手机
- 通过字节码分析this关键字以及异常表的作用
1.创建MyTest3类 public class MyTest3 { public void test(){ try { InputStream is = new FileInputStream(& ...
- Python3基础 只有int类型,没有long类型
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- WPF 问题 PresentationCore.dll!System.Windows.Media.Composition.DUCE.Channel.SyncFlush() 分析
错误信息: 没有足够的内存继续执行程序 在 System.Windows.Media.Composition.DUCE.Channel.SyncFlush() 在 System.Windows.Int ...
- ES6深入浅出-5 新版对象-1.如何创建对象
对象属性的加强: 可以通过new Object(), Object.create()方法,或者使用字面量标记(初始化标记)初始化对象. 一个对象初始化器,由花括号/大括号 ({}) 包含的一个由零个或 ...