ABP Quartz.NET “最佳“实践-1 配置Quartz.NET,注入任务

在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中怎么动态创建任务的给贴出来。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值