Search code examples
springspring-bootquartz-scheduler

Spring-Boot with Quartz and multiple schedulers


I am working with a scenario where we have one database with multiple schemas, one schema for each customer. This allows each customer to set different schedules for their jobs. All schemas have the same set of jobs, only the schedules differ.

I need to write one Spring-Boot app to run all jobs from all schemas.

It seems like this would be done by defining different quartz.properties for each schema, and then configuring a different Scheduler for each one, like this:

@SpringBootApplication
@Configuration
public class MyApplication{

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

    @Bean
    public Scheduler schedulerA(Trigger trigger, JobDetail job) {
        StdSchedulerFactory factory = new StdSchedulerFactory();
        factory.initialize(new ClassPathResource("quartzA.properties").getInputStream());

        Scheduler scheduler = factory.getScheduler();
        scheduler.setJobFactory(springBeanJobFactory());
        scheduler.scheduleJob(job, trigger);

        scheduler.start();
        return scheduler;
    }

    @Bean
    public Scheduler schedulerB(Trigger trigger, JobDetail job) {
        StdSchedulerFactory factory = new StdSchedulerFactory();
        factory.initialize(new ClassPathResource("quartzB.properties").getInputStream());

        Scheduler scheduler = factory.getScheduler();
        scheduler.setJobFactory(springBeanJobFactory());
        scheduler.scheduleJob(job, trigger);

        scheduler.start();
        return scheduler;
    }    
}

My question is, is this correct? Can I just define these schedulers in my SpringBootApplication class annotated with @Configuration, and expect it to work (assuming the properties are correct)? Am I missing anything?


Solution

  • My question is, is this correct? Can I just define these schedulers in my SpringBootApplication class annotated with @Configuration

    This is correct. Alternatively you can use Spring @Schelduled annotation with a Cron defined in properties files.

    @Scheduled(cron = "0 15 10 15 * ?")
    public void scheduleTaskUsingCronExpression() {
    .
    .
    .
    

    But, if you want more control over the jobs like failover, retry policy or track and run/rerun jobs from a dashboard. Think of spring-batch