Quartz.Net—Job
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; }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")}")); } }
Quartz.Net—Job