.net6 webapi项目使用quartz

4 篇文章 1 订阅

.net6 webapi项目使用quartz做定时任务,之前已经写过文章入门了解了quartz的使用

.net6 webapi项目需要引入如下包(版本号都是3.6.0)

Install-Package Quartz
Install-Package Quartz.Extensions.Hosting
Install-Package Quartz.Extensions.DependencyInjection

然后在Program.cs中加入如下配置代码 

//quartz 定时任务
builder.Services.AddQuartz(q =>
{
    q.SchedulerId = "FactorySchedule";
    q.UseMicrosoftDependencyInjectionJobFactory();//注入
    //默认设置
    q.UseSimpleTypeLoader();
    q.UseInMemoryStore();
    q.UseDedicatedThreadPool(tp =>
    {
        tp.MaxConcurrency = 10;
    });

    // quickest way to create a job with single trigger is to use ScheduleJob
    // (requires version 3.2)
    //q.ScheduleJob<TestJob>(trigger => trigger
    //    .WithIdentity("TestJob")
    //    .StartAt(DateBuilder.EvenSecondDate(DateTimeOffset.UtcNow.AddSeconds(7)))
    //    .WithDailyTimeIntervalSchedule(x => x.WithInterval(1, IntervalUnit.Minute))
    //    .WithDescription("TestJobDescription")
    //);

    var jobKey = new JobKey("TestJob");
    q.AddJob<CaseSummaryJob>(opts => opts.WithIdentity(jobKey));
    q.AddTrigger(opts => opts
       .ForJob(jobKey)
       .WithIdentity("TestJob-trigger")
       .WithSimpleSchedule(x => x.WithIntervalInMinutes(3).RepeatForever())
       .StartNow()
   //This Cron interval can be described as "run every minute" (when second is zero)
   //.WithCronSchedule("0 * * ? * *")
   );

});

// we can use options pattern to support hooking your own configuration
// because we don't use service registration api, 
// we need to manually ensure the job is present in DI
builder.Services.AddTransient<TestJob>();
// Quartz.Extensions.Hosting allows you to fire background service that handles scheduler lifecycle
// when shutting down we want jobs to complete gracefully
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);

参考官方文档:Quartz.NET

其他quartz介绍参考C# quartz.net 定时任务(一)_woflyoycm的博客-CSDN博客_c#定时job

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值