Quartz 调度器优化

以下是调度器配置中的一些关键属性及其意义:

属性 说明
Scheduler Name 实例名称,在集群环境中使用以区分不同调度器实例
Scheduler Id 实例 ID,可以自动生成
Max Batch Size 单次执行的最大作业数量
InterruptJobsOnShutdown ..
InterruptJobsOnShutdownWithWait ..
BatchTriggerAcquisitionFireAheadTimeWindow ..

Microsoft 托管扩展配置示例

使用 Microsoft.Extensions.Hosting 构建应用时,可以通过以下方式配置 Quartz 调度器:

var host = Host.CreateDefaultBuilder()
    .ConfigureServices(services =>
    {
        services.AddQuartz(options =>
        {
            options.SchedulerId = ""; // 设置调度器ID
            options.SchedulerName = ""; // 设置调度器名称
            options.MaxBatchSize = ""; // 设置最大批处理作业数量
            options.InterruptJobsOnShutdown = true; // 设置关闭时中断作业
            options.InterruptJobsOnShutdownWithWait = true; // 设置关闭时等待作业中断
            options.BatchTriggerAcquisitionFireAheadTimeWindow = TimeSpan.Zero; // 设置触发器提前获取时间窗口
        });
    })
    .Build();

手动构建调度器示例

如果您不使用依赖注入或想要更细粒度地控制调度器的创建,可以手动构建调度器:

var scheduler = SchedulerBuilder.Create()
    .WithMisfireThreshold(TimeSpan.FromDays(1)) // 设置错过触发的阈值为 1 天
    .WithId("") // 设置调度器ID
    .WithName("") // 设置调度器名称
    .WithMaxBatchSize(2) // 设置每批次最大作业数为2
    .WithInterruptJobsOnShutdown(true) // 设置关闭时中断作业
    .WithInterruptJobsOnShutdownWithWait(true) // 设置关闭时等待作业中断完成
    .WithBatchTriggerAcquisitionFireAheadTimeWindow(TimeSpan.FromMilliseconds(1)) // 设置触发器预获取时间窗为 1 毫秒
    .Build();
在本文档中