Quartz.NET是功能齐全的开源做业调度系统,可用于最小的应用程序到大型企业系统。json
Quartz.NET具备三个主要概念:app
ASP.NET Core经过托管服务对运行“后台任务”具备良好的支持,托管服务在ASP.NET Core应用程序启动时启动,并在应用程序生存期内在后台运行,Quartz.NET版本3.2.0经过Quartz.Extensions.Hosting包引入了对该模式的直接支持,Quartz.Extensions.Hosting能够与ASP.NET Core应用程序一块儿使用,也能够与基于“通用主机”的工做程序服务一块儿使用。函数
虽然.NET Core能够建立“定时”后台服务(例如,每10分钟运行一次任务),但Quartz.NET提供了更为强大的解决方案, 经过使用Cron表达式,您能够确保任务在特定时间(例如,凌晨2:30)运行,或仅在特定的几天运行,或这些时间的任意组合。Quartz.NET还容许您以集群方式运行应用程序的多个实例,以便在任什么时候候都只能运行一个实例。ui
Quartz.NET是一个.NET Standard 2.0 NuGet软件包,因此大部分项目都是支持的,你能够运行安装命令,dotnet add package Quartz.Extensions.Hosting
,或者在NNuget可视化安装,若是查看该项目的.csproj,应该是下边这样:this
<Project Sdk="Microsoft.NET.Sdk.Worker"> <PropertyGroup> <TargetFramework>net5.0</TargetFramework> <UserSecretsId>dotnet-QuartzWorkerService-9D4BFFBE-BE06-4490-AE8B-8AF1466778FD</UserSecretsId> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> <PackageReference Include="Quartz.Extensions.Hosting" Version="3.2.3" /> </ItemGroup> </Project>
安装完成之后,这个包会自动安装 Quartz.NET包,接下来,咱们须要在咱们的应用程序中注册Quartz服务和Quartz 。.net
修改Program.cs,注册服务翻译
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { // Add the required Quartz.NET services services.AddQuartz(q => { // Use a Scoped container to create jobs. I'll touch on this later q.UseMicrosoftDependencyInjectionScopedJobFactory(); }); // Add the Quartz.NET hosted service services.AddQuartzHostedService( q => q.WaitForJobsToComplete = true); // other config }); }
UseMicrosoftDependencyInjectionScopedJobFactory(),这个地方告诉Quartz.NET注册一个IJobFactory,而后从DI容器中获取Job,这样也能够使用 Scoped 类型的服务。日志
WaitForJobsToComplete():当程序关闭时,此设置可确保Quartz.NET在退出以前等待Job正常结束。code
若是如今运行您的应用程序,您将看到Quartz服务启动,并将有不少日志输出到控制台:orm
info: Quartz.Core.SchedulerSignalerImpl[0] Initialized Scheduler Signaller of type: Quartz.Core.SchedulerSignalerImpl info: Quartz.Core.QuartzScheduler[0] Quartz Scheduler v.3.2.3.0 created. info: Quartz.Core.QuartzScheduler[0] JobFactory set to: Quartz.Simpl.MicrosoftDependencyInjectionJobFactory info: Quartz.Simpl.RAMJobStore[0] RAMJobStore initialized. info: Quartz.Core.QuartzScheduler[0] Scheduler meta-data: Quartz Scheduler (v3.2.3.0) 'QuartzScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'Quartz.Core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'Quartz.Simpl.DefaultThreadPool' - with 10 threads. Using job-store 'Quartz.Simpl.RAMJobStore' - which does not support persistence. and is not clustered. info: Quartz.Impl.StdSchedulerFactory[0] Quartz scheduler 'QuartzScheduler' initialized info: Quartz.Impl.StdSchedulerFactory[0] Quartz scheduler version: 3.2.3.0 info: Quartz.Core.QuartzScheduler[0] Scheduler QuartzScheduler_$_NON_CLUSTERED started. info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. ...
如今,您已经将Quartz做为托管服务运行在您的应用程序中,可是如今尚未添加须要运行的Job。
这个地方我建立一个简单的服务,而且我能够从构造函数中获取服务。
using Microsoft.Extensions.Logging; using Quartz; using System.Threading.Tasks; [DisallowConcurrentExecution] public class HelloWorldJob : IJob { private readonly ILogger<HelloWorldJob> _logger; public HelloWorldJob(ILogger<HelloWorldJob> logger) { _logger = logger; } public Task Execute(IJobExecutionContext context) { _logger.LogInformation("Hello world!"); return Task.CompletedTask; } }
我还用[DisallowConcurrentExecution]特性,防止Quartz.NET尝试同时运行同一个做业。
这个地方一般使用Cron表达式,来设置job的执行时间。
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddQuartz(q => { q.UseMicrosoftDependencyInjectionScopedJobFactory(); // Create a "key" for the job var jobKey = new JobKey("HelloWorldJob"); // Register the job with the DI container q.AddJob<HelloWorldJob>(opts => opts.WithIdentity(jobKey)); // Create a trigger for the job q.AddTrigger(opts => opts .ForJob(jobKey) // link to the HelloWorldJob .WithIdentity("HelloWorldJob-trigger") // give the trigger a unique name .WithCronSchedule("0/5 * * * * ?")); // run every 5 seconds }); services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true); // ... });
如今运行应用程序,您将看到和之前相同的启动消息,而后每隔5秒钟就会看到HelloWorldJob写入控制台的信息:
通常状况,咱们都不会把cron表达式写死在代码中,通常是设置在appsettings.json中
{ "Quartz": { "HelloWorldJob": "0/5 * * * * ?" } }
为了更简单的注册服务,这个地方我简单作了一个封装,这样也更灵活。
public static class ServiceCollectionQuartzConfiguratorExtensions { public static void AddJobAndTrigger<T>( this IServiceCollectionQuartzConfigurator quartz, IConfiguration config) where T : IJob { // Use the name of the IJob as the appsettings.json key string jobName = typeof(T).Name; // Try and load the schedule from configuration var configKey = $"Quartz:{jobName}"; var cronSchedule = config[configKey]; // Some minor validation if (string.IsNullOrEmpty(cronSchedule)) { throw new Exception($"No Quartz.NET Cron schedule found for job in configuration at {configKey}"); } // register the job as before var jobKey = new JobKey(jobName); quartz.AddJob<T>(opts => opts.WithIdentity(jobKey)); quartz.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity(jobName + "-trigger") .WithCronSchedule(cronSchedule)); // use the schedule from configuration } }
而后修改Program.cs,而后使用扩展方法:
public class Program { public static void Main(string[] args) => CreateHostBuilder(args).Build().Run(); public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddQuartz(q => { q.UseMicrosoftDependencyInjectionScopedJobFactory(); // Register the job, loading the schedule from configuration q.AddJobAndTrigger<HelloWorldJob>(hostContext.Configuration); }); services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true); }); }
再次运行该应用程序将提供相同的输出:Job每5秒输入一次信息。
原文做者: andrewlock
原文连接: https://andrewlock.net/using-quartz-net-with-asp-net-core-and-worker-services/
欢迎扫码关注咱们的公众号 【全球技术精选】,专一国外优秀博客的翻译和开源项目分享,也能够添加QQ群 897216102