在ABP中不采用Background Jobs和Background Workers这两种方式而直接使用Quartz.NET。由于 Quartz.NET的版本变化,以及知识的更新,在网上找到的诸多参考,收效不佳,最后根据官网资料。一句总结,官网上的才是最佳实践。参考文章Quartz.NET ASP.NET Core Integration
结合ABP框架的使用,就需要在工程中引入Quartz.AspNetCore包,然后某个Model配置服务了。重点也就是前面链接中给出来的
public void ConfigureServices(IServiceCollection services)
{
services.AddQuartz(q =>
{
// base quartz scheduler, job and trigger configuration
});
// ASP.NET Core hosting
services.AddQuartzServer(options =>
{
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
}
另外,把可能用到的Job注入下。
这里我把自己的代码贴出
//在modle的ConfigureService中调用
private void ConfigureJobServices(ServiceConfigurationContext context)
{
//获取配置文件
var configuration = context.Services.GetConfiguration();
//配置参数
context.Services.Configure<QuartzOptions>(options =>
{
options.Scheduling.IgnoreDuplicates = true;
options.Scheduling.OverWriteExistingData = true;
});
//
context.Services.AddQuartz(q => {
q.UseTimeZoneConverter();
q.SchedulerId = "JobSchedulerCore";
q.InterruptJobsOnShutdown = true;
q.InterruptJobsOnShutdownWithWait = true;
q.MaxBatchSize = 5;
q.SchedulerName = "JobScheduler";
q.UseMicrosoftDependencyInjectionJobFactory();
q.UseSimpleTypeLoader();
q.UseDefaultThreadPool(10);
//配置数据库
q.UsePersistentStore(s =>
{
s.UseProperties=true;
s.UseJsonSerializer();
//使用了SQLite数据库
s.UseMicrosoftSQLite(sqlite => {
sqlite.TablePrefix = "QRTZ_";
sqlite.ConnectionString =
configuration.GetConnectionString("Quartz");
sqlite.UseDriverDelegate<SQLiteDelegate>();
});
s.SetProperty("type", "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz");
});
});
//可以看看源码里面里面具体做了那些工作
context.Services.AddQuartzServer(options =>
{
options.WaitForJobsToComplete = true;
options.AwaitApplicationStarted = true;
});
//注入下后面可能用到的任务
context.Services.AddTransient<SimpleJob>();
}
强烈 建议找到Quartz.NET 示例工程的源码看看,。在实例工程中有直接注册任务等用法,我这里没有使用,下一次把在ABP中怎么动态创建任务的给贴出来。