zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    设计模式复习-状态模式
    设计模式复习-观察者模式
    设计模式复习-建造者模式
    设计模式复习-外观模式
    推荐算法-协同过滤
    设计模式复习-模板方法模式
    设计模式复习-原型模式
    设计模式复习-代理模式
    神经网络与机器学习 笔记—泛化和交叉验证
    神经网络与机器学习 笔记—改善反向传播的性能试探法
  • 原文地址:https://www.cnblogs.com/vic-tory/p/13129704.html
Copyright © 2011-2022 走看看