Quartz.Net—JobBuilder

摘要:
通过静态方法构建JobBuilder实例,然后调用类方法Build()来创建IJobDetail的实现。因为它是所使用的类型,所以在执行作业任务时必须通过反射获得对象。设计jobType的优点是,第三方提供的任务只要继承IJob接口,就可以直接使用。publicJobBuilderOfType<T>(){returnOfType;}///<summary˃///设置当///触发与此JobDetail关联时将立即启动并执行的类。/////更新的JobBuilder///publicJobBuilderOfType{JobType=type;returnthis;}2.StroeDurableJob持久性默认情况下,当没有触发器时,作业将被删除。IJobDetailjob=作业生成器。创建()。持久储存。Build();如果设置为true,则不会删除它。ramjobstore中同时存在作业和触发器。
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名字

Quartz.Net—JobBuilder第1张Quartz.Net—JobBuilder第2张
/// <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;
}
View Code

就是制定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》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇移动端——meta标签Android LBS 百度地图(参考: 《第一行代码》第2版(郭霖)11.3.2 确定自己位置的经纬度:准确数字信息)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Java 并发系列之十:java 并发框架(2个)

108maximumPoolSize109线程池中允许的最大线程数。线程池的阻塞队列满了之后,如果还有任务提交,如果当前的线程数小于maximumPoolSize,则会新建线程来执行任务。线程的创建和销毁是需要代价的。121threadFactory122用于设置创建线程的工厂。所谓拒绝策略,是指将任务添加到线程池中时,线程池拒绝该任务所采取的相应策略。...

说说接口封装

今天,我为同事封装了一个接口。当谈到接口封装时,有很多关于它的讨论。在很多情况下,说一个服务好,一个服务坏,实际上是在吐槽服务团队之外暴露的界面质量。无论哪种语言,抽象的封装接口都由一个函数名、几个参数和几个返回值组成。总之,参数不应该被封装……我们在内部尝试接口_Catch不会抛出异常,所有信息都将以错误代码的形式返回。就php而言,建议进行异常处理。...

Uni-app v-on监听事件

使用标记上的v-on监视事件。缩写为@click common click events方法:方法:{Focus(){console.log;},blur(){console.log;},confirm(){console.log;},click(){console.log;},tap(){console.log;},longpress(){console....

DB2字符函数简介及使用

Param2可以是编码单元16-16位UTF-16编码,也就是说,字符串表示为16位UTF-18编码字符串。Codeunits32-32位UTF-32编码,即字符串表示为32位UTF 32编码字符串。请注意,定义为FORBITDATA的字符串不能转换为图形字符。如果length<length,则来自的原始字符串短于结果中的长度。...

.NET5 ABP框架(一)

授权-ABP可以以声明的方式检查权限。如果发生异常,ABP将自动记录并向客户机返回适当的结果。默认情况下,ABP使用Log4Net写入日志。当然,我们也可以通过修改配置来使用其他日志框架。除了本示例中显示的ABP的优点之外,ABP还提供了一个健壮的基础架构和应用程序模型。...

微信小程序-获取input值的两种方法

1、bindinput其中e.detail是获取input数据其中包含value值,cursor是获取光标的位置。...