1
votes

I want to move our Quartz Scheduling configuration to our application.yml instead of maintaining a separate quartz.properties file.

Our Spring Boot application runs and picks up the configuration as expected when using quartz.properties file, but it doesn't pick up the config from application.yml.

Scheduler 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;
    }

}

Pertinent 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

Our quartz.properties was:

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

I feel like I'm missing something?

3
Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.Michael Dodd
Use spring-boot-starter-quartz instead of org.quartz-scheduler. Currently you create all required beans on your own, moreover, you bind config file with appropriate bean on your own as well. In earlier versions of spring boot (pre 2.x), there was no such a starter but currently, you could benefit from it. docs - docs.spring.io/spring-boot/docs/current/reference/html/…Dominik

3 Answers

3
votes

Instead of

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

Use this layout:

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

With the latter layout, I get:

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
votes

Your application.yml configuration sets for spring-boot-starter-quartz and I think you are using org.quartz-scheduler independently. So you should config your application.yml something like this:

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
0
votes

I have recently worked with Spring Boot Quartz Application and was facing a similar issue where the quartz.properties was not being detected by application where I was using application.yml to hold application environment variable

spring:
 quartz:
  properties:
   org.quartz.scheduler:
    instanceName: ${QUARTZ_SCHEDULER_INSTANCE_NAME:Scheduler}
    instanceId: ${QUARTZ_SCHEDULER_INSTANCE_ID:AUTO}
    makeSchedulerThreadDaemon: ${QUARTZ_SCHEDULER_MAKE_THREAD_DAEMON:true}
   org.quartz.jobStore:
    class: ${QUARTZ_JOBSTORE_CLASS:org.quartz.impl.jdbcjobstore.JobStoreTX}
    driverDelegateClass: ${QUARTZ_JOBSTORE_DRIVER:org.quartz.impl.jdbcjobstore.PostgreSQLDelegate}
    tablePrefix: ${QUARTZ_JOBSTORE_TABLE_PREFIX:qrtz_}
    isClustered: ${QUARTZ_JOBSTORE_ISCLUSTER:false}
    dataSource: ${QUARTZ_JOBSTORE_DATASOURCE:myDS}
    misfireThreshold: ${QUARTZ_JOBSTORE_MISFIRE_THRESHOLD:25000}
   org.quartz.threadPool:
    class: ${QUARTZ_THREADPOOL_CLASS:org.quartz.simpl.SimpleThreadPool}
    makeThreadsDaemons: ${QUARTZ_THREADPOOL_DAEMON:true}
    threadCount: ${QUARTZ_THREADPOOL_COUNT:20}
    threadPriority: ${QUARTZ_THREADPOOL_PRIORITY:5}
  org.quartz.dataSource:
    myDS:
      driver: ${SPRING_DATASOURCE_DRIVER:org.postgresql.Driver}
      URL: ${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/postgres}
      user: ${SPRING_DATASOURCE_USERNAME:postgres}
      password: ${SPRING_DATASOURCE_PASSWORD:postgres}
      maxConnections: ${SPRING_DATASOURCE_MAX_CONNECTION:20}
      validationQuery: ${SPRING_DATASOURCE_VALIDATION_QUERY:select 1}

By using the above configuration in the above format, I was not only able to trigger Quartz jobs , i was also able to store in database