Quartz_将持久化配置写在appsettings.json中

准备工作详见:

Quartz.Net_持久化-CSDN博客


持久化配置: 

"Quartz": {
  "quartz.scheduler.instanceName": "MyScheduler",
  "quartz.jobStore.type": "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
  "quartz.jobStore.dataSource": "ABC",
  "quartz.jobStore.driverDelegateType": "Quartz.Impl.AdoJobStore.MySQLDelegate, Quartz",
  "quartz.jobStore.useProperties": "true",
  "quartz.jobStore.tablePrefix": "QRTZ_",
  "quartz.dataSource.ABC.connectionString": "server=localhost;Database=quartz;user id=root;password=123456;SslMode=none;allowPublicKeyRetrieval=true;",
  "quartz.dataSource.ABC.provider": "MySql",
  "quartz.serializer.type": "json"
}

此处仅作示例,依据实际情况进行修改 

注:需要说明的是,详细配置不可写成对象的形式,必需以XXX.YYY.ZZZ的形式书写

Programs.cs

说明详见注释

// 将配置绑定至QuartzOptions(他处需要QuartzOptions时)
// builder.Services.Configure<QuartzOptions>(builder.Configuration.GetSection("Quartz"));

// Quartz配置
builder.Services.AddQuartz(q =>
{
    // 该函数在可能在未来被移除(高版本中默认使用微软的依赖注入)
    //q.UseMicrosoftDependencyInjectionJobFactory();

    q.UsePersistentStore(options =>
    {
        // 将配置绑定至options
        builder.Configuration.GetSection("Quartz").Bind(options);
    });

    // 可在此处添加Job和Trigger(未调用StoreDurably()也会被持久化)
    var jobKey = new JobKey("Job");
    q.AddJob<MyJob>(ops => ops.WithIdentity(jobKey));
    q.AddTrigger(ops => ops
        .ForJob(jobKey)
        .WithIdentity("Trigger")
        .WithSimpleSchedule(x => x
            .WithIntervalInHours(720)
            .RepeatForever()
        )
    );
});

// Quartz的Host配置(与ASP.NET Core项目同生命周期)
builder.Services.AddQuartzHostedService(options =>
{
    options.WaitForJobsToComplete = true;
});

关于Host的补充说明

如此也可创建Host,但在ASP.NET中无需如此(存在 var app = builder.Build() 与 app.Run())

var host = CreateHostBuilder(args).Build();
host.Run();

static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((context, services) =>
            {
                // 从 appsettings.json 中加载 Quartz 配置
                services.Configure<QuartzOptions>(context.Configuration.GetSection("Quartz"));

                // 添加 Quartz 调度器服务
                services.AddQuartz(q =>
                {
                    q.UseMicrosoftDependencyInjectionJobFactory();

                    //从配置文件加载 Quartz 配置
                    q.UsePersistentStore(options =>
                    {
                        context.Configuration.GetSection("Quartz").Bind(options);
                    });

                    var jobKey = new JobKey("Job");
                    q.AddJob<MyJob>(ops => ops.WithIdentity(jobKey));
                    q.AddTrigger(ops => ops
                    .ForJob(jobKey)
                    .WithDescription("Trigger")
                    .WithSimpleSchedule(x => x
                            .WithIntervalInHours(720)
                            .RepeatForever()
                            )
                    );
                });

                // 添加 Quartz Hosted Service
                services.AddQuartzHostedService(options =>
                {
                    options.WaitForJobsToComplete = true;
                });
            });
如果你使用的是 Spring Boot 框架,可以在 `application.properties` 或 `application.yml` 文件添加以下配置: ```properties # Quartz 配置 ## 指定 Quartz 的 Scheduler 实现类 spring.quartz.scheduler-name = MyScheduler spring.quartz.job-store-type = jdbc spring.quartz.jdbc.initialize-schema = always ## 数据库连接配置 spring.quartz.properties.org.quartz.dataSource.myDS.driver = com.mysql.cj.jdbc.Driver spring.quartz.properties.org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/quartz?serverTimezone=UTC spring.quartz.properties.org.quartz.dataSource.myDS.user = root spring.quartz.properties.org.quartz.dataSource.myDS.password = root ## 配置线程池 spring.quartz.properties.org.quartz.threadPool.threadCount = 10 spring.quartz.properties.org.quartz.threadPool.threadPriority = 5 spring.quartz.properties.org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool ``` 上述配置,我们指定了 Quartz 的实现类为 `MyScheduler`,使用了 JDBC 存储方式,在启动时自动初始化数据库。同时,我们还配置了数据库连接信息和线程池相关配置。 如果你使用的是 `application.yml` 文件,可以按如下格式进行配置: ```yaml # Quartz 配置 spring: quartz: scheduler-name: MyScheduler job-store-type: jdbc jdbc: initialize-schema: always properties: org: quartz: dataSource: myDS: driver: com.mysql.cj.jdbc.Driver URL: jdbc:mysql://localhost:3306/quartz?serverTimezone=UTC user: root password: root threadPool: threadCount: 10 threadPriority: 5 class: org.quartz.simpl.SimpleThreadPool ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值