Upgrade quartz to 3.1.0

pull/5083/head
liangshiwei 5 years ago
parent a1f33601c7
commit 46dbe76910

@ -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<AbpQuartzOptions>(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

@ -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<AbpQuartzOptions>(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/).
## 异常处理

@ -15,7 +15,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Quartz" Version="3.0.7" />
<PackageReference Include="Quartz" Version="3.1.0" />
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.1.0" />
</ItemGroup>
<ItemGroup>

@ -1,41 +0,0 @@
using System;
using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Spi;
namespace Volo.Abp.Quartz
{
/// <summary>
/// Get the job from the dependency injection
/// </summary>
public class AbpQuartzJobFactory : IJobFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly ConcurrentDictionary<IJob, IServiceScope> _scopes = new ConcurrentDictionary<IJob, IServiceScope>();
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();
}
}
}

@ -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<AbpQuartzOptions>();
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<ISchedulerFactory>().GetScheduler());
});
Configure<AbpQuartzOptions>(quartzOptions =>
{
@ -30,7 +44,6 @@ namespace Volo.Abp.Quartz
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpQuartzOptions>>().Value;
_scheduler = context.ServiceProvider.GetService<IScheduler>();
_scheduler.JobFactory = context.ServiceProvider.GetService<IJobFactory>();
AsyncHelper.RunSync(() => options.StartSchedulerFactory.Invoke(_scheduler));
}
@ -43,4 +56,4 @@ namespace Volo.Abp.Quartz
}
}
}
}
}

@ -13,6 +13,8 @@ namespace Volo.Abp.Quartz
/// </summary>
public NameValueCollection Properties { get; set; }
public Action<IServiceCollectionQuartzConfigurator> Configurator { get; set; }
/// <summary>
/// How long Quartz should wait before starting. Default: 0.
/// </summary>
@ -45,4 +47,4 @@ namespace Volo.Abp.Quartz
}
}
}
}
}

Loading…
Cancel
Save