.NET Core使用Quartz執(zhí)行調(diào)度任務(wù)進(jìn)階

一、前言運(yùn)用場(chǎng)景

?Quartz.Net是一個(gè)強(qiáng)大、開(kāi)源、輕量的作業(yè)調(diào)度框架,在平時(shí)的項(xiàng)目開(kāi)發(fā)當(dāng)中也會(huì)時(shí)不時(shí)的需要運(yùn)用到定時(shí)調(diào)度方面的功能,例如每日凌晨需要統(tǒng)計(jì)前一天的數(shù)據(jù),又或者每月初需要統(tǒng)計(jì)上月的數(shù)據(jù)。當(dāng)然也會(huì)出現(xiàn)既要統(tǒng)計(jì)日的也統(tǒng)計(jì)月的還需要進(jìn)行其他的操作。那我們改如何來(lái)寫(xiě)這樣的調(diào)度任務(wù)呢?

二、實(shí)際運(yùn)用(.Net Core 2.2)

在一個(gè)解決方案中創(chuàng)建一個(gè).Net控制臺(tái)應(yīng)用程序及一個(gè)類庫(kù),控制臺(tái)應(yīng)用程序用來(lái)作為程序的啟動(dòng)點(diǎn)。類庫(kù)用來(lái)作為調(diào)度任務(wù)的執(zhí)行程序。


然后我們需要完善一下項(xiàng)目的結(jié)構(gòu),首先我們得在控制臺(tái)應(yīng)用程序中創(chuàng)建一個(gè)Startup類,這個(gè)類也是任務(wù)啟動(dòng)的一個(gè)重要條件。

publicclass Startup

? ? {

? ? ? ? public Startup(IConfiguration configuration)

? ? ? ? {

? ? ? ? ? ? Configuration = configuration;

? ? ? ? }

? ? ? ? publicIConfiguration Configuration {get; }

? ? ? ? // This method gets called by the runtime. Use this method to add services to the container.publicvoid ConfigureServices(IServiceCollection services)

? ? ? ? {

? ? ? ? ? ? services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

? ? ? ? }

? ? ? ? // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.publicvoid Configure(IApplicationBuilder app, IHostingEnvironment env)

? ? ? ? {

? ? ? ? ? ? if (env.IsDevelopment())

? ? ? ? ? ? {

? ? ? ? ? ? ? ? app.UseDeveloperExceptionPage();

? ? ? ? ? ? }

? ? ? ? ? ? else? ? ? ? ? ? {

? ? ? ? ? ? ? ? app.UseExceptionHandler("/Error");

? ? ? ? ? ? }

? ? ? ? ? ? app.UseMvc();

? ? ? ? }

? ? }


然后項(xiàng)目會(huì)報(bào)一定的錯(cuò)誤,根據(jù)錯(cuò)誤信息一步一步解決,解決方案:添加NuGet包?Microsoft.AspNetCore

?????? 解決錯(cuò)誤信息之后意味著目前啟動(dòng)程序還算ok了,接下來(lái)我們可以詳細(xì)講下Quartz調(diào)度任務(wù)執(zhí)行。

因?yàn)槲覀兛隙ú粌H僅執(zhí)行一個(gè)調(diào)度任務(wù),實(shí)際項(xiàng)目運(yùn)行中肯定是多個(gè)調(diào)度任務(wù)一起執(zhí)行的,所以我們思路可以轉(zhuǎn)變一下。在類庫(kù)創(chuàng)建一個(gè)公共啟動(dòng)中心,同時(shí)引用NuGet包:Quartz。然后開(kāi)始創(chuàng)建調(diào)度任務(wù)的公共核心

    private IScheduler scheduler;

? ? ? ? ///<summary>/// 創(chuàng)建調(diào)度任務(wù)的入口

? ? ? ? ///</summary>///<returns></returns>publicasync Task Start()

? ? ? ? {

? ? ? ? ? ? await StartJob();

? ? ? ? }

? ? ? ? ///<summary>/// 創(chuàng)建調(diào)度任務(wù)的公共調(diào)用中心

? ? ? ? ///</summary>///<returns></returns>publicasync Task StartJob()

? ? ? ? {

? ? ? ? ? ? //創(chuàng)建一個(gè)工廠? ? ? ? ? ? NameValueCollection param =new NameValueCollection()

? ? ? ? ? ? {

? ? ? ? ? ? ? ? {? "testJob","test"}? ? ? ? ? ? };

StdSchedulerFactory factory

=new StdSchedulerFactory(param);

? ? ? ? ? ? //創(chuàng)建一個(gè)調(diào)度器? ? ? ? ? ? scheduler =await factory.GetScheduler();

? ? ? ? ? ? //開(kāi)始調(diào)度器await scheduler.Start();

? ? ? ? ? ? //每三秒打印一個(gè)info日志awaitCreateJob("_StartLogInfoJob","_StartLogInfoJob"," 0/3 * * * * ? ");

? ? ? ? ? ? //每五秒打印一個(gè)debug日志awaitCreateJob("_StartLogDebugJob","_StartLogDebugJob"," 0/5 * * * * ? ");

//調(diào)度器時(shí)間生成地址--? ? ? ? http://cron.qqe2.com? ? ? ? }

? ? ? ? ///<summary>/// 停止調(diào)度器? ? ? ? ? ?

? ? ? ? ///</summary>publicvoid Stop()

? ? ? ? {

? ? ? ? ? ? scheduler.Shutdown();

       scheduler=null;

? ? ? ? }

? ? ? ? ///<summary>/// 創(chuàng)建運(yùn)行的調(diào)度器

? ? ? ? ///</summary>///<typeparam name="T"></typeparam>///<param name="name"></param>///<param name="group"></param>///<param name="cronTime"></param>///<returns></returns>publicasyncTask CreateJob(stringname,stringgroup,stringcronTime)where T: IJob

? ? ? ? {

? ? ? ? ? ? //創(chuàng)建一個(gè)作業(yè)varjob = JobBuilder.Create()

? ? ? ? ? ? ? ? .WithIdentity("name"+ name,"gtoup"+group)? ? ? ? ? ? ? ? .Build();

//創(chuàng)建一個(gè)觸發(fā)器vartigger = (ICronTrigger)TriggerBuilder.Create()

? ? ? ? ? ? ? ? .WithIdentity("name"+ name,"group"+ group)

? ? ? ? ? ? ? ? .StartNow()

? ? ? ? ? ? ? ? .WithCronSchedule(cronTime)

? ? ? ? ? ? ? ? .Build();

? ? ? ? ? ? //把作業(yè)和觸發(fā)器放入調(diào)度器中await scheduler.ScheduleJob(job, tigger);

? ? ? ? }


然后再去創(chuàng)建兩個(gè)執(zhí)行業(yè)務(wù)邏輯的類,分別是StartLogInfoJob和StartLogDebugJob


publicclass StartLogInfoJob:IJob

? ? {

? ? ? ? publicasync Task Execute(IJobExecutionContext context)

? ? ? ? {

? ? ? ? ? ? await Start();

? ? ? ? }

? ? ? ? publicasync Task Start()

? ? ? ? {

? ? ? ? ? ? LogHelp.Debug("調(diào)度打印Debug");

? ? ? ? }

? ? }

publicclass StartLogDebugJob : IJob

? ? {

? ? ? ? publicasync Task Execute(IJobExecutionContext context)

? ? ? ? {

? ? ? ? ? ? await Start();

? ? ? ? }

? ? ? ? publicasync Task Start()

? ? ? ? {

? ? ? ? ? ? LogHelp.Info("調(diào)度打印Info");

? ? ? ? }

? ? }


到這里就順利的完成了一個(gè)定時(shí)調(diào)度器來(lái)執(zhí)行任務(wù)了,最后我們得把這個(gè)Program文件重新寫(xiě)一下,控制臺(tái)應(yīng)用程序生成的Program文件不太符合我們需要要求,同時(shí)把調(diào)度器在這里面啟動(dòng)。

class Program

? ? {

? ? ? ? staticvoidMain(string[] args)

? ? ? ? {

? ? ? ? ? ? HandleStart();

? ? ? ? ? ? varwebHostArgs = args.Where(arg => arg !="--console").ToArray();

? ? ? ? ? ? varhost = WebHost.CreateDefaultBuilder(webHostArgs)

? ? ? ? ? ? ? ? .UseStartup()

? ? ? ? ? ? ? ? .UseKestrel(options => {

? ? ? ? ? ? ? ? ? ? options.Limits.MinRequestBodyDataRate =null;

? ? ? ? ? ? ? ? })

? ? ? ? ? ? ? ? .Build();

? ? ? ? ? ? host.Run();

? ? ? ? }

? ? ? ? staticvoid HandleStart()

? ? ? ? {

? ? ? ? ? ? try? ? ? ? ? ? {

? ? ? ? ? ? ? ? new Scheduler().Start().GetAwaiter().GetResult();

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception ex)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? LogHelp.Error(ex);

? ? ? ? ? ? }

? ? ? ? }

? ? }


我們?nèi)タ次募A下面Log文件會(huì)發(fā)現(xiàn)有一個(gè)Debug和一個(gè)Info



到這里我們的調(diào)度就完成了,我們需要使用的時(shí)候?qū)⒋蛴∪罩靖鼡Q成我們?nèi)粘O胍幚淼臉I(yè)務(wù)邏輯就可以了。剛剛提到打印日志就順便提一下在.Net Core中如何打印日志吧。

三、.Net Cor打印日志文件

打印日志文件主要是用到了NuGet包:NLog,然后再加上一個(gè)NLog.config,首先在項(xiàng)目中安裝NLog的包,然后創(chuàng)建一個(gè)LogHelper的公共類。

publicclass LogHelp

? ? {

? ? ? ? staticNLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

? ? ? ? publicstaticvoidDebug(string info)

? ? ? ? {

? ? ? ? ? ? logger.Debug(info);

? ? ? ? }

? ? ? ? publicstaticvoidInfo(string info)

? ? ? ? {

? ? ? ? ? ? logger.Info(info);

? ? ? ? }

? ? ? ? publicstaticvoidError(Exception ex,stringinfo ="")

? ? ? ? {

? ? ? ? ? ? logger.Error(ex);

? ? ? ? }

}


然后再添加一個(gè)NLog.config文件

? ? ? ? ? ? ?

完成這兩個(gè)就可以實(shí)現(xiàn)日志的打印了。。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容