Quartz Scheduler

Spring Boot 为使用 Quartz scheduler 提供了几种便利,包括 spring-boot-starter-quartz “Starter”。如果可以使用 Quartz,则会自动配置 Scheduler(通过 SchedulerFactoryBean 抽象)。

Spring Boot offers several conveniences for working with the Quartz scheduler, including the spring-boot-starter-quartz “Starter”. If Quartz is available, a Scheduler is auto-configured (through the SchedulerFactoryBean abstraction).

以下类型的 Bean 会被自动获取并与 Scheduler 关联:

Beans of the following types are automatically picked up and associated with the Scheduler:

  • JobDetail: defines a particular Job. JobDetail instances can be built with the JobBuilder API.

  • Calendar.

  • Trigger: defines when a particular job is triggered.

默认情况下,使用的是内存中 JobStore。但是,如果应用程序中提供 DataSource Bean,并且 configprop:spring.quartz.job-store-type[] 属性已相应配置,则可以配置基于 JDBC 的存储,如下面的示例所示:

By default, an in-memory JobStore is used. However, it is possible to configure a JDBC-based store if a DataSource bean is available in your application and if the configprop:spring.quartz.job-store-type[] property is configured accordingly, as shown in the following example:

spring:
  quartz:
    job-store-type: "jdbc"

当使用 JDBC 存储时,可以在启动时初始化架构,如下面的示例所示:

When the JDBC store is used, the schema can be initialized on startup, as shown in the following example:

spring:
  quartz:
    jdbc:
      initialize-schema: "always"

默认情况下,使用 Quartz 库提供的标准脚本检测并初始化数据库。这些脚本会删除现有表格,并在每次重启时删除所有触发器。还可以通过设置 configprop:spring.quartz.jdbc.schema[] 属性提供自定义脚本。

By default, the database is detected and initialized by using the standard scripts provided with the Quartz library. These scripts drop existing tables, deleting all triggers on every restart. It is also possible to provide a custom script by setting the configprop:spring.quartz.jdbc.schema[] property.

要让 Quartz 使用除应用程序的主 DataSource 外的其他 DataSource,请声明一个 DataSource Bean,使用 @QuartzDataSource 注释其 @Bean 方法。这样做可确保 SchedulerFactoryBean 和架构初始化均使用 Quartz 专用的 DataSource。类似地,要让 Quartz 使用除应用程序的主 TransactionManager 外的其他 TransactionManager,请声明一个 TransactionManager Bean,使用 @QuartzTransactionManager 注释其 @Bean 方法。

To have Quartz use a DataSource other than the application’s main DataSource, declare a DataSource bean, annotating its @Bean method with @QuartzDataSource. Doing so ensures that the Quartz-specific DataSource is used by both the SchedulerFactoryBean and for schema initialization. Similarly, to have Quartz use a TransactionManager other than the application’s main TransactionManager declare a TransactionManager bean, annotating its @Bean method with @QuartzTransactionManager.

默认情况下,由配置创建的作业不会覆盖从持久作业存储中读取的已注册作业。要覆盖现有的作业定义,请设置 configprop:spring.quartz.overwrite-existing-jobs[] 属性。

By default, jobs created by configuration will not overwrite already registered jobs that have been read from a persistent job store. To enable overwriting existing job definitions set the configprop:spring.quartz.overwrite-existing-jobs[] property.

可以使用 spring.quartz 属性和 SchedulerFactoryBeanCustomizer Bean 自定义 Quartz Scheduler 配置,它们支持以编程方式 SchedulerFactoryBean 自定义。可以使用 spring.quartz.properties.* 自定义高级 Quartz 配置属性。

Quartz Scheduler configuration can be customized using spring.quartz properties and SchedulerFactoryBeanCustomizer beans, which allow programmatic SchedulerFactoryBean customization. Advanced Quartz configuration properties can be customized using spring.quartz.properties.*.

特别是,Executor Bean 与调度程序无关,因为 Quartz 提供了一种通过 spring.quartz.properties 配置调度程序的方法。如果您需要自定义任务执行器,请考虑实现 SchedulerFactoryBeanCustomizer

In particular, an Executor bean is not associated with the scheduler as Quartz offers a way to configure the scheduler through spring.quartz.properties. If you need to customize the task executor, consider implementing SchedulerFactoryBeanCustomizer.

作业可以定义 setter 来注入数据映射属性。普通 bean 也可以以类似的方式注入,如下面的示例所示:

Jobs can define setters to inject data map properties. Regular beans can also be injected in a similar manner, as shown in the following example: