Quartz.Net系列(五):Quartz五大構件Job之JobBuilder解析


 所有方法圖:

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;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM