Lycoris.Quartz.Extensions 6.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Lycoris.Quartz.Extensions --version 6.1.1
                    
NuGet\Install-Package Lycoris.Quartz.Extensions -Version 6.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lycoris.Quartz.Extensions" Version="6.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lycoris.Quartz.Extensions" Version="6.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Lycoris.Quartz.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lycoris.Quartz.Extensions --version 6.1.1
                    
#r "nuget: Lycoris.Quartz.Extensions, 6.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lycoris.Quartz.Extensions@6.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lycoris.Quartz.Extensions&version=6.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Lycoris.Quartz.Extensions&version=6.1.1
                    
Install as a Cake Tool

基于Quartz做了一层简单封装,支持Scoped生命周期直接在构造函数注入,简化了使用成本。

安装方式

// net cli
dotnet add package Lycoris.Quartz.Extensions
// package manager
Install-Package Lycoris.Quartz.Extensions

一、注册Quartz调度中心

builder.Services.AddQuartzSchedulerCenter();

二、创建调度任务

创建调度任务的两种方式

基类会根据设置的任务运行截至事件自动停止任务,并且基类中也做了异常捕捉,除了你业务中必要的业务捕捉要,其他未知异常基类都能帮你及时记录

1. 继承扩展的基类

基类中包含以下两个可读属性
  • JobTraceId:当前执行的任务TraceId
  • JobName:当前执行的任务名称
[QuartzJob("测试任务", Trigger = QuartzTriggerEnum.SIMPLE, IntervalSecond = 5)]
public class TestJob : BaseQuartzJob
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    protected override Task DoWorkAsync(IJobExecutionContext context)
    {
        Console.WriteLine($"TestJob => {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
        return Task.CompletedTask;
    }
}

创建好调度任务后,还需要设置任务的配置,设置很简单,在任务类上加上特性[QuartzJob("测试任务", Trigger = QuartzTriggerEnum.SIMPLE, IntervalSecond = 5)]

QuartzJobAttribute使用指南:

  • 构造函数入参为定时任务名称
  • Trigger为触发器类型,分为普通定时器SIMPLE和Cron定时器CRON
  • IntervalSecond:定时秒数,该配置仅对普通定时器有效
  • Cron:如果是Corn定时器,需要配置Cron表达式
  • RunTimes:执行次数,默认是无线循环

  • 2. 继承来自QuartzIJob接口
public class TestJob : IJob
{
    public Task Execute(IJobExecutionContext context)
    {
        return Task.CompletedTask;
    }
}

三、注册调度任务

注册调度任务(以下ServiceBuilder类为演示自行编写的扩展类部分,不包含在当前扩展类库之内,仅做使用示例使用)

public static class ServiceBuilder
{
    // 推荐使用
    public static void AddQuartzJobBuilder(this IServiceCollection services)
    {
        services.AddQuartzJob<TestJob>()
                .AddQuartzJob<TestJob2>()
                .QuartzJobBuild();
    }

    // 推荐使用
    public static void AddQuartzJobBuilder(this IServiceCollection services)
    {
        services.AddQuartzJob(typeof(TestJob), typeof(TestJob2)).QuartzJobBuild();
    }

    // 请优先使用前两种注册方式
    public static void AddQuartzJobBuilder(this IServiceCollection services)
    {
        services.AddQuartzJobAssembly(MethodBase.GetCurrentMethod().ReflectedType.Assembly).QuartzJobBuild();
    }
}

注册任务的另一种方式:注册调度中心时同时注册调度任务

 builder.Services.AddQuartzSchedulerCenter(buider =>
 {
     buider.AddJob<TestJob>();
     buider.AddJob<TestJob2>();
 });

四、调度任务也支持原生的两个特性

  • PersistJobDataAfterExecutionAttribute: 这一次的结果作为值传给下一次
[PersistJobDataAfterExecution]
public class TestJob : BaseQuartzJob
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    protected override Task DoWork(IJobExecutionContext context)
    {
        // 取出的是上一次保存的值
        var val = context.GetJobDataMap("Key");

        // todo

        // 保存这一次的值
        context.AddJobDataMap("Key", "这一次的传值");

        return Task.CompletedTask;
    }
}
  • DisallowConcurrentExecutionAttribute: 只有上一个任务完成才会执行下一次任务
    [DisallowConcurrentExecution]
    public class TestJob : BaseQuartzJob
    

五、数据库支持

扩展不做数据库支持,但是Quartz原生带有各种监听服务,需要使用到数据库做数据持久化的,请自行开发,可实现的接口如下:

  • ISchedulerListener:调度器执行监听
  • ITriggerListener:调度器执行监听
  • IJobListener:调度任务执行监听

由于原生的接口有些有很多方法需要实现,如果想偷懒的小伙伴可以继承我处理好的基类:

  • SchedulerListener
  • TriggerListener
  • JobListener

仅需重写你需要使用到的监听方法即可

注册监听

 builder.Services.AddQuartzSchedulerCenter(buider =>
 {
     buider.AddSchedulerListener<CustomeSchedulerListener>();
     buider.AddTriggerListener<CustomeTriggerListener>();
     buider.AddJobListener<CustomeJobListener>();
 });
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.