Spring 4 and Quartz 2 Integration with Custom Annotations
31 May 2016
I’m integrating Quartz scheduling into an application, and was looking for an annotation based approach to configuration. I quickly found the SivaLabs implementation, which works for Quartz 1.8. I’ve made a few changes to use this with Spring 4.1.4 and Quartz 2.2.3.
The custom annotation remains the same:
The major change is that I have replaced the ApplicationListener class with a bean that performs a single scan of a selected package. I chose this approach because my configuration does not change once loaded, and I wanted to throw a fatal exception on any error in Quartz configuration. I’ve refactored to add each job to the scheduler as it is discovered, rather than building an intermediate list.
The CronTriggerBean and JobDetailBean from Spring 3 are gone in Spring 4. The new code builds the job using the Quartz 2 fluent builders, rather than Spring’s factory beans:
The job factory is unchanged from the original article, and the XML configuration looks like this:
At this point jobs are automatically configured as long they:
implement the Job interface
have the QuartzJob annotation
are in a package scanned by Spring, and by the QuartzJobScanner