有关Quartz.NET,与一线码农大佬对个线?

简介: 跟[一线码农大佬]翻译的某技术文对个线

最近看到一线码农大佬翻译的《如何在 ASP.NET Core 中使用 Quartz.NET 执行任务调度


行文思路:


  1. 安装Quartz.NET


  1. Quartz.NET 中的Job,triggers 和 Schedulers


  1. 创建 Scheduler


  1. 开启和停止 scheduler


  1. 创建 job 工厂


  1. 创建 JobMetadata 存储你的 job 元数据


不可否认,一线大佬的翻译文还是相当精准的, 但个人认为这篇文章的底稿有点硬输出,并没有以一个流畅、直观的编码思路来讲述[如何在ASP.NET Core中使用Quartz.NET 执行定时任务]。


尤其是下面这段:


7f38d8d6ff49b753816dd0503ee0199c.png


想起我之前也写了《ASP.NET Core+Quartz.Net实现web定时任务》, 文章以一个简单的定时任务讲述了Quartz.NET在ASP.NET Core中的应用思路,遇河架桥,遇山开路。

这里我要解释一下上图中:为什么要自定义一个Job工厂?

先看下官方JobFactory的作用:


ac37c2b3ab53cb45a0897fbf2ff0a68e.png


大意是说:


如果某触发器被触发,该触发器关联的Job将被调度器上配置的JobFactory初始化;

Quartz.NET默认的SimpleJobFactory工厂类,是利用反射+无参构造函数构造出Job实例。


翻源码:


//----------------选自Quartz.Simpl.SimpleJobFactory类-------------
using System;
using Quartz.Logging;
using Quartz.Spi;
using Quartz.Util;
namespace Quartz.Simpl
{
    /// <summary> 
    /// The default JobFactory used by Quartz - simply calls 
    /// <see cref="ObjectUtils.InstantiateType{T}" /> on the job class.
    /// </summary>
    /// <seealso cref="IJobFactory" />
    /// <seealso cref="PropertySettingJobFactory" />
    /// <author>James House</author>
    /// <author>Marko Lahma (.NET)</author>
    public class SimpleJobFactory : IJobFactory
    {
        private static readonly ILog log = LogProvider.GetLogger(typeof (SimpleJobFactory));
        /// <summary>
        /// Called by the scheduler at the time of the trigger firing, in order to
        /// produce a <see cref="IJob" /> instance on which to call Execute.
        /// </summary>
        /// <remarks>
        /// It should be extremely rare for this method to throw an exception -
        /// basically only the case where there is no way at all to instantiate
        /// and prepare the Job for execution.  When the exception is thrown, the
        /// Scheduler will move all triggers associated with the Job into the
        /// <see cref="TriggerState.Error" /> state, which will require human
        /// intervention (e.g. an application restart after fixing whatever
        /// configuration problem led to the issue with instantiating the Job).
        /// </remarks>
        /// <param name="bundle">The TriggerFiredBundle from which the <see cref="IJobDetail" />
        ///   and other info relating to the trigger firing can be obtained.</param>
        /// <param name="scheduler"></param>
        /// <returns>the newly instantiated Job</returns>
        /// <throws>  SchedulerException if there is a problem instantiating the Job. </throws>
        public virtual IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            IJobDetail jobDetail = bundle.JobDetail;
            Type jobType = jobDetail.JobType;
            try
            {
                if (log.IsDebugEnabled())
                {
                    log.Debug($"Producing instance of Job '{jobDetail.Key}', class={jobType.FullName}");
                }
                return ObjectUtils.InstantiateType<IJob>(jobType);
            }
            catch (Exception e)
            {
                SchedulerException se = new SchedulerException($"Problem instantiating class '{jobDetail.JobType.FullName}'", e);
                throw se;
            }
        }
        /// <summary>
        /// Allows the job factory to destroy/cleanup the job if needed. 
        /// No-op when using SimpleJobFactory.
        /// </summary>
        public virtual void ReturnJob(IJob job)
        {
            var disposable = job as IDisposable;
            disposable?.Dispose();
        }
    }
}
//------------------节选自Quartz.Util.ObjectUtils类-------------------------
 public static T InstantiateType<T>(Type type)
{
     if (type == null)
     {
          throw new ArgumentNullException(nameof(type), "Cannot instantiate null");
     }
     ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
     if (ci == null)
     {
          throw new ArgumentException("Cannot instantiate type which has no empty constructor", type.Name);
     }
     return (T) ci.Invoke(new object[0]);
}


但是很多情况下我们定义的Job很可能依赖第三方服务,就比如一线大佬文中NotificationJob依赖了ILogger<NotificationJob> 服务。


这样默认的SimpleJobFactory不能满足实例化要求, 考虑将Job任务作为依赖注入组件,加入依赖注入容器。


关键思路:


IScheduler 开放了JobFactory 属性,便于你应用自定义的Job工厂;


在自定义Job工厂中,使用ASP.NET Core依赖注入容器
IServiceProvider解析出特定的Job。


相关文章
|
5天前
|
开发框架 .NET C#
【专栏】理解.NET 技术,提升开发水平
【4月更文挑战第29天】本文介绍了.NET技术的核心概念和应用,包括其跨平台能力、性能优化、现代编程语言支持及Web开发等特性。文章强调了深入学习.NET技术、关注社区动态、实践经验及学习现代编程理念对提升开发水平的重要性。通过这些,开发者能更好地利用.NET构建高效、可维护的多平台应用。
|
5天前
|
算法 Java 编译器
【专栏】.NET 开发:实现高效能的秘诀
【4月更文挑战第29天】本文探讨了提升.NET应用性能的三个方面:理解.NET运行时(垃圾回收、JIT编译器、异步编程和线程并发)、优化代码与算法(代码细节、数据结构选择和算法效率)以及利用工具和框架(性能分析工具、高性能库和CI/CD流程)。通过深入学习、合理设计和有效工具,开发者可实现.NET应用的高效能。
|
5天前
|
安全 Linux API
【专栏】.NET 技术:开启高效开发之门
【4月更文挑战第29天】本文介绍了.NET技术的起源、核心特性和应用场景,阐述了其如何促进高效开发。自2002年微软推出.NET Framework以来,历经.NET Core和.NET 5的演进,实现跨平台支持。.NET的核心特性包括跨平台、面向对象、安全可靠和高性能。借助丰富的类库、强大的开发工具、简洁的语言特性和社区资源,开发者能实现高效开发,创造更多可能性。
|
5天前
|
敏捷开发 缓存 中间件
【专栏】解读.NET 技术的高效开发模式
【4月更文挑战第29天】本文探讨了.NET技术的高效开发模式,关键要素包括面向对象编程、良好架构设计和高效代码管理。通过实例展示了在企业级应用和Web应用开发中如何运用这些原则,强调了分层架构、微服务架构以及遵循编程规范和使用版本控制的重要性。文章旨在为开发者提供.NET开发的实用指南和灵感,促进软件开发效率与质量的提升。
|
消息中间件 NoSQL 关系型数据库
.NET 入门到高级路线
.NET 入门到高级路线
168 0
|
C# 数据库
艾伟_转载:C# .NET学习经验总结
  1. 装箱、拆箱还是别名   许多介绍C# .NET学习经验的书上都有介绍 int -> Int32 是一个装箱的过程,反之则是拆箱的过程。许多其它变量类型也是如此,如:short Int16,long Int64 等。
921 0
|
Java Unix Linux
艾伟_转载:关于.NET VS JavaEE平台争论的沉思录
  最近关于.NET和java平台之间的争论似乎又热起来了,就我关注的两个社区,先是老赵的《为啥老赵不喜欢Java*语言*》。引发了博客园的大讨论,最近csdn又有人发博.Net与J2EE的快餐型比较,引发了空前的大讨论。
1208 0