Clustered Quartz Scheduler With Spring Boot and MongoDB

Spring Boot library for Quartz does not work correctly if you start up two instances of service in parallel. Each one of the services starts the execution of the same jobs while the expected behavior is one of the instances is elected to execute the jobs and the other ones waiting in the background, and in the case of failure of the first service, another one is elected.

Springs’ @Scheduled annotation is a very convenient and easy way of scheduling tasks for several reasons. But, if you have a clustered environment and you must run any job just from one node, that does not hold true. You realize that all scheduled jobs will start executing almost at the same time. This may cause problems ranging from unnecessary calls to data inconsistency.  

Adding Quartz to Spring Boot

In my "Specifications to the Rescue" article, I presented the ease and ability to leverage the JPA Specification in Spring Boot to provide filter options for your RESTful API.  I followed up with another article, called "Testing those Specifications," which covered how those very specifications can be tested.

Taking things another step further, I thought I would demonstrate just how easy it is to add a job scheduler into the same Spring Boot application.