From 46dbe7691070d88382d8afd6e3c740e82f8d4720 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 17 Aug 2020 14:00:59 +0800 Subject: [PATCH] Upgrade quartz to 3.1.0 --- docs/en/Background-Jobs-Quartz.md | 36 ++++++++++++++++ docs/zh-Hans/Background-Jobs-Quartz.md | 38 ++++++++++++++++- .../Volo.Abp.Quartz/Volo.Abp.Quartz.csproj | 3 +- .../Volo/Abp/Quartz/AbpQuartzJobFactory.cs | 41 ------------------- .../Volo/Abp/Quartz/AbpQuartzModule.cs | 25 ++++++++--- .../Volo/Abp/Quartz/AbpQuartzOptions.cs | 4 +- 6 files changed, 97 insertions(+), 50 deletions(-) delete mode 100644 framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzJobFactory.cs diff --git a/docs/en/Background-Jobs-Quartz.md b/docs/en/Background-Jobs-Quartz.md index aa087ace0c..ebd92c3cc1 100644 --- a/docs/en/Background-Jobs-Quartz.md +++ b/docs/en/Background-Jobs-Quartz.md @@ -70,6 +70,42 @@ public class YourModule : AbpModule } ```` +Starting from ABP 3.1 version, we have added `Configurator` to `AbpQuartzOptions` to configure Quartz. For example: + +````csharp +[DependsOn( + //...other dependencies + typeof(AbpBackgroundJobsQuartzModule) //Add the new module dependency + )] +public class YourModule : AbpModule +{ + public override void PreConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); + + PreConfigure(options => + { + options.Configurator = configure => + { + configure.UsePersistentStore(storeOptions => + { + storeOptions.UseProperties = true; + storeOptions.UseJsonSerializer(); + storeOptions.UseSqlServer(configuration.GetConnectionString("Quartz")); + storeOptions.UseClustering(c => + { + c.CheckinMisfireThreshold = TimeSpan.FromSeconds(20); + c.CheckinInterval = TimeSpan.FromSeconds(10); + }); + }); + }; + }); + } +} +```` + +> You can choose the way you favorite to configure Quaratz. + Quartz stores job and scheduling information **in memory by default**. In the example, we use the pre-configuration of [options pattern](Options.md) to change it to the database. For more configuration of Quartz, please refer to the Quartz's [documentation](https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/index.html). ## Exception handling diff --git a/docs/zh-Hans/Background-Jobs-Quartz.md b/docs/zh-Hans/Background-Jobs-Quartz.md index 61de4548f5..427276baaf 100644 --- a/docs/zh-Hans/Background-Jobs-Quartz.md +++ b/docs/zh-Hans/Background-Jobs-Quartz.md @@ -70,7 +70,43 @@ public class YourModule : AbpModule } ```` -Quartz**默认**将作业与调度信息存储在**内存**中,示例中我们使用[选项模式](Options.md)的预配置将其更改为存储到数据库中. 有关Quartz的更多配置请参阅[Quartz文档](https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/index.html). +从ABP3.1版本开始,我们在 `AbpQuartzOptions` 添加了 `Configurator` 用于配置Quartz. 例: + +````csharp +[DependsOn( + //...other dependencies + typeof(AbpBackgroundJobsQuartzModule) //Add the new module dependency + )] +public class YourModule : AbpModule +{ + public override void PreConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); + + PreConfigure(options => + { + options.Configurator = configure => + { + configure.UsePersistentStore(storeOptions => + { + storeOptions.UseProperties = true; + storeOptions.UseJsonSerializer(); + storeOptions.UseSqlServer(configuration.GetConnectionString("Quartz")); + storeOptions.UseClustering(c => + { + c.CheckinMisfireThreshold = TimeSpan.FromSeconds(20); + c.CheckinInterval = TimeSpan.FromSeconds(10); + }); + }); + }; + }); + } +} +```` + +> 你可以选择你喜爱的方式来配置Quaratz. + +Quartz**默认**将作业与调度信息存储在**内存**中,示例中我们使用[选项模式](Options.md)的预配置将其更改为存储到数据库中. 有关Quartz的更多配置请参阅[Quartz文档](https://www.quartz-scheduler.net/). ## 异常处理 diff --git a/framework/src/Volo.Abp.Quartz/Volo.Abp.Quartz.csproj b/framework/src/Volo.Abp.Quartz/Volo.Abp.Quartz.csproj index 8e29b1f803..ad1ac26017 100644 --- a/framework/src/Volo.Abp.Quartz/Volo.Abp.Quartz.csproj +++ b/framework/src/Volo.Abp.Quartz/Volo.Abp.Quartz.csproj @@ -15,7 +15,8 @@ - + + diff --git a/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzJobFactory.cs b/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzJobFactory.cs deleted file mode 100644 index 428dbe03af..0000000000 --- a/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzJobFactory.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Collections.Concurrent; -using Microsoft.Extensions.DependencyInjection; -using Quartz; -using Quartz.Spi; - -namespace Volo.Abp.Quartz -{ - /// - /// Get the job from the dependency injection - /// - public class AbpQuartzJobFactory : IJobFactory - { - private readonly IServiceProvider _serviceProvider; - - private readonly ConcurrentDictionary _scopes = new ConcurrentDictionary(); - - public AbpQuartzJobFactory(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } - - public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) - { - var scope = _serviceProvider.CreateScope(); - var job = scope.ServiceProvider.GetRequiredService(bundle.JobDetail.JobType) as IJob; - if (job == null) - { - throw new ArgumentException("Given job does not implement IJob"); - } - _scopes.TryAdd(job, scope); - return job; - } - - public void ReturnJob(IJob job) - { - _scopes.TryRemove(job, out var serviceScope); - serviceScope?.Dispose(); - } - } -} diff --git a/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzModule.cs b/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzModule.cs index 3a2413a9b8..ec1d8a9a32 100644 --- a/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzModule.cs +++ b/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzModule.cs @@ -1,8 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Quartz; -using Quartz.Impl; -using Quartz.Spi; using Volo.Abp.Modularity; using Volo.Abp.Threading; @@ -15,8 +13,24 @@ namespace Volo.Abp.Quartz public override void ConfigureServices(ServiceConfigurationContext context) { var options = context.Services.ExecutePreConfiguredActions(); - context.Services.AddSingleton(AsyncHelper.RunSync(() => new StdSchedulerFactory(options.Properties).GetScheduler())); - context.Services.AddSingleton(typeof(IJobFactory), typeof(AbpQuartzJobFactory)); + + context.Services.AddQuartz(options.Properties, build => + { + build.UseMicrosoftDependencyInjectionScopedJobFactory(); + // these are the defaults + build.UseSimpleTypeLoader(); + build.UseInMemoryStore(); + build.UseDefaultThreadPool(tp => + { + tp.MaxConcurrency = 10; + }); + options.Configurator?.Invoke(build); + }); + + context.Services.AddSingleton(serviceProvider => + { + return AsyncHelper.RunSync(() => serviceProvider.GetService().GetScheduler()); + }); Configure(quartzOptions => { @@ -30,7 +44,6 @@ namespace Volo.Abp.Quartz var options = context.ServiceProvider.GetRequiredService>().Value; _scheduler = context.ServiceProvider.GetService(); - _scheduler.JobFactory = context.ServiceProvider.GetService(); AsyncHelper.RunSync(() => options.StartSchedulerFactory.Invoke(_scheduler)); } @@ -43,4 +56,4 @@ namespace Volo.Abp.Quartz } } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzOptions.cs b/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzOptions.cs index 9998676042..7dbf415cac 100644 --- a/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzOptions.cs +++ b/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzOptions.cs @@ -13,6 +13,8 @@ namespace Volo.Abp.Quartz /// public NameValueCollection Properties { get; set; } + public Action Configurator { get; set; } + /// /// How long Quartz should wait before starting. Default: 0. /// @@ -45,4 +47,4 @@ namespace Volo.Abp.Quartz } } } -} \ No newline at end of file +}