Spring Boot Quartz Scheduler配置

时间:2019-05-21 08:31:19

标签: java spring spring-boot quartz-scheduler

我想将Quartz Scheduling配置移至application.yml,而不是维护单独的quartz.properties文件。

我们的Spring Boot应用程序运行并使用quartz.properties文件时按预期方式选择配置,但不会从application.yml中获取配置。

计划程序bean:

@SpringBootApplication
public class MyApp{
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

    ...

    @Bean
    public Scheduler scheduler(SomeCustomConfig cfg, RestTemplate restTemplate) throws SchedulerException {
        //StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
        //schedulerFactory.initialize("quartz.properties");
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.getContext().put("restTemplate", restTemplate);
        scheduler.getContext().put("cfg", cfg);
        return scheduler;
    }

}

相关的application.yml

spring:
    application.name: myApp
    quartz:
        properties:
            org:
                quartz:
                    scheduler:
                        instanceId: AUTO
                    threadPool:
                        threadCount: 5
                    plugin:
                        shutdownhook:
                            class: org.quartz.plugins.management.ShutdownHookPlugin
                            cleanShutdown: TRUE
                    jobStore:
                        class: org.quartz.impl.jdbcjobstore.JobStoreTX
                        driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
                        tablePrefix: my_schema.
                        isClustered: true
                        dataSource: myDataSource
                    dataSource:
                        myDataSource:
                            driver: org.postgresql.Driver
                            URL: jdbc:postgresql://localhost/myDataSource
                            user: removed
                            password: removed

我们的quartz.properties是:

org.quartz.scheduler.instanceId = AUTO
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = TRUE
org.quartz.threadPool.threadCount = 5
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.tablePrefix = my_schema.
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.dataSource = myDataSource
org.quartz.dataSource.myDataSource.driver = org.postgresql.Driver
org.quartz.dataSource.myDataSource.URL = jdbc:postgresql://localhost/myDataSource
org.quartz.dataSource.myDataSource.user = removed
org.quartz.dataSource.myDataSource.password = removed

我感觉自己缺少什么?

4 个答案:

答案 0 :(得分:1)

使其正常工作。

切换为使用spring-boot-starter-quartz代替org.quartz-scheduler,并将application.yml更改为:

spring:
  quartz:
    job-store-type: jdbc
    properties:
      org:
        quartz:
          scheduler:
            instanceId: AUTO
          threadPool:
            threadCount: 5
          ...etc

并更改应用程序以使用注入的SchedulerFactoryBean

@Bean
public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean, SomeCustomConfig cfg, RestTemplate restTemplate) throws SchedulerException {
    Scheduler scheduler = schedulerFactoryBean.getScheduler();
    scheduler.getContext().put("restTemplate", restTemplate);
    scheduler.getContext().put("cfg", cfg);
    return scheduler;
}

答案 1 :(得分:1)

代替

spring:
  quartz:
    properties:
      org:
        quartz:
          jobStore:
            isClustered: true

使用此布局:

spring:
  quartz:
    properties:
      org.quartz.jobStore:
        isClustered: true
      org.quartz.scheduler:
        instanceId: AUTO

使用后一种布局,我得到:

2019-09-06 13:45:19.919  INFO PID --- [           main] o.q.c.QuartzScheduler                    : {} Scheduler meta-data: Quartz Scheduler (v2.3.0) 'quartzScheduler' with instanceId '0157799997'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is clustered.

答案 2 :(得分:0)

您为application.yml配置了spring-boot-starter-quartz,我认为您正在独立使用org.quartz-scheduler。因此,您应该像这样配置application.yml

spring:
    application.name: myApp
org:
    quartz:
        scheduler:
            instanceId: AUTO
        threadPool:
            threadCount: 5
        plugin:
            shutdownhook:
                class: org.quartz.plugins.management.ShutdownHookPlugin
                cleanShutdown: TRUE
        jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
            tablePrefix: my_schema.
            isClustered: true
            dataSource: myDataSource
        dataSource:
            myDataSource:
                driver: org.postgresql.Driver
                URL: jdbc:postgresql://localhost/myDataSource
                user: removed
                password: removed

答案 3 :(得分:0)

我最近与Spring Boot Quartz Application一起工作,并且遇到了类似的问题,在我正在使用application.yml来保存应用程序环境变量的应用程序中,未检测到quartz.properties

func

通过使用上述格式的上述配置,我不仅能够触发Quartz作业,而且还能够存储在数据库中