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,并初始化这个类型