一、经常在项目会用到定时任务同步数据或更新缓存等操作,在很久以前我们可能经常会用一个多线程或timer来做定时任务,这样能实现比较简单轻量级的任务;对于任务多且都调用频率不一样的任务,我们都会用到quartz.net这个组件;

                quartz.net是一个强大、开源、轻量的作业调度框架,你能够用它来为执行一个作业而创建简单的或复杂的作业调度。它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等

        二、  接下来简单演示一下quartz使用:

          2.1  首先新建一个aspnet core api 项目,通过nuget包管理器安装引用quartz

 

            2.2  新建一个模拟任务类userinfosyncjob 必须继承ijob接口

namespace quartzdemo.quarzs
{
    public class userinfosyncjob : ijob
    {
          
        public task execute(ijobexecutioncontext context)
        {
            return task.run(() =>
                        {  
                             //.....
                             console.writeline($"{datetime.now.tostring()}:开始执行同步第三方数据");
                             //....同步操作
                           
                        });
        }
    }
}

      2.2  声明一个启动类quartzstartup,来控制任务启动关闭等方法

            添加启动方法

   public async task<string> start()                                           
        {
            //1、声明一个调度工厂
            _schedulerfactory = new stdschedulerfactory();
            //2、通过调度工厂获得调度器
            _scheduler = await _schedulerfactory.getscheduler();  
            //3、开启调度器
            await _scheduler.start();
            //4、创建一个触发器
            var trigger = triggerbuilder.create()
                            .withsimpleschedule(x => x.withintervalinseconds(2).repeatforever())//每两秒执行一次
                            .build();
            //5、创建任务
            var jobdetail = jobbuilder.create<userinfosyncjob>()
                            .withidentity("job", "group")
                            .build();
            //6、将触发器和任务器绑定到调度器中
            await _scheduler.schedulejob(jobdetail, trigger);
            return await task.fromresult("将触发器和任务器绑定到调度器中完成");
        }

         2.3  在网站启动完成时调用quartzstartup的start方法开启任务

                   先注入 quartz调度类

          

                      添加网站启动开始方法

       2.4、运行效果,运行之前将控制台开启(方便查看任务是否在执行,实际环境可写日志)

 

    该调度任务完成,上方定义的触发器是2秒一次,所以该任务每隔2秒执行;(也可以通过配置文件,控制执行平率,cron表达式可以很好控制)

     三、第二结简单演示了quartz的基本用法,本文重点不是主要讲解quartz的用法,上方只是为了没使用过quartz的同行有个简单映像,如果想详细学习,博客园有很多类似的文章,也可以和我探讨一下!

            本文重点是每个任务类怎么通过注入获取其他类的使用及参数配置类等等;

         假如有这样一个需求,userinfosyncjob同步任务里面需要配置数据库连接参数和日志记录、缓存记录等,在之前我们可能通过配置类、日志类、缓存类以工厂形式单例创建获取。

在aspnet core自带ioc容器框架,很多配置类、日志类、缓存类等等,在全局很多地方都会使用,我们现在做法就是把这些类注入到ioc容器中,如果需要的只需要从构造方法中获取;

        我们都知道如果一个从构造方法中获取ioc容器里面的类型实例,必须该类型也要主要到ioc容器中,这样我们就要想办法把userinfosyncjob通过容器来创建生产;

        通过源码发现在quartz有一个默认的生成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();
  }
 }
}

      simplejobfactory 实现了ijobfactory接口,通过源码发现我们如果要替换该工厂来控制job的生成,只需要创建一个iocjobfactory来替换默认job工厂就行

 

public class iocjobfactory : ijobfactory
    {
        private readonly iserviceprovider _serviceprovider;
        public iocjobfactory(iserviceprovider serviceprovider)
        {
           _serviceprovider = serviceprovider;      
        }
        public ijob newjob(triggerfiredbundle bundle, ischeduler scheduler)
        {    
            return _serviceprovider.getservice(bundle.jobdetail.jobtype) as ijob;
        
        }

        public void returnjob(ijob job)
        {
            var disposable = job as idisposable;
            disposable?.dispose();

        }
    }

     在调度任务类里面重新设置job工厂  _scheduler.jobfactory = _iocjobfactory;

      在ioc中注入 userinfosyncjob、stdschedulerfactory、iocjobfactory    

 services.addtransient<userinfosyncjob>();      // 这里使用瞬时依赖注入
            services.addsingleton<ischedulerfactory, stdschedulerfactory>();//注册ischedulerfactory的实例。
            services.addsingleton<quartzstartup>();
            services.addsingleton<ijobfactory,iocjobfactory>();

 

  修改userinfosyncjob任务类,可以通过构造方法注入的方式从容器中拿到日志实现类、缓存类等等

   

 public class userinfosyncjob : ijob
    {
        private readonly ilogger<userinfosyncjob> _logger;
      //  private readonly icache _cache;
        public userinfosyncjob(ilogger<userinfosyncjob> logger)
        {
            //_cache = cache;
            _logger = logger;// engincontext.current.resolve<ilogger<userinfosyncjob>>();
        }
        public task execute(ijobexecutioncontext context)
        {
            return task.run(() =>
                        {  
                             //.....
                            // console.writeline($"{datetime.now.tostring()}:开始执行同步第三方数据");
                            _logger.loginformation ($"{datetime.now.tostring()}:开始执行同步第三方数据");
                             //....同步操作
                             //  我们都知道如果一个从构造方法中获取ioc容器里面的类型,必须该类型也要主要到ioc容器中;
                           
                        });
        }
    }

     调整后运行截图

具体详细步骤请看源码:https://github.com/lxshwyan/quartzdemo.git