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

 所有方法图:

Quartz.Net系列(五):Quartz五大构件Job之JobBuilder解析_第1张图片

1.Create,OfType 

在JobBuilder中有五种方法执行Action:

            var job1 = JobBuilder.Create().OfType().Build();

            var job2 = JobBuilder.Create().Build();

            var job3 = JobBuilder.CreateForAsync().Build();

            var job4 = JobBuilder.Create(typeof(FirstJob));

            var job5 = JobBuilder.Create().OfType(typeof(FirstJob));

底层都是调用OfType方法来获取Type

    public JobBuilder OfType()
    {
      return this.OfType(typeof (T));
    }

    /// 
    /// Set the class which will be instantiated and executed when a
    /// Trigger fires that is associated with this JobDetail.
    /// 
    /// the updated JobBuilder
    /// 
    public JobBuilder OfType(Type type)
    {
      this.jobType = type;
      return this;
    }

2.RequestRecovery

请求恢复,也就是说当应用发生故障的时候,是否重新执行默认是false

            var job = JobBuilder.Create()
                                .RequestRecovery()//请求恢复,也就是说当应用发生故障的时候,是否重新执行
                                .Build();
    public JobBuilder RequestRecovery()
    {
      return this.RequestRecovery(true);
    }

    /// 
    /// Instructs the  whether or not the job
    /// should be re-executed if a 'recovery' or 'fail-over' situation is
    /// encountered.
    /// 
    /// 
    /// If not explicitly set, the default value is .
    /// 
    /// 
    /// the updated JobBuilder
    public JobBuilder RequestRecovery(bool shouldRecover)
    {
      this.shouldRecover = shouldRecover;
      return this;
    }

3.WithDescription

设置描述

            var job = JobBuilder.Create()
                                .RequestRecovery()//请求恢复,也就是说当应用发生故障的时候,是否重新执行
                                .WithDescription("描述") //设置描述
                                .Build();
    public JobBuilder WithDescription(string description)
    {
      this.description = description;
      return this;
    }

4.WithIdentity

给JobDetail起一个Id,方便后面检索

WithIdentity的三种写法

            var job = JobBuilder.Create()
                                .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;
    }

    /// 
    /// Use a  with the given name and group to
    /// identify the JobDetail.
    /// 
    /// 
    /// If none of the 'withIdentity' methods are set on the JobBuilder,
    /// then a random, unique JobKey will be generated.
    /// 
    /// the name element for the Job's JobKey
    ///  the group element for the Job's JobKey
    /// the updated JobBuilder
    /// 
    /// 
    public JobBuilder WithIdentity(string name, string group)
    {
      this.key = new JobKey(name, group);
      return this;
    }

    /// 
    /// Use a  to identify the JobDetail.
    /// 
    /// 
    /// If none of the 'withIdentity' methods are set on the JobBuilder,
    /// then a random, unique JobKey will be generated.
    /// 
    /// the Job's JobKey
    /// the updated JobBuilder
    /// 
    /// 
    public JobBuilder WithIdentity(JobKey key)
    {
      this.key = key;
      return this;
    }

5.StoreDurably

保留存储,也就是说当执行完后,保留Job

            var job = JobBuilder.CreateForAsync()
                                .StoreDurably()
                                .Build();

 

Quartz.Net系列(五):Quartz五大构件Job之JobBuilder解析_第2张图片

 

 

 

    /// 
    /// Whether or not the job should remain stored after it is
    /// orphaned (no s point to it).
    /// 
    /// 
    /// If not explicitly set, the default value is 
    /// - this method sets the value to true.
    /// 
    /// the updated JobBuilder
    /// 
    public JobBuilder StoreDurably()
    {
      return this.StoreDurably(true);
    }

    /// 
    /// Whether or not the job should remain stored after it is
    /// orphaned (no s point to it).
    /// 
    /// 
    /// If not explicitly set, the default value is .
    /// 
    /// the value to set for the durability property.
    /// the updated JobBuilder
    /// 
    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()
                                //.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 !");
             });
        }
    }

 

Quartz.Net系列(五):Quartz五大构件Job之JobBuilder解析_第3张图片

 

 

        public JobBuilder UsingJobData(string key, double value)
        {
            jobDataMap.Put(key, value);
            return this;
        }

        /// 
        /// Add the given key-value pair to the JobDetail's .
        /// 
        ///the updated JobBuilder
        /// 
        public JobBuilder UsingJobData(string key, bool value)
        {
            jobDataMap.Put(key, value);
            return this;
        }

        /// 
        /// Add all the data from the given  to the 
        /// 's .
        /// 
        ///the updated JobBuilder
        /// 
        public JobBuilder UsingJobData(JobDataMap newJobDataMap)
        {
            jobDataMap.PutAll(newJobDataMap);
            return this;
        }

        /// 
        /// Replace the 's  with the
        /// given .
        /// 
        /// 
        /// 
        public JobBuilder SetJobData(JobDataMap newJobDataMap)
        {
            jobDataMap = newJobDataMap;
            return this;
        }

 

你可能感兴趣的:(Quartz.Net系列(五):Quartz五大构件Job之JobBuilder解析)