Tuesday, March 12, 2019

Spring Boot Quartz - Custom Scheduler Factory using QuartzProperties

To have Quartz scheduler in a spring boot project we can follow the steps here.
The example on the link above requires separate properties file for quartz (or manually injecting the property values, see here).

To do it in a cleaner way, quartz configuration should be placed together in the application.yml file. Follow the steps below to adjust the configuration bean:
1. Inject QuartzProperties
@Autowired
QuartzProperties quartzProperties

2. Use the quartz properties on your schedulerFactory
schedulerFactory.setQuartzProperties(asProperties(quartzProperties.getProperties()))

private Properties asProperties(Map source) {
    Properties properties = new Properties();
    properties.putAll(source);
    return properties;
}

3. Done!

Sample quartz configuration in application.yml file:
quartz:
  job-store-type: jdbc
  jdbc:
    initialize-schema: embedded
  overwrite-existing-jobs: true
  scheduler-name: TheScheduler
  properties:
    org:
      quartz:
        scheduler:
          instanceName: TheScheduler
          instanceId: AUTO
        threadPool:
          class: org.quartz.simpl.SimpleThreadPool
          threadCount: 10
          threadPriority: 5
        jobStore:
          misfireThreshold: 60000
          class: org.quartz.impl.jdbcjobstore.JobStoreTX
          driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
          useProperties: false
          tablePrefix: QRTZ_
          isClustered: true
          clusterCheckinInterval: 20000


Reference:

  • https://www.baeldung.com/spring-quartz-schedule
  • https://dzone.com/articles/integrating-quartz-withspring


No comments: